Direto na Imagem
Laboratório
Será utilitzada a aplicação:
~/devops-simulador-caos/src$ ls
Dockerfile package.json server.ts tsconfig.json views
Editar o Dockerfile:
$ vim Dockerfile
FROM node:20.9.0
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN apt update && apt install stress --yes
COPY . .
RUN npm run compile
HEALTHCHECK --interval=10s --timeout=5s --retries=1 --start-period=30s CMD ["curl","-f","http://localhost:3000/health"]
EXPOSE 3000
CMD ["npm", "start"]
Construir a imagem:
~/devops-simulador-caos/src$ docker image build \
-t marcelodpbarbieri/simulador-caos:v2 \
-f Dockerfile .
[+] Building 12.5s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 332B 0.0s
=> [internal] load metadata for docker.io/library/node:20.9.0 1.8s
=> [auth] library/node:pull token for registry-1.docker.io 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 53B 0.0s
=> [1/7] FROM docker.io/library/node:20.9.0@sha256:146bbe4eaee99ae885be2 0.2s
=> => resolve docker.io/library/node:20.9.0@sha256:146bbe4eaee99ae885be2 0.0s
=> => sha256:a6759aea6838201fb9b76b3ffccce537c745cef4b76 2.00kB / 2.00kB 0.0s
=> => sha256:03501b0671a4e8450444678df0aad9c78ecce5fe2f4 7.52kB / 7.52kB 0.0s
=> => sha256:146bbe4eaee99ae885be2a0a767f63a4b96032141d7 1.21kB / 1.21kB 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 24.80kB 0.0s
=> [2/7] WORKDIR /app 0.0s
=> [3/7] COPY package*.json ./ 0.0s
=> [4/7] RUN npm install 5.6s
=> [5/7] RUN apt update && apt install stress --yes 3.1s
=> [6/7] COPY . . 0.0s
=> [7/7] RUN npm run compile 1.3s
=> exporting to image 0.2s
=> => exporting layers 0.2s
=> => writing image sha256:fcf7250a5d8e31876ba86365cd12ec249c21dc6318bfe 0.0s
=> => naming to docker.io/marcelodpbarbieri/simulador-caos:v2 0.0s
O healthcheck não foi exposto na construção, pois o sistema de arquivos não está sendo alterado. O healthcheck altera os metadados da imagem:
$ docker image inspect marcelodpbarbieri/simulador-caos:v2 | grep -Ei -A 6 "health"
"Healthcheck": {
"Test": [
"CMD",
"curl",
"-f",
"http://localhost:3000/health"
],
"Interval": 10000000000,
"Timeout": 5000000000,
"StartPeriod": 30000000000,
"Retries": 1
},
Subir a imagem para o Docker Registry:
$ docker push marcelodpbarbieri/simulador-caos:v2
The push refers to repository [docker.io/marcelodpbarbieri/simulador-caos]
e192a053efd8: Pushed
68f77f0eebc6: Pushed
78936593e0c9: Pushed
080495642fe4: Pushed
06f78889df4d: Pushed
142f1e5cc194: Pushed
eba1ea52dc39: Layer already exists
17e3b73cb8fa: Layer already exists
b2c52efb0fe6: Layer already exists
652b81616682: Layer already exists
80bd043d4663: Layer already exists
30f5cd833236: Layer already exists
7c32e0608151: Layer already exists
7cea17427f83: Layer already exists
v2: digest: sha256:bee75b3afc0de959174f75812f4f818af51dca88da8ba9715081dc2e5491466d size: 3257
Atualizar a tag latest:
$ docker image tag marcelodpbarbieri/simulador-caos:v2 \
marcelodpbarbieri/simulador-caos:latest
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
marcelodpbarbieri/simulador-caos latest fcf7250a5d8e 4 minutes ago 1.17GB
marcelodpbarbieri/simulador-caos v2 fcf7250a5d8e 4 minutes ago 1.17GB
$ docker push marcelodpbarbieri/simulador-caos:latest
The push refers to repository [docker.io/marcelodpbarbieri/simulador-caos]
e192a053efd8: Layer already exists
68f77f0eebc6: Layer already exists
78936593e0c9: Layer already exists
080495642fe4: Layer already exists
06f78889df4d: Layer already exists
142f1e5cc194: Layer already exists
eba1ea52dc39: Layer already exists
17e3b73cb8fa: Layer already exists
b2c52efb0fe6: Layer already exists
652b81616682: Layer already exists
80bd043d4663: Layer already exists
30f5cd833236: Layer already exists
7c32e0608151: Layer already exists
7cea17427f83: Layer already exists
latest: digest: sha256:bee75b3afc0de959174f75812f4f818af51dca88da8ba9715081dc2e5491466d size: 3257
Editar o arquivo compose.yaml e alterar a versão para v2:
~$ vim compose.yaml
services:
web:
image: marcelodpbarbieri/simulador-caos:v2
ports:
- 8080:3000
restart: always
Subir o Docker Compose:
~$ docker compose up -d
[+] Running 2/2
✔ Network marcelo_default Created 0.1s
✔ Container marcelo-web-1 Started 0.3s
Monitoramento:
$ watch 'docker container ls -a'
Container em execução e saudável:
Caso seja utilizada a versão 1 da imagem, o healthcheck não seria habilitado:
$ vim compose.yaml
services:
web:
image: marcelodpbarbieri/simulador-caos:v1
ports:
- 8080:3000
restart: always
~$ docker compose up -d
[+] Running 1/1
✔ Container marcelo-web-1 Started 10.8s
Retornar para a versão 2 onde o healthcheck está habilitado:
~$ vim compose.yaml
services:
web:
image: marcelodpbarbieri/simulador-caos:v1
ports:
- 8080:3000
restart: always
~$ docker compose up -d
[+] Running 1/1
✔ Container marcelo-web-1 Started 10.9s
Desabilitar o healthcheck no Docker Compose:
~$ vim compose.yaml
services:
web:
image: marcelodpbarbieri/simulador-caos:v2
ports:
- 8080:3000
restart: always
healthcheck:
disable: true
~$ docker compose up -d
[+] Running 1/1
✔ Container marcelo-web-1 Started 10.8s
Last updated