docker的安装及项目部署
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
docker的安装及项目部署的更多相关文章
- Linux下tomcat的安装与项目部署
最近在linux下安装了jdk,为了圆我以前的心愿,把tomcat也安装了,顺便部署个项目,也算是小又成就感 废话不说了,直接上过程 一.下载安装对应的jdk,并配置Java环境. 有关jdk的安装请 ...
- 微服务架构 ------ DockerCompose从安装到项目部署
DockerCompose的目的:简化Docker的启动和停止流程,以及编排Docker启动服务与服务之间的关系 DockerCompose的安装:curl -L https://get.daoclo ...
- centos 5.8 x64Jetty的安装以及项目部署配置
链接地址:http://blog.csdn.net/shuixin536/article/details/9049821 安装环境 centos 5.8 x64 安装前须知 首先在安装Jetty之前要 ...
- CentOS7上Docker简单安装及nginx部署
安装 如果原来安装过docker,先把原来的删掉,再安装(如果是首次安装docker忽略第一步,直接在第二步看起) 1.1先查看下已经安装了那些docker yum list installed | ...
- 【Docker】安装tomcat并部署应用
安装tomcat 1.拉取tomcat镜像 docker pull docker.io/tomcat 查看镜像 docker images 2.启动tomcat 首先添加8090端口:firewall ...
- tomcat安装与项目部署
1.tomcat依赖:jdk jre,环境变量配置 java_home jre_home 2.tomcat安装: 下载:http://tomcat.apache.org/ download ...
- CentOS7.5下安装nginx --项目部署
1.安装ngnix一些依赖包 [root@VM_39_157_centos ~]# yum -y install gcc gcc-c++ openssl-devel pcre-devel httpd- ...
- docker中安装nginx,部署前端代码
最近在学习docker,初次接触,难免遇到磕磕碰碰,遂将其整理成博客,以便日后查看. 1.拉取nginx镜像 直接从官方镜像库拉取简单粗暴: docker pull nginx 2.运行 docker ...
- Linux操作系统与项目部署
Linux操作系统与项目部署 注意:本版块会涉及到操作系统相关知识. 现在,几乎所有智能设备都有一个自己的操作系统,比如我们的家用个人电脑,基本都是预装Windows操作系统,我们的手机也有Andro ...
随机推荐
- Dijkstra和Floyd算法
#include #include #include #define Infinity 999 //最大值 #define Max_Vertex_Num 20 //顶点数最多为20 #define L ...
- 细说shiro之一:shiro简介
官网:https://shiro.apache.org/ 一. Shiro是什么Shiro是一个Java平台的开源权限框架,用于认证和访问授权.具体来说,满足对如下元素的支持: 用户,角色,权限(仅仅 ...
- java7 java MethodHandle解析
简介 JDK6之前我们会使用java反射来实现动态方法调用,多数框架用反射的比较多,例如mybatis.spring等.在JDK7中,新增了java.lang.invoke.MethodHandle( ...
- 使用 JS 实现文字上下跑马灯
Ø 前言 今天在做商城首页时,遇到一个上下跑马灯功能,因为之前也只是接触过左右的跑马灯,一时还不知道从何下手.在网上看了几个 demo,并亲自运行了一下,是可以实现的.但是,能运行不知其所以然也不行 ...
- opencv实现坐标旋转(教你框住小姐姐)
一.项目背景 最近在做一个人脸检测项目,需要接入百度AI的系统进行识别和检测.主要流程就是往指定的URL上post图片上去,之后接收检测结果就好了. 百度的检测结果包含这样的信息: left - 人脸 ...
- Python 生成requirement 使用requirements.txt安装类库
快速生成requirement.txt的安装文件 (CenterDesigner) xinghe@xinghe:~/PycharmProjects/CenterDesigner$ pip freeze ...
- python之list+字典练习
d = { '春波':{ "car":{'BMW':5,'BENZ':3,'audi':3,'byd':4}, "house":["北京", ...
- day 10 - 1 函数进阶
函数进阶 命名空间和作用域 命名空间 命名空间 有三种内置命名空间 —— python解释器 就是python解释器一启动就可以使用的名字存储在内置命名空间中 内置的名字在启动解释器的时候被加载进内存 ...
- EL表达式 EL函数 自定义el函数 《黑马程序员_超全面的JavaWeb视频教程vedio》
\JavaWeb视频教程_day12_自定义标签JSTL标签库,java web之设计模式\day12_avi\12.EL入门.avi; EL表达式 1. EL是JSP内置的表达式语言! * jsp2 ...
- response的字节流和字符流输入的区别和问题--02-response的outputStream输出数据的问题.avi
02-response的outputStream输出数据的问题.avi; [字节流] 用meta标签 第一段原始代码:写错: 03-response的Writer输出数据的问题.avi;[字符流输出] ...