—docker-compose简介
docker-compose是Docker公司官方开源项目,主要对单机容器的快速编排,docker-compose将所管理的容器分为三层:
- project 默认为当前目录
- service 通过service名称管理容器
- container 容器
—docker-compose版本
- docker-compose v1.xx 版本为python写的版本
- docker-compose v2.xx 版本为go写的版本
—docker-compose的安装
docker-compose安装包可在github上下载
root@ubuntu-3:~# mkdir -p docker-compose
root@ubuntu-3:~# cd docker-compose
root@ubuntu-3:~/docker-compose# wget https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64
root@ubuntu-3:~/docker-compose# chmod a+x docker-compose-linux-x86_64
root@ubuntu-3:~/docker-compose# cp docker-compose-linux-x86_64 /usr/bin/docker-compose
root@ubuntu-3:~/docker-compose# docker-compose --version
Docker Compose version v2.12.2
–help 查看docker-compose的帮助文档
root@ubuntu-3:~/docker-compose# docker-compose --help
Usage: docker compose [OPTIONS] COMMAND
Docker Compose
Options:
--ansi string Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
--compatibility Run compose in backward compatibility mode
--env-file string Specify an alternate environment file.
-f, --file stringArray Compose configuration files
--profile stringArray Specify a profile to enable
--project-directory string Specify an alternate working directory
(default: the path of the, first specified, Compose file)
-p, --project-name string Project name
Commands:
build Build or rebuild services
convert Converts the compose file to platform's canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service.
down Stop and remove containers, networks
events Receive real time events from containers.
exec Execute a command in a running container.
images List images used by the created containers
kill Force stop service containers.
logs View output from containers
ls List running compose projects
pause Pause services
port Print the public port for a port binding.
ps List containers
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service.
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker Compose version information
Run 'docker compose COMMAND --help' for more information on a command.
新建一个project-1 新建docker-compose.yml文件
root@ubuntu-3:~/docker-compose# mkdir -p project-1
root@ubuntu-3:~/docker-compose# cd project-1/
root@ubuntu-3:~/docker-compose/project-1# cat docker-compose.yml
version: '3.6'
services:
nginx-server: #服务的名称
image: nginx:alpine #服务使用的镜像
networks: #服务使用的网络
- front
expose: #容器暴露的端口
- 80
ports: #宿主机映射端口 8080为宿主映射端口
- "8080:80"
networks: #定义docker-compose使用的网络
front:
driver: bridge
使用up命令创建并启动容器
root@ubuntu-3:~/docker-compose/project-1# docker-compose up -d
root@ubuntu-3:~/docker-compose/project-1# curl localhost:8080 -I
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Fri, 11 Nov 2022 03:44:27 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Dec 2021 18:48:00 GMT
Connection: keep-alive
ETag: "61cb5be0-267"
Accept-Ranges: bytes
docker-compose常用命令示例
查看当前project-1工程services
root@ubuntu-3:~/docker-compose/project-1# docker-compose ps --services
nginx-server
查看当前project-1工程的容器
root@ubuntu-3:~/docker-compose/project-1# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10c5b916bfc5 nginx:alpine "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp project-1_nginx-server_1
停止容器
root@ubuntu-3:~/docker-compose/project-1# docker-compose stop
Stopping project-1_nginx-server_1 ... done
启动容器并在后台运行
root@ubuntu-3:~/docker-compose/project-1# docker-compose up -d
Starting project-1_nginx-server_1 ... done
停止并删除容器和网络
root@ubuntu-3:~/docker-compose/project-1# docker-compose down
Stopping project-1_nginx-server_1 ... done
Removing project-1_nginx-server_1 ... done
Removing network project-1_front
查看日志 log后面的nginx-server为service名称
root@ubuntu-3:~/docker-compose/project-1# docker-compose logs --tail=10 nginx-server
Attaching to project-1_nginx-server_1
nginx-server_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx-server_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: using the "epoll" event method
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: nginx/1.21.5
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: built by gcc 10.3.1 20211027 (Alpine 10.3.1_git20211027)
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: OS: Linux 5.15.0-52-generic
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: start worker processes
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: start worker process 24
nginx-server_1 | 2022/11/11 03:49:38 [notice] 1#1: start worker process 25
docker-compose 单机编排nginx+tomcat示例
root@ubuntu-3:~/docker-compose/project-1# cat docker-compose.yml
version: '3.6'
services:
nginx-server: #服务的名称
image: nginx:alpine #服务使用的镜像
networks: #服务使用的网络
- front
- backend
expose: #容器暴露的端口
- 80
ports: #宿主机映射端口 8080为宿主映射端口
- "8080:80"
links: #服务依赖
- tomcat-server
healthcheck: #健康检查
test: ['CMD', 'curl', '-f', 'tomcat-server:8080']
interval: 5s
timeout: 5s
retries: 3
start_period: 20s
tomcat-server:
image: tomcat:alpine
networks:
- backend
expose:
- 8080
deploy:
resources: #资源限制
limits:
cpus: '1'
memory: 512M
reservations:
cpus: '1'
memory: 512M
networks: #定义docker-compose使用的网络
front:
driver: bridge
backend:
driver: bridge
启动容器并查看的相关信息 WARNING信息可以忽略,yml的版本不同,有些不支持reservation
root@ubuntu-3:~/docker-compose/project-1# docker-compose up -d
root@ubuntu-3:~/docker-compose/project-1# docker-compose ps -a
WARNING: The following deploy sub-keys are not supported and have been ignored: resources.reservations.cpus
Name Command State Ports
----------------------------------------------------------------------------------------------------------------
project-1_nginx-server_1 /docker-entrypoint.sh ngin ... Up (healthy) 0.0.0.0:8080->80/tcp,:::8080->80/tcp
project-1_tomcat-server_1 catalina.sh run Up 8080/tcp
实现nginx代理tomcat 因为是在容器里面修改,重启容器后,修改会丢失。可以自己写dockerfile制作修改好的nginx镜像。
root@ubuntu-3:~/docker-compose/project-1# curl localhost:8080 | grep "Apache"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 615 100 615 0 0 59929 0 --:--:-- --:--:-- --:--:-- 61500
root@ubuntu-3:~/docker-compose/project-1# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b398a6d12b2 nginx:alpine "/docker-entrypoint.…" 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:8080->80/tcp, :::8080->80/tcp project-1_nginx-server_1
abd57a19dcb7 tomcat:alpine "catalina.sh run" 3 minutes ago Up 3 minutes 8080/tcp project-1_tomcat-server_1
root@ubuntu-3:~/docker-compose/project-1# docker exec -it 8b398a6d12b2 sh
/ # cat /etc/nginx/conf.d/default.conf | grep -i -C3 "8080" | grep -v "#"
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://tomcat-server:8080;
}
/ # nginx -s reload
2022/11/11 04:52:05 [notice] 839#839: signal process started
/ # exit
root@ubuntu-3:~/docker-compose/project-1# curl localhost:8080 | grep "Apache"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11184 0 11184 0 0 1969k 0 --:--:-- --:--:-- --:--:-- 2184k
<title>Apache Tomcat/8.5.41</title>
<h1>Apache Tomcat/8.5.41</h1>
User support and discussion for <a href="https://tomcat.apache.org/taglibs/">Apache Taglibs</a>
<h4>Apache Software Foundation</h4>
<li><a href="https://www.apache.org">Apache Home</a></li>
<p class="copyright">Copyright ©1999-2022 Apache Software Foundation. All Rights Reserved</p>