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.

  • The heart of Docker Compose is the docker-compose.yml file, where each service is defined with various options, such as the Docker image to use, exposed ports, and environment variables.

  • With Docker Compose, you can specify the various components of your application, such as databases, web servers, backend services, and more, as separate containers.

  • These containers can communicate with each other and work together to form a complete application stack.

What is YAML?

  • A popular data serialization language for writing configuration files is YAML. YAML can mean either yet another markup language or YAML ain't markup language (a recursive acronym), highlighting the fact that YAML is for data rather than documents.

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

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

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.

Step 1: Install docker-compose.

Step 2 : Create a docker-compose.yml file inside project folder.

docker-compose.yml file inside project

Explanation of code :-

· Docker compose version is 3.3
· In services section defines web server and db server.
· web: This service is defined with the nginx:latest image, which pulls the latest version of the Nginx image from Docker Hub. It maps port 80 on the host machine to port 80 in the container, allowing you to access the Nginx web server.
· db: This service uses the mysql image, which is the official MySQL image from Docker Hub. It maps port 3306 on the host to port 3306 in the container.
· To run mysql we specify the environment variable for mysql.

Step 3: Run docker-compose.yml file using command docker-compose up -d

docker ps or docker-compose ps command list all the containers in the current compose file.

Step 4 : Check your application is running or not in web browser

Step 5 :docker-compose down -This command stops all the services and cleans up the containers, networks, and images.

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.

Step 1: Pull the latest ngnix server from docker hub

Step2 : User Permission to Docker:

Add your user to the docker group to allow running Docker commands without using sudo.

sudo usermod -a  -G docker $USER

Make sure to reboot the server after giving permission.

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

Run the container using command docker run -d –name <name> -p <portnumber> imagename

The docker inspect command to inspect the container's running processes and exposed ports:

docker inspect <container_name or ID>

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

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

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

  • docker stop : To stop one or more running Docker containers.

      docker stop <container-name or ID>
    

docker start : Start one or more stopped containers

docker start <container-name or ID>

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

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

    docker rm : Remove one or more containers

      docker rm <container_name or ID>
    

How to run Docker commands without sudo?

Open a terminal and run the following command to add your user to the Docker group:

sudo usermod -a  -G docker $USER

Once adding your user to the docker group, you need to either log out or log back in to make the changes affected.

sudo reboot

Verify that your user has the necessary permissions to run Docker commands without sudo by running a Docker command without sudo

  docker info

Thank you for reading this blog! Hope you find this article helpful.

Happy Learning!