Day 18 - Docker-Compose

Day 18 - Docker-Compose

Β·

3 min read

Docker for DevOps Engineers

Till now you have created Docker file and pushed it to the Repository. Let's move forward and dig more on other Docker concepts. Aj thodi padhai krte hai on Docker Compose πŸ˜ƒ

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.

  • Learn more about docker-compose visit here

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.

  • Read more about it here


Task-1

Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

Sample docker-compose.yaml file

Step 1 : Installing docker-compose

sudo apt-get update

sudo apt install docker-compose -y

sudo docker-compose --version

Step 2 : Create a docker-compose.yml file

vi docker-compose.yml

  1. version:

    Which version of Docker Compose we are about to use

  2. services:

    It defines what all types of services need to be created

    -> Here we are going to create a web application called nginx by using a prebuilt public image from dockerhub and it is port mapped as 80:80

    -> We are also creating a database mysql which is also a prebuild public image from dockerhub and port mapped as 3306:3306

  3. environment: The clause allows us to set up an environment variable in the container [here set up a password for MySQL]. This is the same as the -e argument in Docker when running a container.

Step 3 : Run the command

docker-compose up -d

docker ps

2 Containers nginx/mysql got created

Access the nginx web application with ec2 publicip:80 in web browser

Do not forget to allow the port 80 in security inbound rules os ec2 instance to access the nginx


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 user permission to docker). Make sure you reboot instance after giving permission to user.

  • Step 1 : Install docker

sudo apt-get update

sudo apt install docker.io

docker --version

sudo systemctl status dockers

docker pull nginx

--- throws an error because ( user ubuntu is not a part of docker group )

so we need add the user ubuntu to docker group by below command and reboot

sudo usermod -aG docker $USER

sudo reboot

After reboot (changes have been saved)

docker pull ngnix

nginx image is pulled from dockerhub

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

How to run Docker commands without sudo?

  • Make sure docker is installed and system is updated (This is already been completed as a part of previous tasks):

  • sudo usermod -a -G docker $USER

  • Reboot the machine.

For reference you can watch this video


Β