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:
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:
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:
However, if there is a need to set/allocate the time to wait for stop/halt before killing the container, use the following command:
How to Remove, Create, and Start a Single Container with Docker Compose?
To omit the target container, apply the following command:
Now, remove the “wordpress” container using the below command:
After that, execute the provided command to create the container:
Lastly, start the created container:
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.