Docker實戰

阿輝ya發表於2023-03-02

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;
}

相關文章