Criação básica de um Docker Compose
$ cd ~
$ mkdir compose
$ cd compose
/compose$
$ touch compose.yaml
$ vim compose.yaml
Ao invés de executar o container com o comando abaixo, será criagado com o compose:
$ docker container run -d -p 8080:8080 nginx:latest
compose.yaml:
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "8080:80"
Subida do docker compose. O terminal ficou travado:
$ docker compose -f compose.yaml up
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/0
✔ Container compose-nginx-1 Created 0.0s
Attaching to nginx-1
nginx-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx-1 | 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
nginx-1 | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: using the "epoll" event method
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: nginx/1.27.1
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: OS: Linux 5.15.146.1-microsoft-standard-WSL2
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: start worker processes
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: start worker process 22
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: start worker process 23
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: start worker process 24
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: start worker process 25
nginx-1 | 2024/09/03 10:51:31 [notice] 1#1: start worker process 26
Subida do compose em mode dettached:
$ docker compose -f compose.yaml up -d
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/1
✔ Container compose-nginx-1 Started 0.2s
O container está sendo executado e pode ser acessado:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65015b52c1d5 nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 38 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp compose-nginx-1
Se o arquivo compose.yaml for alterado ele automaticamente recria o container. Por exemplo, alterar a porta para 8081:
version: "3.8"
services:
nginx:
image: nginx:latest
ports:
- "8081:80"
$ docker compose up -d
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/1
✔ Container compose-nginx-1 Started 1.0s
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2574a24b25d2 nginx:latest "/docker-entrypoint.…" 33 seconds ago Up 31 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp compose-nginx-1
O nome do container pode ser especificado ao invés de utilizar o nome padrão:
$ vim compose.yaml
version: "3.8"
services:
nginx:
container_name: nginx
image: nginx:latest
ports:
- "8081:80"
~/compose$ docker compose up -d
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 2/2
✔ Container compose-nginx-1 Recreated 0.6s
✔ Container nginx Started 0.3s
~/compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
586a00cb498c nginx:latest "/docker-entrypoint.…" 6 seconds ago Up 4 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp nginx
Pode-se reescrever o comando com o Docker Compose ao invés de utilizar o comando abaixo:
$ docker container run nginx echo hello world
hello world
$ docker container run nginx echo hello world
hello world
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
35a4ee97b531 nginx "/docker-entrypoint.…" 5 seconds ago Exited (0) 4 seconds ago modest_keldysh
version: "3.8"
services:
nginx:
container_name: nginx
image: nginx:latest
ports:
- "8081:80"
command:
- echo
- Hello World
$ docker compose up -d
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/1
✔ Container nginx Started
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca35e76327c8 nginx:latest "/docker-entrypoint.…" 21 seconds ago Exited (0) 20 seconds ago nginx
Baixar o Docker Compose:
$ docker compose down
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 2/2
✔ Container nginx Removed 0.0s
✔ Network compose_default Removed 0.6s
Parar o Docker Compose:
~/compose$ docker compose up -d
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 2/2
✔ Network compose_default Created 0.1s
✔ Container nginx Started
~/compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7542994383e7 nginx:latest "/docker-entrypoint.…" 14 seconds ago Up 14 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
~/compose$ docker compose stop
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Stopping 1/1
✔ Container nginx Stopped 0.6s
~/compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
~/compose$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
7542994383e7 nginx:latest "/docker-entrypoint.…" 33 seconds ago Exited (0) 5 seconds ago nginx
Iniciar o Docker Compose:
~/compose$ docker compose start
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 1/1
✔ Container nginx Started 0.2s
~/compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7542994383e7 nginx:latest "/docker-entrypoint.…" 41 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
Pode ser especificado muliplos containers:
version: "3.8"
services:
nginx:
container_name: nginx
image: nginx:latest
ports:
- "8080:80"
nginx02:
container_name: nginx02
image: nginx:latest
ports:
- "8181:80"
~/compose$ docker compose up -d
WARN[0000] /home/marcelo/compose/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 2/2
✔ Container nginx Running 0.0s
✔ Container nginx02 Started 0.3s
~/compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06e0af593137 nginx:latest "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:8181->80/tcp, :::8181->80/tcp nginx02
7542994383e7 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
Last updated