Docker Compose
Docker Compose is a tool that allows you to define and run multi-container Docker applications. It provides a way to configure and orchestrate multiple Docker containers that work together to form a complete application.
With Docker Compose, you can define the configuration of your application and its dependencies in a single file, called a Compose file. This file specifies the services that make up your application, as well as the network, volumes, and other resources that they require.
Using the Compose file, you can start, stop, and manage your application with a single command. Docker Compose will create and manage the necessary containers, networks, and volumes, and ensure that they are properly connected and configured.
Docker Compose is particularly useful for complex applications that require multiple containers to work together, such as web applications that require a web server, a database, and a caching layer. With Docker Compose, you can define and manage all of these containers in a single file, making it easy to deploy and manage your application.
- To learn more about docker-compose visit here.
What is YAML?
YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format. It is often used for configuration files and data exchange between programming languages.
YAML is designed to be easy for humans to read and write, with a simple and intuitive syntax. It uses indentation to define nested structures and relies on whitespace rather than special characters to separate elements.
YAML files use a .yml or .yaml extension.
YAML is often used in conjunction with tools like Docker Compose and Kubernetes to define complex configurations for multi-container applications and infrastructure. YAML files are also used for configuration files in many other applications and programming languages.
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 is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.
Docker Compose is used for running multiple containers as a single service. Each of the containers here runs in isolation but can interact with each other when required. Docker Compose files are very easy to write in a scripting language called YAML, which is an XML-based language that stands for Yet Another Markup Language.
1)Install docker-compose
sudo apt install docker-compose
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.
The services section defines all the different containers we will create.
The build keyword specifies the location of our Dockerfile, and . represents the directory where the docker-compose.yml file is located.
The 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.
And then, we also specify the environment variables for MySQL which are required to run MySQL.
3)Run docker-compose.yml file
docker-compose up: This command does the work of the docker-compose build and docker-compose run commands.
docker ps
or docker-compose ps
command list all the containers in the current docker-compose file.
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.
Task-2
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. (Hint- Use usermod
command to give the user permission to docker). Make sure you reboot the instance after permitting the user.
For running the container as a non-root user, use the command usermod to give the user permission to docker
sudo usermod -a -G docker $USER
Then reboot the instance after permitting the user.
sudo reboot
Copy the docker image command from the docker hub. (ex - docker pull nginx
)
2) Inspect the container's running processes and exposed ports using the docker inspect command.
docker inspect <container_name or ID>
3) Use the docker logs command to view the container's log output.
docker logs <container_name or ID>
4) Use the docker stop and docker start commands to stop and start the container.
docker stop: To stop one or more running Docker containers.
docker stop <container-name or ID>
docker start: Start one or more stopped containers
docker start <container-name or ID>
5) 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 <container_name or ID>
You need to stop the running container before remove.
************************************************************************
Thanks for reading! Hope you find this helpful.
Happy learning !!!
Suggestions are always welcome.
Thank You - Shubham Londhe