首页 Docker教程docker-容器编排管理工具docker compse应用

docker-容器编排管理工具docker compse基础

Docker compse实战案例-实现单机版的Haproxy+Nginx+Tomcat

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

从 docker compose 启动单个容器

注意:使用Docker compose之前,先要安装docker

创建 docker compose文件

docker compose 文件可在任意目录,创建文件名为docker-compose.yml 配置文件,要注意前后的缩进

[root@ubuntu1804 ~]#docker-compose --version
docker-compose version 1.25.4, build unknown
[root@ubuntu1804 ~]#mkdir /data/docker-compose
[root@ubuntu1804 ~]#cd /data/docker-compose
[root@ubuntu1804 docker-compose]#vim  docker-compose.yml
[root@ubuntu1804 docker-compose]#cat docker-compose.yml
service-nginx-web:
  image: docker pull 10.0.0.102/example/nginx-centos7-base:1.6.1
  container_name: nginx-web
  expose:
   - 80 
   - 443
  ports:
    - "80:80"
    - "443:443"
查看配置和格式检查
[root@ubuntu1804 docker-compose]#docker-compose  config 
services:
  service-nginx-web:
    container_name: nginx-web
    expose:
    - 80
    - 443
    image: 10.0.0.102/example/nginx-centos7-base:1.6.1
    network_mode: bridge
    ports:
    - 80:80/tcp
    - 443:443/tcp
version: '2.1'

[root@ubuntu1804 docker-compose]#docker-compose  config -q

#改错ocker-compose文件格式
[root@ubuntu1804 docker-compose]#vim docker-compose.yml
service-nginx-web   #改此行,最后的”:"删除                           
  image: 10.0.0.102/example/nginx-centos7-base:1.6.1
  container_name: nginx-web
  expose:
   - 80
   - 443
  ports:
    - "80:80"
    - "443:443"

[root@ubuntu1804 docker-compose]#docker-compose  config -q
ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
  in "./docker-compose.yml", line 2, column 8
[root@ubuntu1804 docker-compose]#
启动容器

注意:必须要在docker compose文件所在的目录执行:

#前台启动
[root@ubuntu1804 docker-compose]#docker-compose up
Pulling service-nginx-web (10.0.0.102/example/nginx-centos7-base:1.6.1)...
ERROR: Get https://10.0.0.102/v2/: dial tcp 10.0.0.102:443: connect: connection refused
[root@ubuntu1804 docker-compose]#vim /lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 10.0.0.102 
[root@ubuntu1804 docker-compose]#systemctl daemon-reload 
[root@ubuntu1804 docker-compose]#systemctl restart docker
[root@ubuntu1804 docker-compose]#docker-compose up
Pulling service-nginx-web (10.0.0.102/example/nginx-centos7-base:1.6.1)...
1.6.1: Pulling from example/nginx-centos7-base
f34b00c7da20: Pull complete
544476d462f7: Pull complete
39345915aa1b: Pull complete
d5376f2bbd9e: Pull complete
4596aecee927: Pull complete
1617b995c379: Pull complete
d00df95be654: Pull complete
Digest: sha256:82e9e7d8bf65e160ba79a92bb25ae42cbbf791092d1e09fb7de25f91b31a21ff
Status: Downloaded newer image for 10.0.0.102/example/nginx-centos7-base:1.6.1
Creating nginx-web ... done
Attaching to nginx-web

#以上是前台执行不退出
验证docker compose执行结果
#上面命令是前台执行,所以要查看结果,可以再开一个终端窗口进行观察
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE                                         COMMAND                  CREATED             STATUS              PORTS                                      NAMES
d71030504f6a        10.0.0.102/example/nginx-centos7-base:1.6.1   "/apps/nginx/sbin/ng…"   15 seconds ago      Up 13 seconds       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   nginx-web
[root@ubuntu1804 ~]#docker-compose ps 
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml

[root@ubuntu1804 ~]#cd /data/docker-compose/
[root@ubuntu1804 docker-compose]#docker-compose ps 
  Name             Command           State                    Ports                  
-------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

[root@ubuntu1804 docker-compose]#curl 127.0.0.1/app/
Test Page in app
[root@ubuntu1804 docker-compose]#docker-compose images
Container                Repository                  Tag      Image Id       Size  
-----------------------------------------------------------------------------------
nginx-web   10.0.0.102/example/nginx-centos7-base   1.6.1   ea3840c349e5   413.4 MB

[root@ubuntu1804 docker-compose]#docker-compose exec service-nginx-web bash
[root@17c17ad30193 /]# 
[root@17c17ad30193 /]# 
[root@17c17ad30193 /]# 
[root@17c17ad30193 /]# tail -f /apps/nginx/logs/access.log 
172.17.0.1 - - [04/Feb/2020:16:01:42 +0800] "GET /app/ HTTP/1.1" 200 17 "-" "curl/7.58.0"
10.0.0.101 - - [04/Feb/2020:16:06:29 +0800] "GET /app/ HTTP/1.1" 200 17 "-" "curl/7.58.0"
10.0.0.102 - - [04/Feb/2020:16:08:22 +0800] "GET /app/ HTTP/1.1" 200 17 "-" "curl/7.58.0"
结束前台执行
[root@ubuntu1804 docker-compose]#docker-compose up
Pulling service-nginx-web (10.0.0.102/example/nginx-centos7-base:1.6.1)...
1.6.1: Pulling from example/nginx-centos7-base
f34b00c7da20: Pull complete
544476d462f7: Pull complete
39345915aa1b: Pull complete
d5376f2bbd9e: Pull complete
4596aecee927: Pull complete
1617b995c379: Pull complete
d00df95be654: Pull complete
Digest: sha256:82e9e7d8bf65e160ba79a92bb25ae42cbbf791092d1e09fb7de25f91b31a21ff
Status: Downloaded newer image for 10.0.0.102/example/nginx-centos7-base:1.6.1
Creating nginx-web ... done
Attaching to nginx-web
^CGracefully stopping... (press Ctrl+C again to force)  #ctrl+c键,结束容器
Stopping nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose ps 
  Name             Command           State    Ports
---------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Exit 0        
[root@ubuntu1804 docker-compose]#docker ps -a
CONTAINER ID        IMAGE                                         COMMAND                  CREATED             STATUS                          PORTS               NAMES
d71030504f6a        10.0.0.102/example/nginx-centos7-base:1.6.1   "/apps/nginx/sbin/ng…"   5 minutes ago       Exited (0) About a minute ago                       nginx-web

[root@ubuntu1804 docker-compose]#docker-compose start
Starting service-nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose ps 
  Name             Command           State                    Ports                  
-------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

#关闭容器
[root@ubuntu1804 docker-compose]#docker-compose kill
Killing nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose ps 
  Name             Command            State     Ports
-----------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Exit 137
删除容器
[root@ubuntu1804 docker-compose]#docker-compose ps
  Name             Command           State    Ports
---------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Exit 0     

#只删除停止的容器
[root@ubuntu1804 docker-compose]#docker-compose rm
Going to remove nginx-web
Are you sure? [yN] y
Removing nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose up -d
Creating nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose rm
No stopped containers

#停止并删除容器
[root@ubuntu1804 docker-compose]#docker-compose down
Stopping nginx-web ... done
Removing nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose ps 
Name   Command   State   Ports
------------------------------
[root@ubuntu1804 docker-compose]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
#也会自动删除镜像
[root@ubuntu1804 docker-compose]#docker-compose  images
Container   Repository   Tag   Image Id   Size
----------------------------------------------
后台执行
[root@ubuntu1804 docker-compose]#docker-compose  up -d
Creating nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
  Name             Command           State                    Ports                  
-------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
[root@ubuntu1804 docker-compose]#curl 127.0.0.1/app/
Test Page in app
[root@ubuntu1804 docker-compose]#curl http://127.0.0.1/app/
Test Page in app
停止和启动与日志查看
[root@ubuntu1804 docker-compose]#docker-compose stop
Stopping nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
  Name             Command           State    Ports
---------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Exit 0 
[root@ubuntu1804 docker-compose]#docker-compose start 
Starting service-nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
  Name             Command           State                    Ports                  
-------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

[root@ubuntu1804 docker-compose]#docker-compose  restart
Restarting nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
  Name             Command           State                    Ports                  
-------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

#执行上面操作时,可以同时开一个终端,观察日事件
[root@ubuntu1804 docker-compose]#docker-compose events
2020-02-04 15:38:13.253822 container kill 5d92e4da8679a973145e5b4db364ae8cf8596a03c4fd0b3b6a28213a2f155be6 (image=10.0.0.102/example/nginx-centos7-base:1.6.1, name=nginx-web)
2020-02-04 15:38:13.531208 container die 5d92e4da8679a973145e5b4db364ae8cf8596a03c4fd0b3b6a28213a2f155be6 (image=10.0.0.102/example/nginx-centos7-base:1.6.1, name=nginx-web)
2020-02-04 15:38:13.631137 container stop 5d92e4da8679a973145e5b4db364ae8cf8596a03c4fd0b3b6a28213a2f155be6 (image=10.0.0.102/example/nginx-centos7-base:1.6.1, name=nginx-web)
2020-02-04 15:38:15.137495 container start 5d92e4da8679a973145e5b4db364ae8cf8596a03c4fd0b3b6a28213a2f155be6 (image=10.0.0.102/example/nginx-centos7-base:1.6.1, name=nginx-web)
2020-02-04 15:38:15.137546 container restart 5d92e4da8679a973145e5b4db364ae8cf8596a03c4fd0b3b6a28213a2f155be6 (image=10.0.0.102/example/nginx-centos7-base:1.6.1, name=nginx-web)

#以json格式显示日志
[root@ubuntu1804 docker-compose]#docker-compose events --json
{"time": "2020-02-04T15:48:22.423539", "type": "container", "action": "kill", "id": "19d72e9bc85842d8879d7dcf2a3d2defd79a5a0c3c3d974ddfbbbc6e95bf910b", "service": "service-nginx-web", "attributes": {"name": "nginx-web", "image": "10.0.0.102/example/nginx-centos7-base:1.6.1"}}
{"time": "2020-02-04T15:48:22.537200", "type": "container", "action": "exec_die", "id": "19d72e9bc85842d8879d7dcf2a3d2defd79a5a0c3c3d974ddfbbbc6e95bf910b", "service": "service-nginx-web", "attributes": {"name": "nginx-web", "image": "10.0.0.102/example/nginx-centos7-base:1.6.1"}}
{"time": "2020-02-04T15:48:22.745670", "type": "container", "action": "die", "id": "19d72e9bc85842d8879d7dcf2a3d2defd79a5a0c3c3d974ddfbbbc6e95bf910b", "service": "service-nginx-web", "attributes": {"name": "nginx-web", "image": "10.0.0.102/example/nginx-centos7-base:1.6.1"}}
{"time": "2020-02-04T15:48:22.863375", "type": "container", "action": "stop", "id": "19d72e9bc85842d8879d7dcf2a3d2defd79a5a0c3c3d974ddfbbbc6e95bf910b", "service": "service-nginx-web", "attributes": {"name": "nginx-web", "image": "10.0.0.102/example/nginx-centos7-base:1.6.1"}}
{"time": "2020-02-04T15:48:23.979421", "type": "container", "action": "start", "id": "19d72e9bc85842d8879d7dcf2a3d2defd79a5a0c3c3d974ddfbbbc6e95bf910b", "service": "service-nginx-web", "attributes": {"name": "nginx-web", "image": "10.0.0.102/example/nginx-centos7-base:1.6.1"}}
{"time": "2020-02-04T15:48:23.979468", "type": "container", "action": "restart", "id": "19d72e9bc85842d8879d7dcf2a3d2defd79a5a0c3c3d974ddfbbbc6e95bf910b", "service": "service-nginx-web", "attributes": {"name": "nginx-web", "image": "10.0.0.102/example/nginx-centos7-base:1.6.1"}}
暂停和恢复
[root@ubuntu1804 docker-compose]#docker-compose  pause
Pausing nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
  Name             Command           State                     Ports                  
--------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Paused   0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
[root@ubuntu1804 docker-compose]#curl -m 1  http://127.0.0.1/app/
curl: (28) Operation timed out after 1002 milliseconds with 0 bytes received
[root@ubuntu1804 docker-compose]#docker-compose  unpause
Unpausing nginx-web ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
  Name             Command           State                    Ports                  
-------------------------------------------------------------------------------------
nginx-web   /apps/nginx/sbin/nginx   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
[root@ubuntu1804 docker-compose]#curl -m 1  http://127.0.0.1/app/
Test Page in app
指定同时启动容器的数量
[root@ubuntu1804 docker-compose]#vim docker-compose.yml 
[root@ubuntu1804 docker-compose]#cat docker-compose.yml
service-nginx-web:
  image: 10.0.0.102/example/nginx-centos7-base:1.6.1
#  container_name: nginx-web #同时启动多个同一镜像的容器,不要指定容器名称,否则会冲突
  expose:
   - 80 
   - 443
#  ports:                    #同时启动多个同一镜像的容器,不要指定端口号,否则会冲突
#    - "80:80"
#    - "443:443"

#再加一个service
service-tomcat:
  image: 10.0.0.102/example/tomcat-base:v8.5.50

[root@ubuntu1804 docker-compose]#docker-compose  ps 
Name   Command   State   Ports
------------------------------
[root@ubuntu1804 docker-compose]#docker-compose  up -d --scale service-nginx-web=2
Creating docker-compose_service-tomcat_1    ... done
Creating docker-compose_service-nginx-web_1 ... done
Creating docker-compose_service-nginx-web_2 ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
               Name                         Command           State         Ports     
--------------------------------------------------------------------------------------
docker-compose_service-nginx-web_1   /apps/nginx/sbin/nginx   Up       443/tcp, 80/tcp
docker-compose_service-nginx-web_2   /apps/nginx/sbin/nginx   Up       443/tcp, 80/tcp
docker-compose_service-tomcat_1      /bin/bash                Exit 0                  
[root@ubuntu1804 docker-compose]#docker-compose  up -d --scale service-nginx-web=3 --scale service-tomcat=2
Starting docker-compose_service-tomcat_1    ... done
Starting docker-compose_service-nginx-web_1 ... done
Starting docker-compose_service-nginx-web_2 ... done
Creating docker-compose_service-nginx-web_3 ... done
Creating docker-compose_service-tomcat_2    ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
               Name                         Command           State         Ports     
--------------------------------------------------------------------------------------
docker-compose_service-nginx-web_1   /apps/nginx/sbin/nginx   Up       443/tcp, 80/tcp
docker-compose_service-nginx-web_2   /apps/nginx/sbin/nginx   Up       443/tcp, 80/tcp
docker-compose_service-nginx-web_3   /apps/nginx/sbin/nginx   Up       443/tcp, 80/tcp
docker-compose_service-tomcat_1      /bin/bash                Exit 0                  
docker-compose_service-tomcat_2      /bin/bash                Exit 0                  
[root@ubuntu1804 docker-compose]#docker-compose  up -d 
Stopping and removing docker-compose_service-nginx-web_2 ... done
Stopping and removing docker-compose_service-nginx-web_3 ... done
Stopping and removing docker-compose_service-tomcat_2    ... done
Starting docker-compose_service-tomcat_1                 ... done
Starting docker-compose_service-nginx-web_1              ... done
[root@ubuntu1804 docker-compose]#docker-compose  ps 
               Name                         Command           State         Ports     
--------------------------------------------------------------------------------------
docker-compose_service-nginx-web_1   /apps/nginx/sbin/nginx   Up       443/tcp, 80/tcp
docker-compose_service-tomcat_1      /bin/bash                Exit 0 

从docker compose启动多个容器

编辑docker-compose文件并使用数据卷

注意:同一个文件 ,数据卷的优先级比镜像内的文件优先级高

[root@ubuntu1804 docker-compose]#vim docker-compose.yml 
[root@ubuntu1804 docker-compose]#cat  docker-compose.yml 
service-nginx-web:
  image: 10.0.0.102/example/nginx-centos7-base:1.6.1
  container_name: nginx-web 
  volumes:
    - /data/nginx:/apps/nginx/html/#指定数据卷,将宿主机/data/nginx挂载到容器/apps/nginx/html
  expose:
   - 80 
   - 443
  ports:
    - "80:80"
    - "443:443"

service-tomcat-app1:
  image: 10.0.0.102/example/tomcat-web:app1
  container_name: tomcat-app1
  expose:
    - 8080
  ports:
    - "8081:8080"

service-tomcat-app2:
  image: 10.0.0.102/example/tomcat-web:app2
  container_name: tomcat-app2
  expose:
    - 8080
  ports:
    - "8082:8080"

#在宿主机准备nginx测试页面文件
[root@ubuntu1804 docker-compose]#mkdir /data/nginx
[root@ubuntu1804 docker-compose]#echo Docker compose test page > /data/nginx/index.html
启动容器并验证结果
[root@ubuntu1804 docker-compose]#docker-compose up -d
Pulling service-tomcat-app1 (10.0.0.102/example/tomcat-web:app1)...
app1: Pulling from example/tomcat-web
f34b00c7da20: Already exists
544476d462f7: Already exists
39345915aa1b: Already exists
4b792f2bae38: Already exists
4439447a3522: Already exists
fe34d2ec1dd0: Already exists
b8487ca03126: Already exists
5a475b7d8b1a: Already exists
df8703d3d2dd: Already exists
f0da1ffa7aa7: Pull complete
80fd4c70e670: Pull complete
c2a0247d7bfa: Pull complete
b0977ed809cd: Pull complete
Digest: sha256:e0aba904df6095ea04c594d6906101f8e5f4a6ceb0a8f9b24432c47698d0caa8
Status: Downloaded newer image for 10.0.0.102/example/tomcat-web:app1
Pulling service-tomcat-app2 (10.0.0.102/example/tomcat-web:app2)...
app2: Pulling from example/tomcat-web
f34b00c7da20: Already exists
544476d462f7: Already exists
39345915aa1b: Already exists
4b792f2bae38: Already exists
4439447a3522: Already exists
fe34d2ec1dd0: Already exists
b8487ca03126: Already exists
5a475b7d8b1a: Already exists
df8703d3d2dd: Already exists
f0da1ffa7aa7: Already exists
80fd4c70e670: Already exists
1a55cb76a801: Pull complete
565ab795f82a: Pull complete
Digest: sha256:c4d6f166c3933f6c1ba59c84ea0518ed653af25f28b87981c242b0deff4209bb
Status: Downloaded newer image for 10.0.0.102/example/tomcat-web:app2
Creating tomcat-app1 ... done
Creating tomcat-app2 ... done
Creating nginx-web   ... done

[root@ubuntu1804 docker-compose]#docker-compose  ps 
   Name                  Command               State                    Ports                  
-----------------------------------------------------------------------------------------------
nginx-web     /apps/nginx/sbin/nginx           Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
tomcat-app1   /apps/tomcat/bin/run_tomcat.sh   Up      8009/tcp, 0.0.0.0:8081->8080/tcp        
tomcat-app2   /apps/tomcat/bin/run_tomcat.sh   Up      8009/tcp, 0.0.0.0:8082->8080/tcp        
[root@ubuntu1804 docker-compose]#curl http://127.0.0.1/
Docker compose test page
[root@ubuntu1804 docker-compose]#curl http://127.0.0.1:8081/app/
Tomcat Page in app1
[root@ubuntu1804 docker-compose]#curl http://127.0.0.1:8082/app/
Tomcat Page in app2

本文链接:http://www.yunweipai.com/34974.html

docker-容器编排管理工具docker compse基础

Docker compse实战案例-实现单机版的Haproxy+Nginx+Tomcat

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部