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.
What is YAML?
YAML is a data serialization language that is often used for writing configuration files. It is a popular programming language because it is human-readable and easy to understand.
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.
The structure of a YAML file is a map or a list.
YAML files use a .yml or .yaml extension.
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.
Let us git clone the 'react_django_demo_app' project & create a Yaml file "docker-compose.yml"
then add details like
version: represents the version of docker-compose.
services: denotes the different containers or services of docker.
image: specifies the image of docker services.
port: describes the port of the services which is to be exposed.
build: denotes the location of the Dockerfile.
environment: through this, we can set environment variables.
Execute docker compose up
command with -d
as an option to operate the process at the backend. Containers can be seen running by the command docker ps
or docker container ls
& Images by docker images
We can access the web services with respective ports as mapped. Thus we can see docker compose can run multiple services and containers with a single command.
Here in addition to this, we can remove the containers by using simply docker compose down
the command which cleans up containers automatically.
Task-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.
To perform this operation without sudo, we need the command
sudo usermod -a -G docker $USER
then dosudo reboot
Then again ssh login, pull the image from DockerHub withoutsudo
and run the container from the image on our local machine.Inspect the container's running processes and exposed ports using the docker inspect command.
Here we could check or inspect the running status of the container and on which ports it is exposed by 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.
Here we could check the logs of containers by
docker logs
commandThen we can stop and start the container by using
docker stop
anddocker start
commandsUse the docker rm command to remove the container when you're done.
For the complete removal of a container, we just need to stop the container then only we can remove that container by the
docker rm
command.
Reference:
Learn more about YAML
Thanks for reading and also for your valuable time.
Keep learning and upskill with me.