Day 18/90 Docker for DevOps Engineers
Dnyaneshwari khapekar
Expert in Linux, Networking, AWS, EC2, S3, Jenkins, Shell Scripting, Git
Docker Compose
Working of Docker Compose
Using Docker-Compose is essentially a three-step process:
1. Define application environment with Dockerfile for all services.
2. Create a docker-compose.yml file defining with all services under application.
3. Run docker-compose up to run all services under applications.
Key features of Docker Compose
YAML
The syntax of the YAML file is:
keyword: argument
Example of a YAML file
name: XYZ
age: 100
email: [email protected]
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
Docker Compose can be installed as Docker Desktop (Docker Engine + Docker Compose), Docker Compose Plugin, or a Standalone Docker Compose.
I am going to install docker-compose as a standalone in Ubuntu. To install docker-compose, the following commands are used:
sudo apt-get update
sudo apt-get install docker-compose
To verify the successful installation of docker-compose, check the version:
docker-compose version
Once installed, we can start configuring services for the Docker engine by creating docker-compose.yaml file. Let us consider the sample docker-compose.yaml file provided to us. The file has the following information:
version : "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
depends_on:
- db
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123
Let’s break down and understand line by line of the above file.
| version: "3.3"
This line specifies the version of the Docker Compose file format being used. In this case, it’s version 3.3.
| services:
Next part, we define two services: ‘web’ and ‘db’. Each service represents a containerized component of the application.
web:
image: nginx:latest
ports:
- "80:80"
depends_on:
- db
For the web service:
NOTE:
depends_on keyword is used for linking two containers.
The web service depends on the db service. Here's how the linking works:
领英推荐
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
For the db service:
Once the services are defined, to start containers, use the following command:
docker-compose up
We can observe that db is pulled first and then the web.
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.
----> I am pulling the Nginx image. and running the container.> I am pulling the Nginx image. and running the container.
docker pull nginx
docker run -d -p 8080:80 nginx
----> To run the container as a non-root user and use the docker commands without sudo, we need to give the user permission to docker using the following command and then reboot the system:
sudo usermod -aG docker ubuntu
sudo reboot
----> The docker inspect command is used to retrieve detailed information about Docker objects such as containers, images, networks, and volumes.
docker images
docker inspect nginx
The output of docker inspect command has various details in JSON format. Look for the following sections to use custom formatting and inspect running processes and exposed ports:
----> Use the docker logs command to view the container’s log output.
docker logs <container_id>
----> Use the docker stop and docker start commands to stop and start the container.
docker stop cont_id
docker start cont_id
We can observe that once the container is stopped, it is not up. As soon as we start the container, it becomes up.
----> Use the docker rm command to remove the container when you’re done.
We cannot remove a running container. Hence, either we need to stop the container and then delete the container or perform the forceful removal of the container. Let’s forcefully remove the container:
docker rm -f container_id
Thanks.. for reading..
Frontend Developer?? || Growth expert ?? || Follow-> Devops related content?? || Helping Client's to Grow their Profile and Business?? || Feel Free to connect ??|| DM for Promotion ??
11 个月Nice share