Table of contents
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.
Learn more about docker-compose
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.
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 allows you to define and run multi-container Docker applications. It uses a YAML file called docker-compose.yml to configure the services that make up your application.
example 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"
This file defines two services: a web service and a db service. The web service is built from the nginx image and exposes port 80:80. It also sets environment variables, web applications running in the container.
The db service uses the official mysql image from Docker Hub and runs the container.
To run this configuration, save the file as
docker-compose.yml
in your project directory and run the following command:docker-compose up
Task-2
Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine.
Use the docker search command to find images on the docker hub
docker search nginx
Download images from docker hub
docker pull nginx
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 giving permission to the userdocker usermod -aG docker $USER docker reboot
docker run nginx
Inspect the container's running processes and exposed ports using the docker inspect command.
docker ps #You can list all running containers by running this command docker inspect f62f2c9ec6d1 # This section contains a list of all processes running inside the container, along with their PID, name, and other details.
To view the container's exposed ports, you can look for the
ExposedPorts
section in the output of thedocker inspect
command.Use the docker logs command to view the container's log output.
docker ps docker logs f62f2c9ec6d1 #command to view its log output
By default, the
docker logs
command shows the last 10 lines of the container's log output. You can specify a different number of lines to show using the--tail
option.docker logs --tail 5 f62f2c9ec6d1
You can also follow the container's log output in real-time using the
--follow
or-f
option. This will continuously output new log messages as they are generated by the container.docker logs --follow f62f2c9ec6d1
Use the docker stop and docker start commands to stop and start the container.
docker stop f62f2c9ec6d1 #stop container docker start f62f2c9ec6d1 #start container
Use the docker rm command to remove the container when you're done.
docker rm f62f2c9ec6d1
Thankyou for reading my blog...........................