惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
有赞技术团队
有赞技术团队
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
博客园 - Franky
小众软件
小众软件

Maciej Walkowiak - Java & Spring

Blog Generating HTTP clients in Spring Boot application from OpenAPI spec PostgreSQL and UUID as primary key Dynamic Projections with Spring Data JPA Container logs with Spring Boot and Testcontainers Reified Generics in Java? Faster integration tests with reusable Testcontainers and Flyway Running one-time jobs with Quartz and Spring Boot The best way to use Testcontainers with Spring Boot Spring Boot & Flyway - clear database between integration tests What's new in Spring? Activate Maven Profile by Operating System Spring Boot with Thymeleaf and Tailwind CSS - Complete Guide How to publish a Java library to Maven Central - Complete Guide Single file Java applications with JBang Beautiful bash scripts with Gum Running Java on CRaC How to log PostgreSQL queries with Testcontainers Spring Boot 3.0 & GraalVM Native Image - not a free lunch Creating Spring Cloud Function projects with AWS SAM Loading classpath resources to String with a custom JUnit extension Creating Project Templates with Cookiecutter Auto-Registering JUnit 5 extensions Spring Boot component scanning without annotations Listing Maven dependencies in Spring Boot Actuator Info endpoint Spring Cloud AWS 2.3 RC2 Released How I built vlad-cli - command line interface to Vlad Mihalcea The State of Java Relational Persistence On Choosing a Tech Stack
Docker Compose - waiting until containers are ready
2022-11-03 · via Maciej Walkowiak - Java & Spring
Published on
  • Docker
  • Docker Compose

Docker Compose since version 2.1.1 has a new flag for the detached mode - --wait.

Running docker-compose -d --wait waits until containers are healthy and only then exits (containers of course still run in the background).

To make it work, it has to be defined what does healthy mean - each container the command is meant to wait for must have defined healthcheck entry in docker-compose.yml.

For example, we can verify that Postgres & MySQL is ready to accept connections with following configuration:

yml

services:
  postgres:
    image: postgres:13.3
    environment:
      - POSTGRES_USER=test
      - POSTGRES_DB=test
      - POSTGRES_PASSWORD=test
    healthcheck:
      test: ["CMD", "pg_isready"]
      interval: 3s
      timeout: 5s
      retries: 5

Healthcheck configuration ​

Let's break down the healthcheck configuration options:

  • test - is a command to run - if it returns 0 - container is healthy, 1 - unhealthy
  • interval - time to wait before first test is made and then between further test invocations
  • timeout - time to wait until test command returns
  • retries - the number of retries to run test until we give up
  • start_period - assumed time that containers need to start and become healthy - if test returns 1 during this time, it is not counted against the number of retries

Here are test commands for some popular containers that I often use:

  • Postgres: [ "CMD", "pg_isready" ]
  • MySQL: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
  • Redis: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
  • RabbitMQ: rabbitmq-diagnostics -q ping

Result ​

And that's how it looks in action:

docker-compose up -d --wait

It is useful especially for chaining commands, for example - start services with Docker Compose and once they are ready, run application:

bash

$ docker-compose up -d --wait && ./mvnw spring-boot:run

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed