Search code examples
dockerdocker-composestartup

What is the best way to auto start multiple existing dependent containers?


I have a wordpress container which depends on another mysql container. Basically I want to automatically start them at startup time. As there is dependency between them, I am wondering how could I ensure this dependency.

I've taken a look at docker-compose, but it seems that docker-compose can only manipulate images instead of containers. As I don't want to mount volumns from container to my host, I don't want to create new container every time.

I've also taken a look at manually create two services using systemd. But it is difficult to decide if mysql is launched(Although the container has been launched, its service still needs more time.).

So what is the recommended way to launch dependent containers at startup time?


Solution

  • You can use the depends_on param to manage the dependencies Here is the compose file for your usecase

    version: '3.3'
    
    services:
       db:
         image: mysql:5.7
         volumes:
           - db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: somewordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress
    
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         ports:
           - "8000:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
    volumes:
        db_data: {}
    

    More Info: https://docs.docker.com/compose/wordpress/