Skip to main content

Command Palette

Search for a command to run...

Day 18 Task: Docker for DevOps Engineers

Published
3 min readView as Markdown

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.

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.

ls

cd projects

git clone already done.

ls

cd two-tier-flask-app

ls

Remove old docker-compose.yml file

rm docker-compose.yml

Create new -vim docker-compose.yml

Write a new docker-compose.yml file and save it :wq!

When you run a command "docker-compose up -d" it gives a error because docker compose not install.

docker compose installation command.

sudo snap install docker

sudo apt install docker-compose

After solving the error in "docker-compose.yml" file again run command "docker-compose up -d"

docker-compose up -d

Docker compose command successfully run and two tier flask web application created.

Task-2

1.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 siddharthgupt444/node-app:latest
docker pull siddharthgupt444/node:12.2.0-alpine

docker images

cd project

ls

cd node-todo-cicd

ls

docker build -t node-app .

docker run -d -p 8000:8000 node-app:latest

Output:

2.Inspect the container's running processes and exposed ports using the docker inspect command.

To inspect a running container's processes and exposed ports using the docker inspect command, you'll need to know the container's ID or name.

docker inspect --format '{{.State.Pid}}' 2a08fb992c72

This command will provide you with the Process ID (PID) of the container.

Inspect Exposed Ports:

docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{$conf}} {{end}}' 2a08fb992c72

3. Use the docker logs command to view the container's log output.

The docker logs command allows you to view the logs generated by a running container. You'll need the container's name or ID to access its logs.

Here's the basic syntax:

docker logs 2a08fb992c72

For example, to follow the logs of a container named "2a08fb992c72" in real-time, you'd use

docker logs -f 2a08fb992c72

4.Use the docker rm command to remove the container when you're done.

docker rm container id

How to run Docker commands without sudo?

1.Make sure docker is installed and system is updated.

2.Use this command=sudo usermod -a -G docker $USER

3.Now reboot the machine then your docker commands without sudo will work.

Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future😊😊

More from this blog

90daysofdevopschallenge

37 posts