# Criação básica de um Docker Compose

```bash
$ 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:

```bash
$ docker container run -d -p 8080:8080 nginx:latest
```

compose.yaml:

```yaml
version: "3.8"

services:
  nginx:
    image: nginx:latest
    ports:
      - "8080:80"
```

Subida do docker compose. O terminal ficou travado:

```bash
$ 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*:

```bash
$ 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:

```bash
$ 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
```

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2Fz2IB24gXeX9R5U18L6qv%2Fimage.png?alt=media&#x26;token=944a4aa8-bce7-401f-ae4f-e6cb8d1ca49b" alt=""><figcaption></figcaption></figure>

Se  o arquivo compose.yaml for alterado ele automaticamente recria o container. Por exemplo, alterar a porta para 8081:

```yaml
version: "3.8"

services:
  nginx:
    image: nginx:latest
    ports:
      - "8081:80"
```

```bash
$ 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 
```

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2FLfP5heFdN4ikop0PSqfT%2Fimage.png?alt=media&#x26;token=fceac232-1b9a-4325-a119-6655a9c71047" alt=""><figcaption></figcaption></figure>

O nome do container pode ser especificado ao invés de utilizar o nome padrão:

```bash
$ vim compose.yaml
```

```yaml
version: "3.8"

services:
  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - "8081:80"
```

```bash
~/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
```

```bash
~/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:

```bash
$ 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
```

```yaml
version: "3.8"

services:
  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - "8081:80"
    command:
      - echo
      - Hello World
```

```bash
$ 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 
```

```bash
$ 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:

```bash
$ 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:

```bash
~/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:

```bash
~/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:

```yaml
version: "3.8"

services:
  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - "8080:80"
  nginx02:
    container_name: nginx02
    image: nginx:latest
    ports:
      - "8181:80"

```

```bash
~/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

```

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2F8RIiKDQhEitAEWTl9n0R%2Fimage.png?alt=media&#x26;token=dfcf7010-2042-4445-aba4-56af96ba42f0" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2056950903-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7JJDMtYZqFc5W4Gd6mo1%2Fuploads%2FWzTvfY4Sajrdvt9sf44B%2Fimage.png?alt=media&#x26;token=7a6a776d-6626-44f8-8ee2-7f025624d453" alt=""><figcaption></figcaption></figure>
