Docker for DevOps Engineers part-2🚀

Docker for DevOps Engineers part-2🚀

Introduction🔥

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file named "docker-compose.yml" to configure the services, networks, and volumes that your application requires. With Docker Compose, you can start and stop all the services in your application with a single command. it's especially useful for local development environments and small to medium-sized projects.

docker-compose commands:

# creates and starts containers for all the services specified:
docker-compose up

# To run the containers in detached mode (in the background):
 docker-compose up -d

# command to stop and remove the containers, networks, and volumes defined in the docker-compose.yml file:
docker-compose down

# command lists the running containers associated with your Docker Compose project:
docker-compose ps

# command to specify a service name to see logs from a specific service:
docker-compose logs <service-name>

# execute commands in a running container:
docker-compose exec <service-name> <command>

# builds the Docker images for the services defined in the docker-compose.yml file:
docker-compose build

# pulls the latest images for the services defined in the docker-compose.yml file:
docker-compose pull

# restarts the services in your Docker Compose setup:
docker-compose restart
version : "3.3"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123"
  • This line specifies the version of the Docker Compose syntax being used. In this case, it's version 3.3.

  • services section defines the different services (containers) that you want to run as part of your Docker Compose setup.

  • the web is the name of the service you're defining.

  • image: nginx: latest specifies that this service should use the latest version of the official Nginx Docker image from Docker Hub.

  • ports indicate how you want to map ports from the host machine to the container.

  • 80:80 maps port 80 from the host to port 80 in the container. This means you can access the Nginx web server running inside the container by accessing port 80 on your host machine.

  • db is the name of the second service you're defining.

  • image: MySQL specifies that this service should use the official MySQL Docker image from Docker Hub.

  • ports similarly map port 3306 from the host to port 3306 in the container. This is typically used for MySQL database connections.

environment is used to set environment variables within the container. In this case, you're setting the MYSQL_ROOT_PASSWORD environment variable to test@123. This is the password that will be set for the root user of the MySQL database.

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 permitting 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.👤

in this example, we will pull and run the official Nginx image from Docker Hub.

Pull and Run the Docker Image:

# Pull the Nginx image
docker pull nginx:latest

# Run the Nginx container as a non-root user
docker run -d --name my-nginx -p 80:80 -u 1000:1000 nginx:latest

Inspect Container Processes and Exposed Ports:

# container details using docker inspect 
docker inspect my-nginx

View Container Log Output:

# View container logs
docker logs my-nginx

Stop and Start Container:

# Stop the container
docker stop my-nginx

# Start the container again
docker start my-nginx

Remove container:

# Remove the container
  docker rm my-nginx

Conclusion🧬🏭

in conclusion, Docker Compose offers a streamlined approach to managing multi-container applications through a single configuration file. This simplifies the orchestration of services, networks, and volumes, enhancing the development and deployment process. Moreover, Docker commands such as docker rm, docker stop, docker logs, docker inspect, and docker pull provide vital tools to efficiently interact with containers. These commands enable users to manage container lifecycle, gain insights into running instances, access logs, inspect configurations, and fetch images from repositories. Collectively, these capabilities empower developers and administrators to effectively deploy, monitor, troubleshoot, and maintain Docker-based applications.

Did you find this article valuable?

Support Achyut Das by becoming a sponsor. Any amount is appreciated!