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 at the same time and attach to the containers that are defined in a file called ‘docker-compose.yml’. It is used in all environments such as development, staging, and production as well.
Syntax:
docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]
How compose-up works in Docker?
- 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’ which has 2 services, web and db, and one volume.
Here is the ‘docker-compose.yml’ file:
Code:
version: '2'
services:
web:
image: "nginx:alpine"
ports:
- "80:80"
volumes:
- my-vol:/var/www/html
db:
image: "mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
my-vol: {}
Example #1
Let’s run the ‘docker-compose up’ command to start these services at once:
Code:
docker-compose up
Output:
Explanation:
- In the above snapshot, we can see that the services are getting created and it is running, however, it gave some output on the console as well because the services are running in the foreground, not in the background.
- If we exit from here using the ‘ctrl+c’ shortcut, Docker daemon will stop the containers running under the service.
Example #2
We can specify the flags as per our requirement to control the behavior of the command.
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.
Code:
docker-compose up -d
Output:
b. Use of the ‘–quiet-pull’ flag
If we simply run the ‘docker-compose up’ command and if the Docker image mentioned in the ‘docker-compose.yml’ file does not exist locally then it will pull it from the registry and 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 progress bar as shown below:
docker-compose up --quiet-pull
- Without the ‘–quiet-pull’ flag
Code:
docker-compose up
Output:
- With the ‘–quiet-pull’ flag
Code:
docker image ls -a
Output:
Explanation:
- In the above snapshot, we can see that there was no Docker image available locally but still not showing the progress bar when started 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 are 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, Docker daemon creates new containers as below.
Code:
docker-compose up -d
Output:
In the above example, we can see that if we run the ‘docker-compose up -d’ twice, it says ‘up-to-date’.
In some scenarios, we want to recreate the containers whether there are any changes made to the file or not.
Let’s use the ‘–force-recreate’ without making any changes to the file to recreate the containers.
Code:
docker-compose up --force-recreate
Output:
Explanation:
- In the above snapshot, we can see that the container ID of the containers has been changed after running the command with the ‘–force-recreate’ flag.
Sometimes, we make the changes to the ‘docker-compose.yml’ file but don’t want to recreate the containers if already running.
Let’s use the ‘–no-recreate’ flag after changing the image name of the web service from ‘nginx:alpine’ to ‘nginx’.
Code:
docker-compose up --d --no-recreate
Output:
Explanation:
- 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.
Code:
docker-compose up --no-start
Output:
Explanation:
- 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 there is any service that is no longer required, we can use the ‘–remove-orphans’ flag to delete the unwanted containers. Here, just remove the ‘db’ service from the ‘docker-compose’ file and run the command as shown below.
Code:
docker-compose up -d --remove-orphans
Output:
Explanation:
- In the above snapshot, we can see that the orphan container is getting removed after removing 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 management of our application which has multiple services that make it easier to manage the containers.
- We can even build a Docker image while deploying the service using the ‘build’ directive.
Conclusion
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 to the ‘docker stack’ to start a full-blown application in Docker swarm.
Recommended Articles
This is a guide to docker-compose up. Here we discuss the introduction to docker-compose up with examples and advantages respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 3 Hands-on Projects | 11+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses