Day 18 Task: Docker for DevOps Engineers

Day 18 Task: Docker for DevOps Engineers

Docker Compose

  • Docker Compose is a tool that was developed to help define and share multi-container applications.

  • With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

What is YAML?

  • YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

  • YAML is a popular programming language because it is human-readable and easy to understand.

  • YAML files use a .yml or .yaml extension.

Docker Compose Commands -

The docker-compose command provides several subcommands to manage Docker containers with docker-compose. Please keep in mind that we deal with services and not containers while working 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 mentioned in the .yaml file

    • docker-compose build <web> - Build a single service named web

  • 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 a single container named web

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

    • docker-compose down - Stop and delete all containers

    • docker-compose down <web> - Stop and delete a single container named web

  • 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
  • exec – This will execute a command to the running container. For eg- list files in a container associated with the web service.

    • docker-compose exec web ls -l
  • start - This will start stopped containers of the services defined in the config file

    • docker-compose start - Start all containers

    • docker-compose start <web> - Start single container named web

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

    • docker-compose stop - Stop all containers

    • docker-compose stop <web> - Stop single container named web

  • restart – This will restart containers of the services defined in the config file

    • docker-compose restart - Restart all containers

    • docker-compose restart <web> - Restart single container named web

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

    • docker-compose pause - Pause all containers

    • docker-compose pause <web> - Pause single container named web

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

    • docker-compose unpause - Start all paused containers

    • docker-compose unpause <web> - Start a single paused container named web

  • rm – This will remove stopped containers for the services defined in the config file.

    • docker-compose rm - Remove all stopped containers

    • docker-compose rm <web> - Remove a single stopped container named web

Let's look at the components from the above file -

version - specifies the version of docker-compose to be used. Should be in quotes otherwise it will be considered as something else.

services - to list all the services that docker-compose has to create for you.

image - it will create the container from an already existing image present in your system or it will download from a repository.

ports - it maps the port number of the container with the host's port number.

environment - it will define the environment variables to be used in the container.

Some components that were not used in the above file -

build - it takes the path of the dockerfile from the image that will be built to run the container.

volume - it mounts a host's directory with a directory in the container.

We can check the status of both services here -

Now, if you are using an AWS EC2 instance, go to <ec2_public_address:80>.

For this, you may have to add a TCP rule for port 80 in your instance's security group.

We have successfully created services using docker-compose.yaml

Task 2: Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine.

Run the container as a non-root user (Hint- Use usermod command to give the user permission to docker). Make sure you reboot the instance after giving permission to the user**.**

Inspect the container's running processes and exposed ports using the docker inspect command.

Use the docker logs command to view the container's log output.

Use the docker stop and docker start commands to stop and start the container.

Use the docker rm command to remove the container when you're done.

We can remove a container only if it is stopped.

Today, we did a hands-on on docker and docker-compose commands.

Thanks for reading!

#devops#90DaysOfDevops#TrainWithShubham

Let's connect on Linkedin - linkedin.com/in/namya-khullar-7b5758200