What is 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.
Simplifies complex app deployment and interactions.
Enables scaling and ensures portability.
Streamlines development with reproducible environments.
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.
Widely adopted in DevOps for configuration management.
Supports complex data structures and is easy to read and write.
Tasks
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.
version : "3.3" services: web: image: nginx:latest ports: - "80:80" db: image: mysql ports: - "3306:3306" environment: - "MYSQL_ROOT_PASSWORD=test@123"
Docker Compose allows you to define the environment for your application, containers, networks, and volume in a single YAML file.
It allows you to configure each service, specifying the docker image, port volume and other components required for your application.
It also enables you to define the link between the containers.
You can even set the environment variables within the docker-compose.yml file, which makes it easy to control the behaviour of the container without modifying the docker image.
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 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.
Steps:
To pull a pre-existing docker image from a public repository like DockerHub, you can use the docker pull. Write the actual image name after the command.
docker pull <image_name>
Before running the docker container, give permission to docker so you can use it without sudo by using the sudo usermod command.
sudo usermod -aG docker $USER
This will add the user (ubuntu) to the docker group.
After giving the permissions to the docker, make sure to reboot your system.
sudo reboot
To run the container as a non-root user, you typically set the user inside the docker image itself.
docker run -d --name my-container <image_name>
Replace the actual name of the docker image after the command.
You can use the docker inspect command to inspect the container's details including running processes and exposed port.
docker inspect <container_name>
If you don't know how to get the container name, use the docker ps command and simply copy the name.
To view the container's log output, use the docker logs command.
docker logs <container_name_or_Id>
Use the actual container name or ID.
To start or stop the container, use the following command:
#to stop the container docker stop <container_name> #to start the container docker start <container_name>
Use the actual container name.
Finally, when you are done with the container, you can remove the container by using the following command.
docker rm <container_name>
Use the actual container name.
How to use Docker commands without sudo?
Make sure that the is updated and docker is installed.
Use the sudo usermod -aG docker $USER command to give permissions to docker (as we see in the previous task).
Reboot the system using the sudo reboot command.
<That's all for today. Hope you like it. FOLLOW me to join me in the journey of DevOps>