Docker: docker-compose file

发布时间 2023-09-25 11:44:00作者: ZhangZhihuiAAA

docker-compose.yaml:

version: '3.3'

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

With regards to the preceding docker-compose file, we have the term services that refers to all the applications that we would be running in one go. In this first example test, we can attempt to run the nginx container image under the service called nginx.
We can also set it such that we would export port 80 of the nginx container to our current workstation’s port 8080.

zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$ docker compose -f docker-compose.yaml up
[+] Running 2/1
 ✔ Network aaa_default    Created                                                                                                                                 0.1s 
 ✔ Container aaa-nginx-1  Created                                                                                                                                 0.0s 
Attaching to aaa-nginx-1
aaa-nginx-1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
aaa-nginx-1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
aaa-nginx-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
aaa-nginx-1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
aaa-nginx-1  | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
aaa-nginx-1  | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
aaa-nginx-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
aaa-nginx-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
aaa-nginx-1  | /docker-entrypoint.sh: Configuration complete; ready for start up
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: using the "epoll" event method
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: nginx/1.25.2
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: OS: Linux 6.2.0-33-generic
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker processes
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker process 29
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker process 30
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker process 31
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker process 32
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker process 33
aaa-nginx-1  | 2023/09/25 03:15:50 [notice] 1#1: start worker process 34
^CGracefully stopping... (press Ctrl+C again to force)
Aborting on container exit...
[+] Stopping 1/1
 ✔ Container aaa-nginx-1  Stopped                                                                                                                                 0.4s 
canceled

We can stop running the nginx application via docker-compose by doing a keyboard interrupt. However, the application would still be around (although stopped), and we will probably be unable to rerun the command: docker-compose -f docker-compose.yaml up once more without any issue. To properly clean up, we can run the following command:

zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$ docker compose -f docker-compose.yaml down
[+] Running 2/2
 ✔ Container aaa-nginx-1  Removed                                                                                                                                 0.0s 
 ✔ Network aaa_default    Removed

 

docker-compose.yaml:

version: '3.3'

services:
  app:
    build:
      context: .
      dockerfile: dockerfile
    ports:
      - 8080:8080

When we use build here, it serves as some sort of indication to docker-compose that the image for this service might require us to run a Docker container build process to generate it.

zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$ docker compose -f docker-compose.yaml up -d
[+] Building 9.2s (12/12) FINISHED                                                                                                                      docker:default
 => [app internal] load .dockerignore                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                   0.0s
 => [app internal] load build definition from Dockerfile                                                                                                          0.0s
 => => transferring dockerfile: 214B                                                                                                                              0.0s
 => [app internal] load metadata for docker.io/library/golang:1.21                                                                                                2.8s
 => [app 1/7] FROM docker.io/library/golang:1.21@sha256:c416ceeec1cdf037b80baef1ccb402c230ab83a9134b34c0902c542eb4539c82                                          0.0s
 => [app internal] load build context                                                                                                                             0.0s
 => => transferring context: 459B                                                                                                                                 0.0s
 => CACHED [app 2/7] WORKDIR /app                                                                                                                                 0.0s
 => CACHED [app 3/7] ADD go.mod go.sum ./                                                                                                                         0.0s
 => CACHED [app 4/7] RUN go env -w GOPROXY=https://goproxy.io,direct                                                                                              0.0s
 => CACHED [app 5/7] RUN go mod download                                                                                                                          0.0s
 => [app 6/7] ADD . .                                                                                                                                             0.0s
 => [app 7/7] RUN go build -o app .                                                                                                                               6.1s
 => [app] exporting to image                                                                                                                                      0.3s
 => => exporting layers                                                                                                                                           0.3s
 => => writing image sha256:c5ca78c1b68074e5e4031f248a133252105b44ba94a62d45497a9a7fe7348b1d                                                                      0.0s
 => => naming to docker.io/library/aaa-app                                                                                                                        0.0s
[+] Running 2/2
 ✔ Network aaa_default  Created                                                                                                                                   0.1s 
 ✔ Container aaa-app-1  Started

 

zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$ docker compose logs
aaa-app-1  | 2023/09/25 03:28:10 Hello world sample started.