Examples of docker-compose up | Advantages – eduCBA

<img src

=”https://cdn.educba.com/academy/wp-content/uploads/2021/01/docker-compose-up.jpg” alt=”docker-compose up” /> Introduction to docker-compose up ‘

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.

Syntax:

docker-compose up [options] [-

scale SERVICE=NUM…] [SERVICE…]

docker-compose up <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 ‘

docker-compose.yml

  • ‘. We define all services, volumes, networks, etc. in this file.
  • When we run the ‘docker-compose up’ command from the Docker CLI, the Docker

  • 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.

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:

my-vol

:

{}

Example #1

Let’s run the ‘docker-compose up

‘ command to start these services at once

:

Code

:

docker-compose

up

Output

:

docker-compose up 2

Explanation:

  • 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.

Example #2

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

.

Code

: docker-compose up -d

Output:

b. Using

the ‘-quiet-pull’ flag If we simply run the command ‘

docker-compose up

‘ 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 ‘-

quiet-pull

‘ 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

Code

:

docker image ls

a

Output

:

<img src

=”https://cdn.educba.com/academy/wp-content/uploads/2020/08/docker-compose-up-6.jpg” alt=”‘-quiet-pull’ flag” />

Explanation:

  • 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

‘ command.

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.

Code

: docker-compose up -d

Output:

<img src

=”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.

Code

: docker-compose up -force-recreate

Output

:

docker-compose up 8

Explanation:

  • 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’.

Code

: docker-compose up -d -no-recreate

Output

:

docker-compose up 9

Explanation:

  • 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.

Code

:

docker-compose up

-no-start Output:

no-start' flag

Explanation:

  • 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 ‘-

remove-orphans’

flag

to remove unwanted containers. Here, simply remove the ‘db’ service from the ‘docker-compose’ file and run the command as below.

Code

: docker-compose up -d -remove-orphans

Output

:

remove-orphans' flag

Explanation:

  • 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.

Conclusion

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.

Recommended articles

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:

  1. Dockerfile
  2. Docker

  3. Push Advantages of
  4. Docker

  5. Docker
  6. Architecture