Table of contents
- Docker Compose
- Docker Compose
- What is YAML?
- Task-1
- 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 (Hint- Useusermodcommand to give the user permission to docker). Make sure you reboot the instance after permitting the user.
- Inspect the container's running processes and exposed ports using thedocker inspectcommand.
- Use thedocker logscommand to view the container's log output.
- Use thedocker stopanddocker startcommands to stop and start the container.
- Use thedocker rmanddocker killcommands to remove the container when you're done.
- How to run Docker commands without sudo?
Till now you have created Docker file and pushed it to the Repository. Let's move forward and dig more on other Docker concepts. Today, let's spend some time studying on Docker Compose 😃
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.
To learn more about docker-compose visit here
Docker Compose
- Docker Compose is another best tool for docker to setup multi-container environments. Using this create a single compose file with defining all the containers with there environments. You can easily use single command to build images and run all the containers.
There is the three-steps process to work with Docker Compose.
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.
You must have Docker Engine installed on your system. If you don’t have already installed, you can install using this command.
sudo apt-get install docker.io -y
sudo apt-get install docker-compose -y
Example docker-compose.yml
file of nginx
# vim docker-compose.yml
-------------------------------------------------------------------------------
version : "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
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.To read more about YAML click on it here
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.
# vim docker-compose.yml
-------------------------------------------------------------------------------
version: "3.3" # Specifies the version of the Docker Compose file format being used.
services: # Defines the services to be deployed.
web: # Service named 'web'.
image: nginx:latest # Specifies the Docker image to be used for the 'web' service (in this case, the latest version of the Nginx web server).
ports:
- "80:80" # Maps port 80 of the host to port 80 of the 'web' service container. Allows access to the web server running inside the container.
db: # Service named 'db'.
image: mysql # Specifies the Docker image to be used for the 'db' service (in this case, the latest version of MySQL).
ports:
- "3306:3306" # Maps port 3306 of the host to port 3306 of the 'db' service container. Allows access to the MySQL database running inside the container.
environment:
- "MYSQL_ROOT_PASSWORD=test@123" # Sets the environment variable MYSQL_ROOT_PASSWORD to 'test@123' for the MySQL container. This defines the root password for the MySQL database.
The
web
service uses the Nginx image and exposes port 80 of the container to port 80 of the host machine.The
db
service uses the MySQL image and exposes port 3306 of the container to port 3306 of the host machine. It also sets the root password for the MySQL database using theMYSQL_ROOT_PASSWORD
environment variable.The docker-compose build and docker-compose run commands are replaced by this one. If the images are not already present locally, it creates them and launches the containers
docker-compose up -d
docker ps
docker-compose ps
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 (Hint- Useusermod
command to give the user permission to docker). Make sure you reboot the instance after permitting the user.
sudo usermod -a -G docker $USER && sudo reboot
docker pull nginx:latest
- run the nginx container
docker run -d --name nginx -p 80:80 nginx:latest
docker ps && docker images
Inspect the container's running processes and exposed ports using thedocker inspect
command.
docker inspect <container_id>
Use thedocker logs
command to view the container's log output.
docker logs <container_id>
Use thedocker stop
anddocker start
commands to stop and start the container.
docker stop <container_id>
docker start <container_id>
Use thedocker rm
anddocker kill
commands to remove the container when you're done.
docker kill <container_id>
docker rm <container_id>
How to run Docker commands without sudo?
Make sure docker is installed and system is updated (This is already been completed as a part of previous tasks):
sudo usermod -a -G docker $USER
Reboot the machine using
sudo reboot
.
Happy Learning
Thanks For Reading! :)
-DevOpsParthu💝💥