Table of contents
Till now we have created a Docker file and pushed it to the Repository Day 17. Let's move forward and dig more into other Docker concepts
Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications using a simple YAML file. 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. Compose making it easier to orchestrate and manage complex applications that consist of multiple interconnected containers.
What is YAML?
YAML is a human-readable 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 files use a .yml or .yaml extension, and follow specific syntax rules.
YAML is designed to be easy for both humans to read and write, as well as for machines to parse and generate.
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.
n this example, let's consider setting up a simple web application that consists of a web server and a database.
Create a Project Directory: Start by creating a new directory for your project. Inside this directory, you'll create your
docker-compose.yml
file and any other necessary files.Write the
docker-compose.yml
File:
Here's an example docker-compose.yml
file for setting up a web application with a web server (using Nginx) and a database (using MySQL):
version: '3' # Version of the Docker Compose syntax
services: # services that we want to create
webserver:
image: nginx:latest # Docker image for the Nginx web server
ports:
- "80:80" # Map host port 80 to container port 80
database:
image: mysql:latest # Docker image for the MySQL database
environment: # environment variables of container
MYSQL_ROOT_PASSWORD: rootpass
- Start Containers: Open a terminal, navigate to your project directory containing the
docker-compose.yml
file, and run the following command:
docker-compose up
## To check the containers
docker-compose ps -a
## Verify the nginx service in browser - http://54.196.80.186:80
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- Use
usermod
command to give user permission to docker). Make sure you reboot instance after giving permission to user.
Inspect the container's running processes and exposed ports using 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
Use the docker rm command to remove the container when you're done.
Pulling and Running a Docker Image:
Let's start by pulling and running an example image from Docker Hub. We'll use the official Nginx image for this example.
# Pull the Nginx image from Docker Hub
docker pull nginx
# Run the container as a non-root user
docker run -d --name my-nginx -p 8080:80 nginx
## Run the container and verify nginx default page in browser
Setting User Permissions and Rebooting:
Since Docker requires root privileges to run containers, you'll need to add your user to the docker
group to allow them to run Docker commands without using sudo
. After adding your user to the group, you should reboot the system for the changes to take effect.
# Add your user to the 'docker' group
sudo usermod -aG docker your_username
# Reboot the instance
sudo reboot
Now you can see in the above SS I don't have to use sudo
Inspecting Container Details:
You can use the docker inspect
command to get detailed information about the container, including its running processes and exposed ports.
# Get detailed information about the container
docker inspect my-nginx
Viewing Container Logs:
The docker logs
command allows you to view the log output of a running container.
# View the logs of the container
docker logs my-nginx
Stopping and Starting the Container:
You can use the docker stop
and docker start
commands to stop and start the container, respectively.
# Stop the container
docker stop my-nginx
# Start the container
docker start my-nginx
Removing the Container:
When you're done experimenting, you can remove the container using the docker rm
command.
# Remove the container
docker rm my-nginx
Happy Learning!