Making Docker and Deployment Process
Step:
set up your docker environment
build a image of activeMQ with Dockerfile
build a image of swieApp with Dockerfile
delploy your project with docker-compose.yml
一.Set up your docker environment
Install Docker CE Offline
1.Download three packages:containerd.io_1.2.4-1_amd64.deb,docker-ce-cli_18.09.3_3-0_ubuntu-bionic_amd64.deb,docker-ce_18.09.3_3-0_ubuntu-bionic_amd64.deb from https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/
2.Install containerd.io
$ sudo apkg -i containerd.io_1.2.4-1_amd64.deb
3.Install docker client
$ sudo apkg -i docker-ce-cli_18.09.3_3-0_ubuntu-bionic_amd64.deb
4.Install docker server
$ sudo apkg -i docker-ce_18.09.3_3-0_ubuntu-bionic_amd64.deb
5.Check if docker is installed successfully
$ sudo docker -v #Just check if the docker-ce-cli client package is installed successfully.
$ sudo socker images #View the local image, if the error is reported, the installation is not successful.
$ sudo docker run hello-world #Check online if docker is installed successfully.
Install Docker CE Online
Before you install Docker CE for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
1.SET UP THE REPOSITORY
1. Update the apt package index:
$ sudo apt-get update
2. Install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
3. Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
(Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.
$ sudo apt-key fingerprint 0EBFCD88
pub
4096R/0EBFCD88 2017-02-22
Key fingerprint =
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid
Docker Release (CE deb) <docker@docker.com>
sub
4096R/F273FCD8 2017-02-22)
4. Use the
following command to set up the stable repository.
$ sudo
add-apt-repository \
"deb
[arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
2.INSTALL DOCKER
CE
1. Update the
apt package index.
$ sudo
apt-get update
2. Install the
latest version of Docker CE, or go to the next step to install a
specific version:
$ sudo
apt-get install docker-ce
3. To install
a specific version of Docker CE, list the available versions in the
repo, then select and install:
a. List the
versions available in your repo:
$
apt-cache madison docker-ce
b. Install
a specific version using the version string from the second column,
for example, 5:18.09.1~3-0~ubuntu-xenial.
$ sudo
apt-get install docker-ce=5:18.09.1~3-0~ubuntu-xenial
4. Verify that
Docker CE is installed correctly by running the hello-world image.
$ sudo
docker run hello-world
(This
command downloads a test image and runs it in a container. When the
container runs, it prints an informational message and exits.)
二.Build a image of
activeMQ with Dockerfile
1. Edit Dockerfile
Create an empty
directory. Change directories (cd) into the new directory, create a
file called Dockerfile, copy-and-paste the following content into
that file, and save it. Take note of the comments that explain each
statement in your new Dockerfile.
# Use an official
jdk runtime as a parent image
FROM openjdk:8
# Copy the current
directory contents into the container at /usr/src/apache
COPY .
/usr/src/apache
# Set the working
directory to /usr/src/apache
WORKDIR
/usr/src/apache
# Make port
1884/61616/8161/5672/61613/1883/61614 available to the world outside
this container
EXPOSE 1884
EXPOSE 61616
EXPOSE 8161
EXPOSE 5672
EXPOSE 61613
EXPOSE 1883
EXPOSE 61614
# Start the
activemq service when the container launches
ENTRYPOINT
./apache-activemq-5.15.3/bin/linux-x86-64/activemq start &&
/bin/bash
This Dockerfile
refers to a package of apache-activemq-5.15.3 we haven’t created
yet, namely apache-activemq-5.15.3. Let’s create it next.
2. Copy the
package of activemq to the current directory
Put the package
of activemq in the same folder with the Dockerfile. This completes
our service of activemq, which as you can see is quite simple. When
the above Dockerfile is built into an image, apache-activemq-5.15.3
is present because of that Dockerfile’s COPY command, and the
output from activemq is accessible over HTTP thanks to the EXPOSE
command.
3. Build the app
into a image
a. 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 apache-activemq-5.15.3
b. Now run the
build command. This creates a Docker image, which we’re going to
name using the --tag option. Use -t if you want to use the shorter
option.
$ docker
build --tag=activemq . (dont't forget the dot ".", this
means make it in the current directory)
c. Where is
your built image? It’s in your machine’s local Docker image
registry:
$ docker
image ls
REPOSITORY
TAG IMAGE ID
activemq
latest 326387cea398
Note how the
tag defaulted to latest. The full syntax for the tag option would be
something like --tag=activemq:v0.0.1.
4. Test whether
the image was created successfully
We test whether
the image was created successfully by creating a container.
a. Now let’s
run the app in the background, in detached and interactive mode:
$ docker run
-d -i -p 4000:1884 activemq
-i
--interactive Keep STDIN open even if not attached
-d
--detach Run container in background and print container ID
-p
--publish-all Publish all exposed ports to random ports
b. You get the
long container ID for your app and then are kicked back to your
terminal. Your container is running in the background. You can also
see the abbreviated container ID with docker container ls (and both
work interchangeably when running commands):
$ docker
container ls
CONTAINER
ID IMAGE COMMAND CREATED
STATUS PORTS
NAMES
9979d09fe820 activemq "/bin/sh -c './apach…"
6 seconds ago Up 5 seconds 1883/tcp, 5672/tcp, 8161/tcp,
61613-61614/tcp, 61616/tcp, 0.0.0.0:4000->1884/tcp
vigorous_galileo
c. Now use
docker container stop to end the process, using the CONTAINER ID,
like so:
$ docker
container stop 9979d09fe820
So far, we have
successfully created a image of activemq and successfully run it by a
container. If you want to share your image, you can take the
following steps.
Share your image
To demonstrate the
portability of what we just created, let’s upload our built image
and run it somewhere else. After all, you need to know how to push to
registries when you want to deploy containers to production.
A registry is a
collection of repositories, and a repository is a collection of
images—sort of like a GitHub repository, except the code is already
built. An account on a registry can create many repositories. The
docker CLI uses Docker’s public registry by default.
1. Log in
with your Docker ID
$ docker login
2. Tag the image
$ docker tag
image username/repository:tag
for example: $
docker tag activemq haizeiwang/activemq:v1
-haizeiwang is my
account name
-activemq is my
remote repository name
-v1 is my image
tag,which is the version number
3. Publish the
image
$ docker push
username/repository:tag
Once complete, the
results of this upload are publicly available. If you log in to
Docker Hub, you see the new image there, with its pull command.
4. Pull and run the
image from the remote repository
$ docker run -p
4000:1884 haizeiwang/activemq:v1
If the image
isn’t available locally on the machine, Docker pulls it from the
repository.
No matter where
docker run executes, it pulls your image, along with jdk , and runs
your code. It all travels together in a neat little package, and you
don’t need to install anything on the host machine for Docker to
run it.
三.Build a image of
swieApp with Dockerfile
Repeat the
second step,but omit running and sharing.
1. Edit the
Dockerfile
FROM python:3.7
ENV
PYTHONUNBUFFERED 1
RUN echo python -V
RUN mkdir /app
RUN mkdir /app/db
COPY . /app
WORKDIR
/app/SwieProject
RUN pip3 install -r
../requirements.txt
EXPOSE 8080
ENTRYPOINT python
./wanlidaserver.py
(This
Dockerfile refers to a couple of files we haven’t created yet,
namely wanlidaserver.py and requirements.txt. Let’s create those
next.)
2. Create a file
named requirements.txt
This file provides
all the packages that the project needs that Python does not have.
When executing the "RUN pip3 install -r ../requirements.txt"
command, all the packages in the file will be installed. Put it in
the same folder with the Dockerfile. The file contents are as
follows:
pymysql
DBUtils
sanic
paho-mqtt
pandas
xlwt
qrcode
zplgrf
xlutils
requests
DBUtils
3. Create your
python project
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
Note:If you follow
the steps, the database and activeMQ service will start successfully,
but you will encounter the problem that the web service fails to
start because it cannot connect to the database. This is because you
create a database service without data, you need to import the
database table structure and data that have been created locally into
the Swissmic_WMS database.Do not close the service that has already
been started, and then open another terminal.
1.Export the local
Swissmic_WMS database data to the db.sql file in the current
directory.
$ mysqldump -uroot
-proot Swissmic_WMS >./db.sql
2. Make sure you
are still at the top level of your new directory. Here’s what ls
should show:
$ ls
activeMQ db
docker-compose.yml db.sql
3. View information
about running containers.
$ docker ps
CONTAINER ID
IMAGE COMMAND CREATED
STATUS PORTS
NAMES
2fea493b0104
mysql:5.7 "docker-entrypoint.s…" 4 days
ago Up 2 hours 33060/tcp, 0.0.0.0:4000->3306/tcp
wanlidaapp_db_1
129c12684fc8
haizeiwang/activemq:v1 "/bin/sh -c './apach…" 4 days
ago Up 2 hours 1883/tcp, 5672/tcp, 8161/tcp,
61613-61614/tcp, 61616/tcp, 0.0.0.0:1884->1884/tcp
wanlidaapp_activemq_1
4. Import the
db.sql file into the db container
$ docker cp db.sql
wanlidaapp_db_1:/root/ (Note:"wanlidaapp_db_1" is the
container name,you can also change to the container ID.)
5. Go into the
container that runs the database
$ docker exec -ti
2fea493b0104 bash
6. Switch to the
root directory.
$ cd /root
7. Import the
db.sql file into the Swissmic_WMS database in the container
$ mysql -u root -p
Swissmic_WMS < db.sql
8. Test data is
imported successfully
$ mysql -u root -p
root
>> use
Swissmic_WMS;
>> show
tables;
If you see the
table you want, congratulations, the data import is successful.
Otherwise, it is necessary to check from the first step.
>> exit;
9. Exit the
container
$ exit
b. Stop the
services
$ docker-compose
stop
c. Update and run
again
$ docker-compose up
d. View a running
container on another terminal
$ docker ps
CONTAINER ID
IMAGE COMMAND CREATED
STATUS PORTS
NAMES
9cb77a4c2732
haizeiwang/wanlida_server:v3 "/bin/sh -c 'python …"
About a minute ago Up 17 seconds 0.0.0.0:8080->8080/tcp
wanlidaapp_web_1
2fea493b0104
mysql:5.7 "docker-entrypoint.s…" 4
days ago Up 18 seconds 33060/tcp,
0.0.0.0:4000->3306/tcp
wanlidaapp_db_1
129c12684fc8
haizeiwang/activemq:v1 "/bin/sh -c './apach…" 4
days ago Up 19 seconds 1883/tcp, 5672/tcp, 8161/tcp,
61613-61614/tcp, 61616/tcp, 0.0.0.0:1884->1884/tcp
wanlidaapp_activemq_1
Note: If the above
result is displayed, it means that all the startups have been
successful. You can enter http://localhost:8080/wms in the browser to
test whether the login page appears.
So far, we have
successfully deployed the project service using Method 1. In order to
facilitate the direct use of database image in the future, we will
recreate a database image using the database container that is
running now. Proceed as follows:
1. Stop the
services
$ docker-compose
stop
2. Convert the
database container to a image
$ sudo docker
commit -m "Describe the changed information" -a "author
information" 2fea493b0104 haizeiwang/mysql:v1
3. Upload the image
to your remote repository
$ docker push
haizeiwang/mysql:v1
We will use
Method 2 to redeploy the service.
Method 2: Use the
docker stack deploy command
1. Before we can
use the docker stack deploy command we first edit the
docker-compose.yml file like this:
version: "3"
services:
db:
image:
haizeiwang/mysql:v1
expose:
- "3306"
volumes:
-
./db:/var/lib/mysql
ports:
- "4000:3306"
depends_on:
- activemq
activemq:
image:
haizeiwang/activemq:v1
volumes:
-
./activeMQ:/var/lib/activeMQ
stdin_open: yes
tty: yes
privileged: yes
ports:
- "1884:1884"
web:
image:
haizeiwang/wanlida_server:v1
ports:
- "8080:8080"
links:
- db
- activemq
depends_on:
- db
2. Create a node of
swarm manager, here we use the host as a swarm manager
$ docker swarm init
3. Now let’s
deploy the project. You need to give your app a name. Here, it is set
to SwieApp:
$ docker stack
deploy -c docker-compose.yml SwieApp
Note:If you
follow the steps, the database and activeMQ service will start
successfully, but you will encounter the problem that the web service
fails to start because it cannot connect to the database. This is
because you create a database service without data, you need to
import the database table structure and data that have been created
locally into the Swissmic_WMS database.Do not close the service that
has already been started, and then open another terminal.
1. Go into the
container that runs the database
$ docker exec -ti
container’s ID bash
2. Switch to the
root directory.
$ cd /root
3. Import the
db.sql file into the Swissmic_WMS database in the container
$ mysql -u root -p
Swissmic_WMS < db.sql
4. Test data is
imported successfully
$ mysql -u root -p
root
>> use
Swissmic_WMS;
>> show
tables;
If you see the
table you want, congratulations, the data import is successful.
Otherwise, it is necessary to check from the first step.
>> exit;
5. Exit the
container
$ exit
6. Take down the
app
$ docker stack rm
SwieApp
7. Redeploy run
$ docker stack
deploy -c docker-compose.yml SwieApp
4. Check if three
services are started
$ docker service ls
ID
NAME MODE REPLICAS IMAGE
PORTS
h83om38jtc7v
SwieApp_activemq replicated 1/1
haizeiwang/activemq:v1 *:1884->1884/tcp
ihz1e4c2c4qk
SwieApp_db replicated 1/1
haizeiwang/mysql:v1 *:4000->3306/tcp
juwgtknfg151
SwieApp_web replicated 1/1
haizeiwang/wanlida_server:v3 *:8080->8080/tcp
5. Look for output
for the web service, prepended with your app name.
$ docker service ps
SwieApp_web
ID
NAME IMAGE NODE
DESIRED STATE CURRENT STATE ERROR
PORTS
lt8zpktos7dj
SwieApp_web.1 haizeiwang/wanlida_server:v1 guojihai-TM1604
Running Running about a minute ago
6. You can run
http://localhost:8080/wms in your browser. If successful, you will
see the login page.
The difference
between Method 1 and Method 2:After the server is
restarted, Method 1 needs to restart the service with the command
docker-compose start, but Method 2 will restart automatically.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~end line~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uninstall Docker CE
1.Uninstall the Docker CE package:
$ sudo apt-get purge docker-ce
$ sudo apt-get remove --auto-remove docker
2.Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
$ sudo rm -rf /var/lib/docker
Uninstall Docker Compose
1.To uninstall Docker Compose if you installed using curl:
$ sudo rm /usr/local/bin/docker-compose
2.To uninstall Docker Compose if you installed using pip:
$ pip uninstall docker-compose