Table of contents
Docker Compose
Docker Compose simplifies managing multi-container applications. It uses a YAML file to define services, networks, and volumes. With a single command, it creates and manages the entire application stack. It simplifies the setup, orchestration, and management of containers, ensuring consistent and reproducible deployments across different environments.
or
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 human-readable data serialization format.
It uses indentation and plain text to represent structured data.
YAML is commonly used for configuration files.
It is easy for humans to write and understand.
YAML supports lists, dictionaries, and scalar values.
It allows the representation of complex data structures.
YAML is used in applications like Docker Compose and Kubernetes.
It is widely used for configuration files in various programming languages.
YAML's simplicity and readability make it popular for managing structured data.
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.yml
version : "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
"version: '3.3'": Specifies the version of Docker Compose being used.
"services": Starts the service definitions section.
"web": Defines the "web" service.
"image: nginx:latest": Specifies that the service should use the latest version of the NGINX container image from Docker Hub.
"ports": Maps ports between the host and container.
- "80:80": Exposes port 80 of the host machine to port 80 of the NGINX container.
"db": Defines the "db" service.
"image: mysql": Specifies that the service should use the MySQL container image from Docker Hub.
"ports": Maps ports between the host and container.
- "3306:3306": Exposes port 3306 of the host machine to port 3306 of the MySQL container.
"environment": Sets environment variables for the MySQL container.
- "MYSQL_ROOT_PASSWORD=test@123": Defines the root password for the MySQL instance as "test@123".
This Docker Compose file will create two containers: one running NGINX and another running MySQL. The NGINX container will be accessible on port 80 of the host machine, while the MySQL container will be accessible on port 3306.
Two commands are used in Docker compose
docker-compose down
docker-compose up -d
"docker-compose down": Stops and removes the containers, networks, and volumes defined in the Docker Compose file. It essentially shuts down the services defined in the Compose file and cleans up associated resources.
"docker-compose up -d": Starts the containers defined in the Docker Compose file in detached mode, meaning the containers run in the background. This command creates and starts the services defined in the Compose file, allowing them to operate independently.
These commands are often used together to gracefully stop and remove existing containers with "docker-compose down" and then start fresh containers in the background with "docker-compose up -d".
Task-2
- Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine.
--> To find images in DockerHub use the command
docker search nginx
--> Now download this image to your local machine with the command
docker pull nginx
--> Run the container as a non-root user. Make sure you reboot the instance after giving permission to the user.
sudo usermod -aG docker $USER
docker reboot
#(By running this command, the current user is granted permission to execute Docker commands without needing to use the "sudo" prefix each time.)
docker run -d nginx
#(-d --> detached mode, this will run ngnix in background)
- Inspect the container's running processes and exposed ports using the docker inspect command.
docker inspect d0c7ba7e7ab4 #(container ID)
To view exposed ports then scroll down and look
- Use the docker logs command to view the container's log output.
docker logs d0c7ba7e7ab4 #(container ID)
To get a limited no. of line logs add --tail in the above command or to get real-time logs generating add --follow in the above command.
- Use the docker stop and docker start commands to stop and start the container.
docker stop d0c7ba7e7ab4 #(will stop container)
docker start d0c7ba7e7ab4 #(will start container)
- Use the docker rm command to remove the container when you're done.
docker kill d0c7ba7e7ab4 #this command will kill the running container
docker rm d0c7ba7e7ab4 #this command remove the container
Thank you for reading. I hope you were able to understand and learn something new from my blog.
Happy Learning!
Please follow me on Hashnode and do connect with me on LinkedIn ArnavSingh