Docker

How to Restart a Single Container with Docker Compose

Docker Compose is a feature that allows the programmer to manage multiple containers as a single service. However, the Docker Compose CLI comprises the cmdlets that can be applied to a single container. For instance, the “docker-compose restart” command enables restarting a target container or a service without affecting the other executing containers or services.

This tutorial covers the below-listed content:

How to Setup Docker Compose?

Before restraining a single container with Docker Compose, first, overview the prerequisites required to set up Docker Compose and incorporate the containers, services, etc. in the “docker-compose.yml” file. To do so, consider the following steps:

Step 1: Create a “docker-compose.yml” File

First of all, create the stated file and write the below-given code in it:

version: '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:

 
In this code, perform the below-given steps:

    • The “image” keyword is utilized to specify the image from Docker hub for the “mysql” and “wordpress” containers.
    • For the database, the “ports” keyword is used to specify the ports needed to be exposed for “wordpress”.
    • Also, specify the environment variables for both “mysql” and “wordpress” needed to run “mysql” and “wordpress”, respectively.

Step 2: Execute the “docker-compose.yml” File

Now, build and execute the Docker Compose file via the following cmdlet:

docker-compose up -d

 

How to Restart/Reinitiate a Single Container with Docker Compose?

Now, to restart a single container with Docker, use the below-applied command that restarts the “wordpress” container:

docker-compose restart wordpress

 

However, if there is a need to set/allocate the time to wait for stop/halt before killing the container, use the following command:

docker-compose restart -t 30 wordpress

 

How to Remove, Create, and Start a Single Container with Docker Compose?

To omit the target container, apply the following command:

docker-compose stop wordpress

 

Now, remove the “wordpress” container using the below command:

docker-compose rm wordpress

 

After that, execute the provided command to create the container:

docker-compose create wordpress

 

Lastly, start the created container:

docker-compose start wordpress

 

Conclusion

A single container can be restarted with Docker Compose using the “docker-compose restart” command followed by the target container’s name to be restarted. However, to reload from the recipe container i.e., the “docker-compose.xml” file, the container can be removed and then created and started again as well.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.