=”https://cdn.educba.com/academy/wp-content/uploads/2021/01/docker-compose-up.jpg” alt=”docker-compose up” /> Introduction to docker-compose up ‘
‘ is a Docker command to start and run an entire application on a separate host that contains multiple services, for example, Web, DB, etc. You can also create volumes and networks at the same time and attach them to containers that are defined in a file named ‘docker-compose.yml’. It is also used in all environments, such as development, staging and production.
docker-compose up [options] [-
scale SERVICE=NUM…] [SERVICE…]
<img src="https://cdn.educba.com/academy/wp-content/uploads/2020/08/docker-compose-up-1.jpg" alt="docker-compose up 1
How does compositing work in Docker?
The docker-compose up command looks for the file in the current directory for a file named ‘
- ‘. We define all services, volumes, networks, etc. in this file.
- CLI converts ‘docker-compose.yml’ to ‘json’ and makes a REST API call to the Docker API server and the Docker daemon creates all the services, networks, volumes, etc. for us.
- You have multiple options to control deployment, for example, if there are containers running for a service and we have made some changes to the service configuration 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 changes using the ‘-no-recreate’ command.
When we run the ‘docker-compose up’ command from the Docker CLI, the Docker
Examples of docker-compose up
Below are the examples
: We will create a docker-compose.yml’ that has 2 services, web and db, and a volume. Here is the file ‘docker-compose.yml’: 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:
Let’s run the ‘docker-compose up
‘ command to start these services at once
- In the snapshot above, we can see that services are being created and running, however, it also gave some output in the console because services run in the foreground, not in the background.
- If we leave here using the shortcut ‘ctrl + c’, the Docker daemon will stop the containers running under the service.
We can specify the flags according to our requirement to control the behavior of the command
Let’s understand some essential flags with examples
: a. Using the ‘-d’ flag We can use the ‘-d’ flag to run the
services in separation mode as we use them while running a new container
: docker-compose up -d
the ‘-quiet-pull’ flag If we simply run the command ‘
‘ and if the Docker image mentioned in the
file ‘docker-compose.yml’ does not exist locally so it will take it out of the registry and shows the progress bar of it and if we are not interested in the progress bar, we use the flag ‘-quiet-pull’ to silently extract the Docker image without progress bar as shown below: docker-compose
up -quiet-pull Without the ‘-
‘ flag Code: docker-compose up Output: <img src="https://cdn.educba.com/academy/wp-content/uploads/2020/08/docker-compose-up-5.jpg" alt="docker-compose
up 5″ />
- With ‘-quiet-pull’ indicator
docker image ls
=”https://cdn.educba.com/academy/wp-content/uploads/2020/08/docker-compose-up-6.jpg” alt=”‘-quiet-pull’ flag” />
- In the snapshot above, we can see that there was no Docker image available locally but it still didn’t show the progress bar when the services started because we used the ‘-quiet-pull
c. Using the ‘-force-recreate’ and ‘-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 no configuration changes are made to the ‘docker-compose.yml’ file, while if we have made some changes to the file but do not want to recreate the containers if it is already running, we use the ‘—no-recreate’ flag. By default, if there are no changes to the ‘docker-compose.yml’, the Docker daemon does nothing and if changes are detected in the file, the Docker daemon creates new containers as shown below.
: docker-compose up -d
=”https://cdn.educba.com/academy/wp-content/uploads/2020/08/docker-compose-up-7.jpg” alt=”docker-compose up” /> In the
example above, 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 or not there are 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 snapshot above, we can see that the container ID of the containers has been changed after executing the command with
the ‘-force-recreate’ flag. Sometimes, we make the changes to the ‘
docker-compose.yml’ file but we don’t want to recreate the containers if they are already running.
Let’s use the ‘-no-recreate’
flag After renaming the web service image from ‘nginx:alpine’ to ‘nginx’.
: docker-compose up -d -no-recreate
- In the example above, we can see that the new Docker image has been extracted, but if we check the container ID of the containers, it is the same before and after executing the command using the ‘-no-recreate’ flag, which means that no new container was created.
d. Using the ‘-no-start’ flag
Used to create the containers but does not start it. Cannot be used with the ‘-d’ indicator.
- In the snapshot above, we can see that the container has just been created but is not running.
and. Using the ‘-remove-orphans’ flag If there is any service that is no longer needed, we can use the ‘-
to remove unwanted containers. Here, simply remove the ‘db’ service from the ‘docker-compose’ file and run the command as below.
: docker-compose up -d -remove-orphans
- In the snapshot above, we can see that the orphaned container is being deleted after removing the ‘db’ service from the file.
Advantages of docker-compose up
Below are the advantages mentioned:
- We can start a complete application using this command in one shot
- It provides a single point of management of our application that has multiple services that facilitate the management of containers
- We can even create a Docker image while deploying the service using the ‘build’ directive.
docker-compose up is a command to start an entire application, however, it only works on the standalone host, not in Docker swarm mode, so it is not recommended for production. We have to go to the ‘docker stack’ to start a full-fledged application in Docker swarm.
This is a guide to writing docker. Here we discuss the introduction to docker-compose up with examples and advantages respectively. You can also take a look at the following articles to learn more:
- Push Advantages of