Updated May 19, 2023
Introduction to docker-compose up
‘docker-compose up’ is a Docker command to start and run an entire app on a standalone host that contains multiple services, for example, Web, DB, etc. It can also create volumes and networks simultaneously and attach to the containers defined in a file called ‘docker-compose.yml.’ It is used in all environments, including development, staging, and production.
docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]
How does compose-up work in Docker?
- The docker-compose up command searches for the file in the current directory for a file called ‘docker-compose.yml.’’ We define all services, volumes, networks, etc., in this file.
- When we execute the ‘docker-compose up’ command from Docker CLI, Docker CLI converts the ‘docker-compose.yml’ to ‘json’ and makes a REST API call to the Docker API server, and Docker daemon creates all the services, networks, volumes, etc. for us.
- It has multiple options to control the deployment; for example, if there are running containers for a service and we have made some changes to the configuration of the service and want to apply it using the ‘docker-compose up’ command, then Docker daemon stops all existing containers and recreates it; however, we can prevent the changes by using the ‘–no-recreate’ command.
Examples of docker-compose up
Given below are the examples:
We will create a docker-compose.yml’ with 2 services, web and db, and one volume.
Here is the ‘docker-compose.yml’ file:
Let’s run the ‘docker-compose up’ command to start these services at once:
- In the above snapshot, we can see that the services are getting created and running; however, it also gave some output on the console because the services are running in the foreground, not in the background.
- If we exit using the ‘ctrl+c’ shortcut from here, the Docker daemon will stop the containers running under the service.
We can specify the flags per our requirement to control the command’s behavior.
Let’s understand some essential flags with examples:
a. Use of the ‘-d’ flag
We can use the ‘-d’ flag to run the services in detach mode, as we use while running a new container.
docker-compose up -d
b. Use of the ‘–quiet-pull’ flag
If we run the ‘docker-compose up’ command and the Docker image mentioned in the ‘docker-compose.yml’ file does not exist locally, it will pull it from the registry. It shows the progress bar of the same, and if we are not interested in the progress bar, we use the ‘–quiet-pull’ flag to silently pull the Docker image without the progress bar, as shown below:
docker-compose up --quiet-pull
- Without the ‘–quiet-pull’ flag
- With the ‘–quiet-pull’ flag
docker image ls -a
- In the above snapshot, we can see that no Docker image was available locally but still not showing the progress bar when starting the services because we have used the ‘–quiet-pull’ command.
c. Use of the ‘–force-recreate’ and the ‘–no-recreate’ command
The above flags are contradictory to each other. We can only use one flag at a time. The ‘–force-recreate’ flag is used to recreate the containers even if there is no configuration changes have been made to the ‘docker-compose.yml’ file, whereas if we have made some changes to the file but don’t want to recreate the containers if it is already running, then we use the ‘—no-recreate’ flag. By default, if there are no changes in the ‘docker-compose.yml,’ Docker daemon does nothing, and if there are any changes detected in the file, the Docker daemon creates new containers as below.
docker-compose up -d
The above example shows that if we run the ‘docker-compose up -d’ twice, it says ‘up-to-date.’
In specific scenarios, there is a need to recreate the containers regardless of any changes made to the file.
Let’s use the ‘–force-recreate’ without making any changes to the file to recreate the containers.
docker-compose up --force-recreate
- In the above snapshot, we can observe that the container ID of the containers changed after running the command with the ‘–force-recreate’ flag.
Sometimes, we change the ‘docker-compose.yml’ file but don’t want to recreate the containers if they are already running.
Let’s use the ‘–no-recreate’ flag after changing the image name of the web service from ‘nginx:alpine’ to ‘nginx.’
docker-compose up --d --no-recreate
- In the above example, we can see that the new Docker image has been pulled, but if we check the container ID of the containers, that is the same before and after running the command using the ‘–no-recreate’ flag, which means no new container was created.
d. Use of the ‘–no-start’ flag
It is used to create the containers but does not start it. It cannot be used with the ‘-d’ flag.
docker-compose up --no-start
- In the above snapshot, we can see that the container is just created, but it is not running.
e. Use of the ‘–remove-orphans’ flag
If any service is no longer required, we can use the ‘–remove-orphans’ flag to delete the unwanted containers. Here, remove the ‘db’ service from the ‘docker-compose’ file and run the command as shown below.
docker-compose up -d --remove-orphans
- The above snapshot demonstrates the removal of the orphan container after eliminating the ‘db’ service from the file.
Advantages of docker-compose up
Given below are the advantages mentioned:
- We can start a complete application using this command in a single shot.
- It provides a single point of our application management with multiple services, making it easier to manage the containers.
- We can even build a Docker image using the build’ directive while deploying the service.
docker-compose up is a command to start a full-blow application; however, it only works on the standalone host, not in the Docker swarm mode, so it is not recommended for production. We have the ‘docker stack’ to start a full-blown application in Docker swarm.
This is a guide to docker-compose up. Here we discuss the introduction to docker-compose up with examples and advantages. You may also have a look at the following articles to learn more –