Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Docker Compose: A Mini Orchestration Tool for Local Development
56%

Healthchecks and Wait-for-it Scripts

To ensure that a service is fully ready before another service starts, we can use healthchecks. A healthcheck is a command that runs inside the container to check if the service is healthy. If the command returns a successful exit code, the service is considered healthy.

We can define a healthcheck in the docker-compose.yml file using the healthcheck option. Here is an example:

cat << EOF > $HOME/wordpress/docker-compose.yml
services:
  db:
    image: mysql:9.6.0
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: mypassword
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
  wordpress

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More