Docker

How to Understand Docker Compose Up and Down?

Docker Compose is a tool used to specify and execute numerous containers as a single service. It permits users to specify the configuration for multiple Docker containers, such as ports, volumes, images, dependencies, etc., in a single YAML file. It starts and stops Docker containers together through the “docker-compose” command. The most often used commands in Docker Compose are “up” and “down”.

This write-up will explain Docker Compose up and down with an example.

How to Understand Docker Compose Up and Down?

The “docker-compose up” is a command that is used to build and start containers for a service defined in a docker-compose.yml file. It builds the images first for the services if they do not exist. This command is also useful for deploying multi-container apps with one command.

On the other hand, the “docker-compose down” command is utilized to stop and remove containers, volumes, networks, and images created by “docker-compose up”. Moreover, running this command permanently removes any data stored in the containers. Thus, it is recommended to take backups of essential data before using this command.

Example of Docker Compose Up and Down

Let us take an example where we create a compose file with some configurations. Then, start and stop the compose services to see how they work. To do so, check out the below-provided steps.

Step 1: Create Compose File
On Visual Studio Code, create a new file named “docker-compose.yml”. Then, configure the required services. For instance, we have configured the following services:

services:
  web:
    image: nginx:latest
    ports:
      - "9090:80"

In the above code:

  • services” key is utilized to configure the compose services. Here, we have configured one service i.e., “web”.
  • web” service uses the “nginx:latest” image and maps port “9090” on the host machine to port “80” in the container.

Step 2: Start the Compose Services
To start the compose services, utilize the “docker-compose up” command:

docker-compose up

This command has created and started the containers for a service defined in a compose file.

Step 3: Verification
Now, navigate to the allocated ports of the local host and verify whether the services are executing in the containers or not:

The above output indicates that the compose service has been started successfully.

Step 4: Stop Compose Services
To stop the compose services, execute the “docker-compose down” command:

docker-compose down

The above-stated command has stopped and removed the Docker container and network.

Conclusion

The “docker-compose up” command builds and starts containers for a service defined in a docker-compose.yml file. Whereas the “docker-compose down” command stops and removes containers, volumes, networks, and images created by “docker-compose up”. This write-up has explained Docker Compose up and down with the practical implementation.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.