ZhangZhihui's Blog  

depends_on

depends_on expresses startup and shutdown dependencies between services.

Short syntax

The short syntax variant only specifies service names of the dependencies. Service dependencies cause the following behaviors:

  • Compose creates services in dependency order. In the following example, db and redis are created before web.

  • Compose removes services in dependency order. In the following example, web is removed before db and redis.

Simple example:

services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

Compose guarantees dependency services have been started before starting a dependent service. Compose waits for dependency services to be "ready" before starting a dependent service.

 

复制代码
services:
  postgres:
    image: postgres:16-alpine
    environment:
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=aaa
      - POSTGRES_DB=zimple_bank
  api:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - DB_SOURCE=postgresql://root:aaa@postgres:5432/zimple_bank?sslmode=disable
    depends_on:
      - postgres
复制代码

 

但是从下面的日志看depends_on并不能保证在postgres数据库可用之后才启动web服务:

复制代码
[+] Running 3/2
 ✔ Network zimplebank_default       Created                                                                                                         0.1s 
 ✔ Container zimplebank-postgres-1  Created                                                                                                         0.0s 
 ✔ Container zimplebank-api-1       Created                                                                                                         0.0s 
Attaching to zimplebank-api-1, zimplebank-postgres-1
zimplebank-postgres-1  | The files belonging to this database system will be owned by user "postgres".
zimplebank-postgres-1  | This user must also own the server process.
zimplebank-postgres-1  | 
zimplebank-postgres-1  | The database cluster will be initialized with locale "en_US.utf8".
zimplebank-postgres-1  | The default database encoding has accordingly been set to "UTF8".
zimplebank-postgres-1  | The default text search configuration will be set to "english".
zimplebank-postgres-1  | 
zimplebank-postgres-1  | Data page checksums are disabled.
zimplebank-postgres-1  | 
zimplebank-postgres-1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
zimplebank-postgres-1  | creating subdirectories ... ok
zimplebank-postgres-1  | selecting dynamic shared memory implementation ... posix
zimplebank-postgres-1  | selecting default max_connections ... 100
zimplebank-postgres-1  | selecting default shared_buffers ... 128MB
zimplebank-postgres-1  | selecting default time zone ... UTC
zimplebank-postgres-1  | creating configuration files ... ok
zimplebank-api-1       | [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
zimplebank-api-1       | 
zimplebank-api-1       | [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
zimplebank-api-1       |  - using env:  export GIN_MODE=release
zimplebank-api-1       |  - using code: gin.SetMode(gin.ReleaseMode)
zimplebank-api-1       | 
zimplebank-api-1       | [GIN-debug] POST   /users                    --> github.com/ZhangZhihuiAAA/zimplebank/api.(*Server).CreateUser-fm (3 handlers)
zimplebank-api-1       | [GIN-debug] POST   /users/login              --> github.com/ZhangZhihuiAAA/zimplebank/api.(*Server).LoginUser-fm (3 handlers)
zimplebank-api-1       | [GIN-debug] POST   /accounts                 --> github.com/ZhangZhihuiAAA/zimplebank/api.(*Server).CreateAccount-fm (4 handlers)
zimplebank-api-1       | [GIN-debug] GET    /accounts/:id             --> github.com/ZhangZhihuiAAA/zimplebank/api.(*Server).GetAccount-fm (4 handlers)
zimplebank-api-1       | [GIN-debug] GET    /accounts                 --> github.com/ZhangZhihuiAAA/zimplebank/api.(*Server).ListAccounts-fm (4 handlers)
zimplebank-api-1       | [GIN-debug] POST   /transfers                --> github.com/ZhangZhihuiAAA/zimplebank/api.(*Server).CreateTransfer-fm (4 handlers)
zimplebank-api-1       | [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
zimplebank-api-1       | Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
zimplebank-api-1       | [GIN-debug] Listening and serving HTTP on 0.0.0.0:8080
zimplebank-postgres-1  | running bootstrap script ... ok
zimplebank-postgres-1  | sh: locale: not found
zimplebank-postgres-1  | 2023-12-16 08:35:02.705 UTC [30] WARNING:  no usable system locales were found
zimplebank-postgres-1  | performing post-bootstrap initialization ... ok
zimplebank-postgres-1  | initdb: warning: enabling "trust" authentication for local connections
zimplebank-postgres-1  | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
zimplebank-postgres-1  | syncing data to disk ... ok
zimplebank-postgres-1  | 
zimplebank-postgres-1  | 
zimplebank-postgres-1  | Success. You can now start the database server using:
zimplebank-postgres-1  | 
zimplebank-postgres-1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
zimplebank-postgres-1  | 
zimplebank-postgres-1  | waiting for server to start....2023-12-16 08:35:03.328 UTC [36] LOG:  starting PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit
zimplebank-postgres-1  | 2023-12-16 08:35:03.329 UTC [36] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
zimplebank-postgres-1  | 2023-12-16 08:35:03.331 UTC [39] LOG:  database system was shut down at 2023-12-16 08:35:03 UTC
zimplebank-postgres-1  | 2023-12-16 08:35:03.335 UTC [36] LOG:  database system is ready to accept connections
zimplebank-postgres-1  |  done
zimplebank-postgres-1  | server started
zimplebank-postgres-1  | CREATE DATABASE
zimplebank-postgres-1  | 
zimplebank-postgres-1  | 
zimplebank-postgres-1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
zimplebank-postgres-1  | 
zimplebank-postgres-1  | waiting for server to shut down....2023-12-16 08:35:03.497 UTC [36] LOG:  received fast shutdown request
zimplebank-postgres-1  | 2023-12-16 08:35:03.498 UTC [36] LOG:  aborting any active transactions
zimplebank-postgres-1  | 2023-12-16 08:35:03.500 UTC [36] LOG:  background worker "logical replication launcher" (PID 42) exited with exit code 1
zimplebank-postgres-1  | 2023-12-16 08:35:03.500 UTC [37] LOG:  shutting down
zimplebank-postgres-1  | 2023-12-16 08:35:03.501 UTC [37] LOG:  checkpoint starting: shutdown immediate
zimplebank-postgres-1  | 2023-12-16 08:35:03.565 UTC [37] LOG:  checkpoint complete: wrote 925 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.010 s, sync=0.052 s, total=0.065 s; sync files=301, longest=0.002 s, average=0.001 s; distance=4269 kB, estimate=4269 kB; lsn=0/191BA30, redo lsn=0/191BA30
zimplebank-postgres-1  | 2023-12-16 08:35:03.572 UTC [36] LOG:  database system is shut down
zimplebank-postgres-1  |  done
zimplebank-postgres-1  | server stopped
zimplebank-postgres-1  | 
zimplebank-postgres-1  | PostgreSQL init process complete; ready for start up.
zimplebank-postgres-1  | 
zimplebank-postgres-1  | 2023-12-16 08:35:03.614 UTC [1] LOG:  starting PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit
zimplebank-postgres-1  | 2023-12-16 08:35:03.614 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
zimplebank-postgres-1  | 2023-12-16 08:35:03.614 UTC [1] LOG:  listening on IPv6 address "::", port 5432
zimplebank-postgres-1  | 2023-12-16 08:35:03.616 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
zimplebank-postgres-1  | 2023-12-16 08:35:03.618 UTC [52] LOG:  database system was shut down at 2023-12-16 08:35:03 UTC
zimplebank-postgres-1  | 2023-12-16 08:35:03.622 UTC [1] LOG:  database system is ready to accept connections
复制代码

 

posted on   ZhangZhihuiAAA  阅读(281)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示