Day-18 Task: Docker for DevOps Engineers(Docker-Compose): Part-3

Day-18 Task: Docker for DevOps Engineers(Docker-Compose): Part-3

What is Docker?

Docker is an open platform for developing, shipping and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

To learn more about docker visit the link https://deepakcloud22.hashnode.dev/day-16-task-docker-for-devops-engineers-part-1

What is Docker Compose?

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

With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

How to Install Docker Compose?

Visit the Docker official site and download the latest version of the Docker compose tool.

Here we will install the docker-compose tool in the ubuntu linux operating system.

sudo apt-get install docker-compose -y : To install the docker-compose tool

sudo docker-compose --version To check the docker-compose installed/version

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.

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.

Here we will create the docker-compose file to deploy the Wordpress and MySQL containers.

Step 1: Install docker-compose

We have already installed the docker-compose in our system.

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

  • version:"3.3" : denotes that we are using version 3.3 of Docker Compose.

  • services : The service section defines all the different containers we will create. In our example, we have two services, WordPress as web01, and MySQL as db01 .

  • image : The image keyword is used to specify the image from the docker hub for WordPress and MySQL containers.

  • ports : The ports keyword is used to expose the ports of the container and the host (the left side port is the host-port and the right side is container-port )

  • container_name : The container_name is used to give the name for the containers.

  • volumes : The volume keyword in the service is used to attach the volume to the containers(wordpress:/var/www/html and db-vol:/var/lib/mysql)

  • environment : the environment keyword is used to set the environment variables. Here we used

    - MYSQL_USER=wordpress_user : Mysql user.

    - MYSQL_PASSWORD=wordpress_password : Mysql user password.

    - MYSQL_DATABASE=wordpress : Mysql database name.

    - MYSQL_ROOT_PASSWORD="redhat" : Mysql root password.

  • volumes : This keyword is used as another volume service that will create the volumes which we attach to the containers. (wordpress: {} and db-vol: {})

Step 3: Validate the docker-compose.yml file

docker-compose config : Validate the docker-compose file.

Step 4: Run the docker-compose.yaml file

docker-compose up : This command does the work of the docker-compose build and docker-compose run commands. It builds the images if they are not located locally and starts the containers. If images are already built, it will fork the container directly.

The -d flag executes the docker-compose file in the background.

docker ps : Lists of the running container

docker-compose ps : Shows lists of all the containers in the current docker-compose file.

Step 5: Verify that the WordPress application is working by accessing it in a web browser using EC2 instance public IP address.

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 (Hint- Use usermod command to give 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.

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

2. Run the container as a non-root user, use the command usermod to give a user permission to docker and reboot the instance.

sudo usermod -a  -G docker $USER

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

  • First, deploy the container using nginx:alpine image using

sudo docker run -d --[container-name] -p [host-port:container-port] [image-name]

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

sudo docker inspect [container-name or container-id]

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

sudo docker logs [container-name or container-id]

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

sudo docker stop [container-name or container-id] : Stop the running container.

sudo docker start [container-name or container-id] : Start the running container.

6. Use the docker rm command to remove the container.

First, stop the container if running to remove or

Use --force, -f : Flag to remove the running container forcefully.

sudo docker rm [container-name or container-id]

sudo docker rm -f [container-name or container-id]

How to run Docker commands without sudo?

To run the docker command without sudo use the command usermod and reboot the instance after executing the usermod command.

sudo usermod -a  -G docker $USER


Thank you for reading the article.

Thanks for your valuable time.

Happy Learning !... Keep Learning ! 😊