Since we have created the project through pycharm, copy the entire project directly to the same folder with the Dockerfile. We named the project SwieProject. The executable file wanlidaserver.py is in the project directory SwieProject. When executing the "WORKDIR /app/SwieProject" command, set "/app/SwieProject" to the working directory of the container. So we can execute the file wanlidaserver.py in the current directory.
4. We are ready to build the app. Make sure you are still at the top level of your new directory. Here’s what ls should show:
$ ls
Dockerfile requirements.txt SwieProject
5. Now run the build command to create a Docker image of swieApp
$ docker build -t swieApp .
Here we have successfully created a swieApp image. And I have already shared it with my remote repository and will use it when deploying the app. The image name is haizeiwang/wanlida_server:v1
四.Delploy your project with docker-compose.yml
1. Edit the docker-compose.yml
A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.Content is as follows:
version: "3" # Use version 3
services: # The service to be established by the project
db: # The name of the database service
image: mysql:5.7 # Use the official mysql database image, 5.7 is the version number
expose:
- "3306" # Make port 3306 available to the world outside this container
volumes: # Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
- ./db:/var/lib/mysql # Persist the data of the container mysql to the host db directory
ports:
- "4000:3306" # Map container 3306 port to host 4000 port
environment: # What the database initialization process needs to do
- MYSQL_DATABASE=Swissmic_WMS # Create a database called Swissmic_WMS
- MYSQL_ROOT_PASSWORD=root # Set the root account password to root
- MYSQL_USER=swie # Create a user with a database named Swie
- MYSQL_PASSWORD=xiaomanniubi123 # Set the password for the Swie user as xiaomanniubi123
depends_on: # Control startup sequence
- activemq # Start after the activemq service starts
activemq: # The name of the activeMQ service
image: haizeiwang/activemq:v1 # Use the image we created earlier
volumes:
- ./activeMQ:/var/lib/activeMQ # Persist the data of the container activeMQ to the host activeMQ directory
stdin_open: yes # Keep the service up and running. Note: The "yes" can be changed to "true".
tty: yes # Keep in terminal output
privileged: yes # Make the root of the container have root privileges on the external host, otherwise it is just a normal user right of the external host.
ports:
- "1884:1884" # Map container 1884 port to host 1884 port
web: # The name of the web project service
image: haizeiwang/wanlida_server:v1 # Use the wanlida_server image we created earlier
ports:
- "8080:8080" # Map container 8080 port to host 8080 port
links: # Command to connect to other services. The parameter directly uses the name of the service.
- db # Command to connect to database service
- activemq # Command to connect to activeMQ service
depends_on:
- db # Start after the db service starts
2. Deploy application services
The whole file means that the entire project needs to start three services, the startup sequence is activemq>db>web,the web is the last one started and must be the last one.Next we have two ways to run this yml file, which is to deploy the application service.
Method 1: Use the docker-compose command
a. Create and start services
$ docker-compose up