Skip to main content

Command Palette

Search for a command to run...

18 Task: Docker for DevOps Engineers

Published
3 min read
18 Task: Docker for DevOps Engineers

Docker Compose

Docker Compose is another best tool for docker to set up multi-container environments. Using this create a single compose file with defining all the containers with their environments. You can easily use a single command to build images and run all the containers.

There is a three-step process to work with Docker Compose.

1. Define the application environment with Dockerfile for all services.
2. Create a docker-compose.yml file defining all services under the application.
3. Run docker-compose up to run all services under applications.

Docker Compose CLI Reference

The docker-compose command provides several subcommands to manage Docker containers with docker-compose.

build –

The build option is used to build images for services for which the build is defined.

$ docker-compose build ## Build all services

$ docker-compose build web ## Build single service

up –

Use to create docker containers with available services in the docker-compose.yml file in the current directory. Use the -d switch to launch containers in daemon mode.

$ docker-compose up -d ## Create all containers

$ docker-compose up -d web ## Create single container

down –

This will stop and delete all containers, network and associated images for the services defined in a config file

$ docker-compose down ## Restart all containers

$ docker-compose down web ## Restart single container

ps –

This will list all containers created for the services defined in a config file with their status, port bindings and command.

$ docker-compose ps

start –

This will start stopped containers of the services defined in config file

$ docker-compose start ## Start all containers

$ docker-compose start web ## Start single container

stop –

This will stop running containers for the services defined in config file

$ docker-compose stop ## Stop all containers

$ docker-compose stop web ## Stop single container

restart –

This will restart containers of the services defined in config file

$ docker-compose restart ## Restart all containers

$ docker-compose restart web ## Restart single container

pause –

This will pause running containers for the services defined in config file.

$ docker-compose pause ## Pause all containers

$ docker-compose pause web ## Pause single container

unpause –

This will start paused containers for the services defined in config file.

$ docker-compose pause ## Start all paused containers

$ docker-compose pause web ## Start single paused container

YAML- Yet Another Markup Language

YAML is a popular programming language because it is designed to be easy to read and understand. It can also be used in conjunction with other programming languages. Because of its flexibility and accessibility, YAML is used by Ansible.

YAML syntax

YAML files use a .yml or .yaml extension and follow specific syntax rules.

3 dashes (---) are used to signal the start of a document.

TASK :

How to run Docker commands without sudo?

If you want to be able to skip sudo for docker commands, add your user to the docker group:

$ sudo usermod -aG docker <plug your user here>

  1. Install docker-compose

  1. create docker-compose.yml file

  1. Now Run the docker-compose.yml file using the command docker-compose up -d

  2. Check containers using the command docker ps

  3. Check whether your application is running or not in a web browser using ec2’s public IP.

  4. To stop the services and delete the container use the command docker-compose down.

  5. Use the docker logs <container_id> command to view the container’s log output.

  6. Use the docker rm <container_id> command to remove the container when you’re done.

    Use the -f option to remove the container forceable.