#Day 18 - Docker For DevOps Engineers

Passionate AWS Developer | DevOps Engineer with a strong background in cloud architecture and solutions engineering. Leveraging the power of Amazon Web Services (AWS), knowledge of the AWS global infrastructure, design and implement robust cloud-based solutions that align with clients' specific needs.
What is Docker Compose?
Compose is a tool for defining and running multi-container docker applications.
It helps streamline the process of running complex applications by allowing you to define all necessary services, networks and volume in a single configuration file.
It works in all environments like production, staging, development, testing as well as CI workflow.
Some key features of Compose that make it effective are:
It has commands to manage the whole lifecycle of applications.
Start, stop, and rebuild services
View the status of running services
Stream the log output of running services
Run a one-off command on a service
It uses YAML file to define the services and with a single command can spin everything up and tear it all down
What is YAML?
YAML stands for Yaml Aint's markup language or Yet another markup language.
It is a popular programming language that is human-readable and easy to understand. It is a data serialization language that is often used for writing configuration files.
YAML is a widely used format for writing configuration files for different DevOps tools, programs, and applications
YAML uses indentation to define structure in the file, which is helpful if you are used to writing Python code and are familiar with the indentation style the language uses.
YAML files use a .yml or .yaml extension.
How to create a YAML file:
To create a YAML file, use either the
.yamlor.ymlfile extension.Before writing any YAML code, you can add three dashes (
---) at the start of the file.YAML allows you to have multiple YAML documents in a single YAML file, making file organization much easier.
Separate each document with three dashes (
---).To add a comment to comment out a line of code, use the
#character.YAML doesn't use symbols such as curly braces, square brackets, or opening or closing tags - just indentation.
YAML doesn't allow you to use any tabs when creating indentation - use spaces instead.
Whitespace doesn't matter as long as child elements are indented inside the parent element.
Sample YAML file:
---
Employees:
- name: John Doe
department: Engineering
country: USA
- name: Kate Kateson
department: IT support
country: United Kingdom
---
Fruit:
- Oranges
- Pears
- Apples
Task 01:
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.
First of all, Install Docker-compose as a stand-alone in Ubuntu. Command to install Docker compose.
sudo apt-get update
sudo apt-get install docker-compose


After installing docker-compose, verify the version by following the command:
docker-compose version

Now create a docker-compose.yaml file to set up the environment, configure the services and links between different containers, and also to use environment variables.
vim docker-compose.yaml

The first line:
version : "3.3"
This line specifies the version of the Docker Compose file format being used. In this case, it’s version 3.3.
services :
In the second line, two services sre defined: ‘web’ and ‘db’. Each service represents a containerized component of the application.
For the web service:
The
imagespecifies that thenginximage used for this service, with thelatesttag.The
portssection maps the host machine's port 80 to the container's port 80.
For the db service:
The
imagespecifies that themysqlimage is used for this service.The
portssection maps the host machine's port 3306 to the container's port 3306.The
environmentsection under thedbservice sets an environment variable for the MySQL container. In this case, it sets theMYSQL_ROOT_PASSWORDenvironment variable totest@123. This password will be used for the root user of the MySQL database.
Once the services are defined, run the following command to start the service.
docker-compose up


As we can see that docker container is created and is started.
Task: 02
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.
docker pull nginx
docker run -d -p 8080:8080 nginx


Run the container as non-root user:
sudousermod -aG docker ubuntu
sudo reboot

Inspect the container's running processes and exposed ports using the docker inspect command:
The
docker inspectcommand is used to retrieve detailed information about Docker objects such as containers, images, networks, and volumes.docker inspect <image-name> docker inspect nginx
It shows the output in JSON format as shown.
Use the docker logs command to view the container's log output:
docker logs <container-id>

- Use the docker stop and docker start commands to stop and start the container.
docker stop <container-id>
docker start <container-id>

Use the docker rm command to remove the container when you're done.
We cannot remove a running container. Hence, either we need to stop the container and then delete the container or perform the forceful removal of the container. Let’s forcefully remove the container:
docker rm -f <container-id>




