With all of your services defined in your docker-compose file, you just need to issue a single command to start all of the containers, create the volumes, and set up and connect the networks: docker-compose up -d When you run docker-compose up for the first time, it will download all of the necessary Docker images, which might take a while. We’ll introduce two different tools (dockerize and docker-compose-wait tool) to make docker compose wait for any service or dependencies to start. You can use these two solutions to deploy your application stack, particularly when you want: docker compose wait for mysql, docker compose wait for postgres, docker compose wait for redis.
If you are new to docker, and if you have taken over a system that already has docker application running, you should at least know how to maintain it.
This quick tutorial explains how to start, stop, remove, restart, and view status of docker container application using docker-compose.
docker-compose is very helpful when you are managing a complex multi container docker application.
1. Start Docker Containers In the Background
All the services of your application are typically defined under the docker-compose.yml file. Inside this yml file, you’ll also define all your application service dependencies.
Sometimes, you might also have a separate Dockerfile, where you’ll specify how to build a particular image.
Typically, when you execute docker-compose up, it will download and pull the appropriate image (if it is not cached locally on your server), it will then build the image using your application code, and finally start the whole docker application with all the dependencies.
To start, go to the directory where docker-compose.yml file resides, and execute the following docker-compose up command.
You’ll notice that it will download the container only the 1st time when you execute it, after that, it will use the cached version. You’ll not see the “Pulling..” line in the about output anymore.
You’ll only see the following when you start the docker-compose from the next time around.
The -d options runs the docker application in the background as a daemon. This will leave the application running until you decide to stop it.
In the above example output, it has started the following services:
- mongo for database
- nginx for webserver
- tomcat for application server
2. Start Docker Containers In the Foreground
When you don’t specify the -d option, docker-compose will start all the services in the foreground.
In this case, you can see all log messages directly on the screen.
This is helpful when you are debugging any startup related issues with your docker containers, images, or services.
In this case, the application will be up and running until you hit Ctrl-C to cancel the foreground process.
In this case, when you press Ctrl-C, it is equivalent to executing the “docker-compose stop”. So, it will stop all the containers gracefully.
3. Additional docker-compose Startup Options
When you use docker-compose up, if there are any changes in the docker-compose.yml file that affects the containers, they will stopped and recreated.
But, you can force docker-compose not to stop and recreate the containers, you can use –no-recreate option as shown below during the docker-compose up. In other words, if the container already exits, this will not recreate it.
You also can do the opposite. The following will forcefully recreate the containers even if nothing in the docker-compose.yml is changed.
You can also specify the timeout value. Default value is 10 seconds, but the following command will use the time-out value of 30 seconds.
The following are few additional options you can use along with “docker-compose up”
- –no-deps This will not start any linked depended services.
- –no-build This will not build the image, even when the image is missing
- –abort-on-container-exit This will stop all the containers if any container was stopped. You cannot use this option with -d, you have to use this option by itself.
- –no-color In the output, this will not show any color. This will display the monochrome output on screen.
4. Stop All Docker Containers
To stop a docker application that is running in the foreground, you just have to press Ctrl-C as show above.
But, to stop a docker application that is running in the background, use the docker-compose stop as shown below.
There are two steps to stop a docker application containers:
- First, stop the running containers using docker-compose stop
- Second, remove the stopped containers using docker-compose rm -f
Stop the application containers using docker-compose stop:
Remove the application containers using docker-compose rm -f:
Note: If you don’t specify -f in the above command, it will prompt you for Y/N before removing it.
Since you’ll be doing this frequently, combine both of the above stop and rm, as shown below.
In this case, since we have “&&”, which will execute the 2nd command only after the 1st command is successful. So, it will do “rm -f”, only after stopping the docker containers successfully.
5. Stop a Specific Docker Container
Instead of stopping all the containers, you can also specifically stop a particular service.
The following example, will stop only the data container
You can also specify a shutdown time-out during docker-compose stop. By default it will wait for 10 seconds. For some reason, if you know that your application might take little longer to stop, you may want to increase this time-out as shown below during the shutdown.
6. Remove Container Volumes
While removing a stopped containers, it doesn’t remove all the volumes that are attached to the containers.
In a typical situation, you don’t want to remove the attached volumes during your regular stop/start/rm process.
But, if you decide to remove the attached volumes, you can do that during rm by using -v option as shown below.
The following will remove the volumes that are attached to the containers.
You can also remove a specific container by specifying the container name. The following will remove only the data container.
Download Docker Compose
7. Status of Docker Containers
To view the Status of an docker application, execute the following docker-compose ps command.
In the above output, we see that all of our three containers are running without any issue. The above output doesn’t show the container id. If you want to get an ID for a particular container, use the -q option.
The following will display the ID for the data container.
After a docker-compose rm -f, if you execute the docker-compose ps, you’ll not see any containers listed in the output.
However, after a docker-compose stop, if you execute docker-compose ps, you’ll see empty values in the “Ports” column, and the “State” column will display Exit and the corresponding exit value of the process when it stopped.
8. Restart Multiple Docker Containers
To summarize, if you just want to restart multiple containers that are created by docker-compose.yml file, use the following commands in sequence.
This will first stop all the containers, next remove all the containers, and finally start them in the background as specified by the docker-compose.yml file.
First, cd to the directory where docker-compose.yml file is present, and then execute the following to restart.
- Docker Tutorial
- Docker Useful Resources
- Selected Reading
Download Docker Compose Version 3
Docker Compose is used to run multiple containers as a single service. For example, suppose you had an application which required NGNIX and MySQL, you could create one file which would start both the containers as a service without the need to start each one separately.
In this chapter, we will see how to get started with Docker Compose. Then, we will look at how to get a simple service with MySQL and NGNIX up and running using Docker Compose.
Docker Compose ─ Installation
The following steps need to be followed to get Docker Compose up and running.
Step 1 − Download the necessary files from github using the following command −
The above command will download the latest version of Docker Compose which at the time of writing this article is 1.10.0-rc2. It will then store it in the directory /home/demo/.
Step 2 − Next, we need to provide execute privileges to the downloaded Docker Compose file, using the following command −
We can then use the following command to see the compose version.
Syntax
Parameters
version − This is used to specify that we want the details of the version of Docker Compose.
Output
The version details of Docker Compose will be displayed.
Example
The following example shows how to get the docker-compose version.
Output
You will then get the following output −
Creating Your First Docker-Compose File
Now let’s go ahead and create our first Docker Compose file. All Docker Compose files are YAML files. You can create one using the vim editor. So execute the following command to create the compose file −
Let’s take a close look at the various details of this file −
The database and web keyword are used to define two separate services. One will be running our mysql database and the other will be our nginx web server.
The image keyword is used to specify the image from dockerhub for our mysql and nginx containers
For the database, we are using the ports keyword to mention the ports that need to be exposed for mysql.
And then, we also specify the environment variables for mysql which are required to run mysql.
Now let’s run our Docker Compose file using the following command −
This command will take the docker-compose.yml file in your local directory and start building the containers.
Once executed, all the images will start downloading and the containers will start automatically.
And when you do a docker ps, you can see that the containers are indeed up and running.