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

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

🌀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 service networks, and volumes, 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.

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

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

1️⃣Step1 : Install docker-compose

sudo apt-get install docker-compose

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

vim docker-compose.yml

version: '3'
services:
    backend:
    build:
      context: .
    ports:
      - "5000:5000" # - known as list
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql   # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
      - mysql-data:/var/lib/mysql  # Mount the volume for MySQL data storage

volumes:
  mysql-data:
  • version: "3" denotes that we are using version 3 of Docker Compose.

  • The services section defines all the different containers we will create.

  • Build keyword specifies the location of our Dockerfile, and . represents the directory where the docker-compose.yml file is located.

  • Image keyword is used to specify the image from the docker hub for mysql containers

  • For the database and web, we are using the ports keyword to mention the ports that need to be exposed.

  • Then, we also specify the environment variables for mysql which are required to run mysql.

3️⃣Step 3: Run the docker-compose.yml file

docker-compose up -d

To verify container is created

docker ps

4️⃣ Step 4: Verify that the application is working by accessing it in a web browser

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

docker-compose down

💼Task-2:

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

  • 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 permitting the user.

1️⃣Step 1: Run the container as a non-root user

To run the container as a non-root user, use the command usermod to give the user permission to docker

sudo usermod -a -G docker $USER

2️⃣Step 2: Pull a pre-existing Docker image from the Docker Hub and run it on your local machine.

docker pull nginx

Run the docker image

docker run -d -p 8000:8000 nginx:latest

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

docker inspect 2e1c9c74b20c

4️⃣ Step 4: Use the docker logs command to view the container's log output.

docker logs 2e1c9c74b20c

5️⃣Step 5: Use the docker stop and docker start commands to stop and start the container.

docker stop 2e1c9c74b20c

docker start 2e1c9c74b20c

6️⃣Step 6: Use the docker rm command to remove the container when you're done.

docker rm: Remove one or more containers.

--force, -f: Force the removal of a running container.

docker rm -f 2e1c9c74b20c

📍 Conclusion:

With these tasks, you've taken a significant step toward mastering Docker Compose and further enhanced your Docker skills. Keep exploring and experimenting with different configurations to deepen your understanding of container orchestration.

Thank you for reading!

Contact me on Linkedin 🤝

Check out my GitHub for more resources 📚