Docker实战

Docker实战

Example 1

Dockerfile 构建 Tomcat

opt 下 创建一个 docker 目录

[root@localhost /]# sudo mkdir -p /opt/docker/

cd 进入 docker 目录

[root@localhost /]# sudo cd /opt/docker/

wget 下载 tomcat

[root@localhost docker]# sudo wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.tar.gz

wget 下载 jdk,可能会下载失败。百度网盘链接: https://pan.baidu.com/s/1TQZLOQC7G7-960KIgg95Ig 提取码: 3hdr

[root@localhost docker]# wget https://download.oracle.com/otn/java/jdk/8u301-b09/d3c52aa6bfa54d3ca74e617f18309292/jdk-8u301-linux-x64.tar.gz?AuthParam=1627967851_5942d2bdcb3e02f21c7285b7e3a04981

查询刚才下载的两个文件是否存在

[root@localhost docker]# ls
apache-tomcat-9.0.50.tar.gz  jdk-8u301-linux-i586.tar.gz

编译一个 host.txt 文件

[root@localhost docker]# vim host.txt
[root@localhost docker]# cat host.txt 
我是宿主机器

编写 Dockerfile 注意 Dockerfile f 不要大写

[root@localhost docker]# vim Dockerfile

Dockerfile 文件内容

FROM centos
MAINTAINER fmm<fmm@sina.com>
# 把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下
COPY host.txt /usr/local/target.txt
ADD jdk-8u301-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.50.tar.gz /usr/local/
# 安装vim编辑器
RUN yum install -y vim
# 设置工作访问时候的WORKDIR 路径,登录落脚点
ENV MYPATH /usr/local/
WORKDIR $MYPATH
# 配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_301
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.50
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# 容器运行时监听的端口
EXPOSE 8080
# 启动时运行tomcat
# ENTRYPOINT ["/usr/local/apache-tomcat-9.0.50/bin/startup.sh"]
# CMD ["/usr/local/apache-tomcat-9.0.50/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/logs/catalina.out

打包镜像,注意分号后面是镜像版本号,0后面有一个空格,然后再是一个点。

[root@localhost docker]# docker build -t ming-tomcat:1.0 .
Sending build context to Docker daemon    157MB
Step 1/14 : FROM centos
 ---> 300e315adb2f
Step 2/14 : MAINTAINER fmm<fmm@sina.com>
 ---> Using cache
 ---> 51be552ff17d
Step 3/14 : COPY host.txt /usr/local/target.txt
 ---> Using cache
 ---> bbdd694ce4db
Step 4/14 : ADD jdk-8u301-linux-x64.tar.gz /usr/local/
 ---> 64428e0e4060
Step 5/14 : ADD apache-tomcat-9.0.50.tar.gz /usr/local/
 ---> de3fe793dce4
Step 6/14 : RUN yum install -y vim
 ---> Running in 01627db322e6
CentOS Linux 8 - AppStream                      5.3 MB/s | 8.3 MB     00:01    
CentOS Linux 8 - BaseOS                         2.4 MB/s | 4.5 MB     00:01    
CentOS Linux 8 - Extras                          15 kB/s |  10 kB     00:00    
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-15.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-15.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-15.el8         appstream       48 k
 which               x86_64      2.21-12.el8               baseos          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm        531 kB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 875 kB/s |  48 kB     00:00    
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm  4.2 MB/s | 1.4 MB     00:00    
(4/5): which-2.21-12.el8.x86_64.rpm             202 kB/s |  49 kB     00:00    
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm    7.6 MB/s | 6.3 MB     00:00    
--------------------------------------------------------------------------------
Total                                           3.5 MB/s | 7.8 MB     00:02     
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                      1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : which-2.21-12.el8.x86_64                               1/5 
  Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                2/5 
  Installing       : vim-common-2:8.0.1763-15.el8.x86_64                    3/5 
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5 
  Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5 
  Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                    5/5 
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5 
  Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                    2/5 
  Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                  3/5 
  Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                4/5 
  Verifying        : which-2.21-12.el8.x86_64                               5/5 

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-15.el8.x86_64    
  vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
  which-2.21-12.el8.x86_64             

Complete!
Removing intermediate container 01627db322e6
 ---> 66ebeda5daa9
Step 7/14 : ENV MYPATH /usr/local/
 ---> Running in b46cc82a894d
Removing intermediate container b46cc82a894d
 ---> 3690069781ba
Step 8/14 : WORKDIR $MYPATH
 ---> Running in e2fcac5645f1
Removing intermediate container e2fcac5645f1
 ---> 968389dd44cc
Step 9/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_301
 ---> Running in f027615fe14a
Removing intermediate container f027615fe14a
 ---> 8d7a87ba210e
Step 10/14 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in b1ee49a4043a
Removing intermediate container b1ee49a4043a
 ---> 6085c831a279
Step 11/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.50
 ---> Running in 5b7da759cd99
Removing intermediate container 5b7da759cd99
 ---> 8a298f4438a6
Step 12/14 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in b563fefb4a38
Removing intermediate container b563fefb4a38
 ---> eee5e37b2b3c
Step 13/14 : EXPOSE 8080
 ---> Running in 60660dbfd059
Removing intermediate container 60660dbfd059
 ---> 4841ecf2a2d5
Step 14/14 : CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/logs/catalina.out
 ---> Running in 8d29ccef591f
Removing intermediate container 8d29ccef591f
 ---> 17cada45b5ab
Successfully built 17cada45b5ab
Successfully tagged ming-tomcat:1.0

查看镜像

[root@localhost docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
ming-tomcat   1.0       17cada45b5ab   About a minute ago   652MB
centos        latest    300e315adb2f   7 months ago         209MB

共享数据卷 启动容器; ctrl+q+p后台运行 ;ctrl+c 退出

[root@localhost docker]# docker run -it  -p 9080:8080 --name t9 \
	-v /opt/docker/tomcat/test:/usr/local/apache-tomcat-9.0.39/webapps/test \
	-v /opt/docker/tomcat/logs:/usr/local/apache-tomcat-9.0.39/logs \
	--privileged=true ming-tomcat:1.0

查询 docker 进程

[root@localhost docker]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS         PORTS                                       NAMES
10aab89e9538   ming-tomcat:1.0   "/bin/sh -c '/usr/lo…"   2 minutes ago   Up 2 minutes   0.0.0.0:9080->8080/tcp, :::9080->8080/tcp   t9

测试是否启动成功, 如下成功。也可以在浏览器中访问 http://192.168.101.59:9080

[root@localhost docker]# curl http://127.0.0.1:9080
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/9.0.50</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>

通过 exec 命令进入容器内,可以使用容器名称,也可以使用容器 ID 进入

[root@localhost docker]# docker exec -it t9 /bin/sh

查找宿主机上拷贝进来的 target.txt 文件石否存在

sh-4.4# cd /usr/local/  
sh-4.4# ls
apache-tomcat-9.0.39  apache-tomcat-9.0.50  bin  etc  games  include  jdk1.8.0_301  lib  lib64  libexec  sbin  share  src  target.txt
sh-4.4# cat target.txt 
我是宿主机器

Example 2

Nginx +2个 Tomcat 负载

第一部分 Tomcat 镜像内容

拉取 tomcat 镜像

[root@localhost docker]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
627b765e08d1: Pull complete 
c040670e5e55: Pull complete 
073a180f4992: Pull complete 
bf76209566d0: Pull complete 
f10db7ba7580: Pull complete 
5b2f970878fa: Pull complete 
ed434bfebf18: Pull complete 
f6c437110aa9: Pull complete 
a772951f83db: Pull complete 
752225c3768e: Pull complete 
Digest: sha256:6e40250d8fac4eca05c2067cb81f79427e4ddbaf4e78d5ecd21c35e8c5f2bfcf
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest

启动 tomcat

[root@localhost docker]# docker run -id --name t9 -p9001:8080 tomcat

地址栏访问 http://192.168.101.59:9001/ ip 修改为你自己的 ip

image-20210803144947156

页面显示 404 而不是 tomcat 官网,因为镜像文件中 webapps 文件是空,使用如下命令进去查看一下

docker exec -it t9 /bin/sh

[root@localhost docker]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                       NAMES
6e5d4591d451   tomcat    "catalina.sh run"   4 seconds ago   Up 2 seconds   0.0.0.0:9001->8080/tcp, :::9001->8080/tcp   t9
[root@localhost docker]# docker exec -it t9 /bin/sh
# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
# cd webapps
# ls
# pwd
/usr/local/tomcat/webapps

如果想显示官网也很简单,其实 webapps 下载的官方文档是 webapps.dist 我们 copy 到 webapps 下即可

拷贝完成后 ctrl+q+p后台运行

cp -r ../webapps.dist/* ../webapps
# 1、或者删除 rm -rf webapps 
# 2、修改webapps.dist mv  webapps.dist webapps 
# 3、同样的效果

重启 tomcat 容器

[root@localhost docker]# docker restart t9

image-20210803150940764

第二部分 挂载两个Tomcat

创建两个挂载文件的目录

[root@localhost docker]# mkdir -p /opt/docker/tomcat1/
[root@localhost docker]# mkdir -p /opt/docker/tomcat2/

在容器和本地文件系统之间复制文件/文件夹; docker cp 容器名称或容器ID 容器路径 宿主机路径

[root@localhost docker]# docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

从刚才 tomcat 容器中 复制两份配置文件

[root@localhost docker]# docker cp t9:/usr/local/tomcat/conf /opt/docker/tomcat1/conf
[root@localhost docker]# docker cp t9:/usr/local/tomcat/webapps  /opt/docker/tomcat1/webapps

[root@localhost docker]# docker cp t9:/usr/local/tomcat/conf /opt/docker/tomcat2/conf
[root@localhost docker]# docker cp t9:/usr/local/tomcat/webapps  /opt/docker/tomcat2/webapps

编辑 tomcat1 tomcat2 port分别为 80018002

[root@localhost docker]# vim /opt/docker/tomcat1/conf/server.xml
[root@localhost docker]# vim /opt/docker/tomcat2/conf/server.xml

启动 tomcat1 挂载文件路径 -v

[root@localhost docker]# docker run -id  -p 8001:8001 --name t1 \
                        -v /opt/docker/tomcat1/conf:/usr/local/tomcat/conf \
                        -v /opt/docker/tomcat1/webapps:/usr/local/tomcat/webapps tomcat

启动 tomcat2挂载文件路径 -v

[root@localhost docker]# docker run -id  -p 8002:8002 --name t2 \
                        -v /opt/docker/tomcat2/conf:/usr/local/tomcat/conf \
                        -v /opt/docker/tomcat2/webapps:/usr/local/tomcat/webapps tomcat

启动过程如果报错 docker: Error response from daemon: Conflict. The container name "/t1" is already in use by container

说明容器名字重复,容器名称不可重复

1、删除换个名字

2、删除这个名字

[root@localhost docker]# docker rm t1

3、删除所有容器 ,删除容器不会影响镜像,所以不用担心重新创建容器即可。

[root@localhost docker]# docker rm $(docker ps -qa)

查询容器进程,如下所示证明运行成功

[root@localhost docker]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                                 NAMES
02b94c15fffc   tomcat    "catalina.sh run"   5 seconds ago    Up 3 seconds    0.0.0.0:8002->8002/tcp, :::8002->8002/tcp, 8080/tcp   t2
6c6236bb4259   tomcat    "catalina.sh run"   18 seconds ago   Up 16 seconds   0.0.0.0:8001->8001/tcp, :::8001->8001/tcp, 8080/tcp   t1

第三部分 拉取 Nginx 镜像

nginx

[root@localhost docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
33847f680f63: Pull complete 
dbb907d5159d: Pull complete 
8a268f30c42a: Pull complete 
b10cf527a02d: Pull complete 
c90b090c213b: Pull complete 
1f41b2f2bf94: Pull complete 
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

创建 nginx 容器 挂载配置文件

[root@localhost /]# docker run -it  -p 80:80 --name ng nginx

地址栏访问 http://192.168.101.59/ 如下启动成功

image-20210803160339701

拷贝 nginx 文件

[root@localhost docker]# docker cp ng:/etc/nginx/ /opt/docker/
配置负载均衡

修改配置文件

[root@localhost docker]# vim /opt/docker/nginx/conf.d/default.conf

配置文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
	}
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

http 内增加

这里的 172 ip 地址是容器内的 ip 地址 端口也是容器内的端口

通过 docker inspect 查看容器细节

其中 IPAddress 就是容器的 ip 地址

"SandboxKey": "/var/run/docker/netns/e519d853b305",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7ff8a9ce52473837df2065019c31082281b418c5ff662c7619343cdfb1c8aec3",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
upstream www.ming.com{
    server 172.17.0.2:8001 weight=5;
    server 172.17.0.3:8001 weight=10;
}

修改 server location

location / {
    	proxy_pass    http://www.ming.com;
        #root   /usr/share/nginx/html;
        index  index.html index.htm;
}

最终版本

 upstream www.ming.com{
    server 172.17.0.2:8001 weight=5;
    server 172.17.0.3:8002 weight=10;
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    	proxy_pass http://www.ming.com;
        #root   /usr/share/nginx/html;
        index  index.html index.htm;
	}
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

停止nignx

[root@localhost nginx]# docker stop ng

删除容器

[root@localhost nginx]# docker rm ng

重新创建容器,并挂载配置文件

[root@localhost nginx]# docker run -it -v /opt/docker/nginx/:/etc/nginx  -p 80:80 --name ng nginx
测试负载均衡

访问宿主机 80 端口 http://192.168.101.59/

1、这里为了区分访问到是哪个 tomcat 我修改了每个 webapp/ROOT/index.jsp

2、H1 标签修改为对应的 tomcat 端口

3、页面多刷新几次就可以看出效果

<div id="asf-box">
	<h1>Tomcat 8002</h1>
</div>

image-20210803162654760

image-20210803162642832

Example 3

安装 mysql

我这里使用 mysql 5.7

[root@localhost /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
33847f680f63: Already exists 
5cb67864e624: Pull complete 
1a2b594783f5: Pull complete 
b30e406dd925: Pull complete 
48901e306e4c: Pull complete 
603d2b7147fd: Pull complete 
802aa684c1c4: Pull complete 
5b5a19178915: Pull complete 
f9ce7411c6e4: Pull complete 
f51f6977d9b2: Pull complete 
aeb6b16ce012: Pull complete 
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

创建mysql 需要挂载的几个目录

[root@localhost /]# mkdir -p /opt/docker/mysql/conf
[root@localhost /]# mkdir -p /opt/docker/mysql/logs
[root@localhost /]# mkdir -p /opt/docker/mysql/data

创建容器并挂载文件

[root@localhost /]# docker run -d -p 6894:3306 --name mysql \
                    -v /opt/docker/mysql/conf:/etc/mysql/ \
                    -v /opt/docker/mysql/logs:/logs \
                    -v /opt/docker/mysql/data:/var/lib/mysql \
                    -e MYSQL_ROOT_PASSWORD=qtykGhC29eP4Smp mysql:5.7

查看是否启动成

[root@localhost /]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
3ae93d1f929c   mysql:5.7   "docker-entrypoint.s…"   4 seconds ago   Up 2 seconds   33060/tcp, 0.0.0.0:6894->3306/tcp, :::6894->3306/tcp   mysql

使用mysql 可视化工具连接测试

image-20210803164731447

image-20210803164752623

数据库备份

[root@localhost /]# docker exec -it mysql mysqldump -uroot -pqtykGhC29eP4Smp db_docker > /opt/db_docker.sql

其他

根据自己的业务需求举一反三

nginx负载

ip_hash ip地址分配

upstream www.ming.com{
  			ip_hash;
       server 172.17.0.2:8081 ;
       server 172.17.0.3:8082 ;
}

upstream 权重配置

是配置nginx与后端服务器负责均衡非常重要的一个模块,并且它还能对后端的服务器的健康状态进行检查,若后端服务器中的一台发生故障,则前端请求不会转发到故障的机器;weight是权重配置, 权重越高 分配到的概率越高

upstream www.ming.com{
       server 172.17.0.2:8081 weight=5; 
       server 172.17.0.3:8082 weight=10;
}

backup 备份配置

backup 不参与负载均衡 ,其他机器挂掉才会立即启动

upstream www.ming.com{
       server 172.17.0.2:8081 ;
       server 172.17.0.3:8082 backup;
}

down 宕机配置

down 不参与负载均衡 , 维护机器

upstream www.ming.com{
       server 172.17.0.2:8081 ;
       server 172.17.0.3:8082 down;
}
posted @ 2021-08-03 17:03  天葬  阅读(774)  评论(0编辑  收藏  举报