docker-compose是Docker公司官方开源项目,主要对单机容器的快速编排,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
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.
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
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
root@ubuntu-3:~/docker-compose/project-1# docker-compose ps --services
nginx-server
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
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
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
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
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>