Docker Compose for DevOps Engineer

Unnati GuptaUnnati Gupta
5 min read

#90daysdevopschallenge

#day18

If you don't have knowledge about docker. Please learn first docker.

Go through this article to learn docker:

https://devunnatig.hashnode.dev/docker

Docker Compose:

Docker Compose is a tool for defining & running multi-container applications.

Let's take an example of microservices, which have an application, that provides multiple services like Account Service, Product Catalog, and Order Service. So for these types of applications, use docker-compose to create multiple containers.

By using a single command you can run a multicontainer application.

In docker-compose used YAML files to configure services of applications that called docker-compose.yaml.

Compose works in all environments; production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:

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

Key Features of Docker Compose:

a) Docker Compose provides the facility to create multiple isolated environments on a single host by using a project name only.

b) The default project name is the base name of the project directory. You can set a custom name by using the -p command line option.

c) The default project directory is the base directory of the compose file. A custom value for it can be defined with the --project-directory command line option.

d) Preserves volume data when containers are created. It means when you run the docker-compose up command if any containers are from previous runs, it copies the volumes from the old container to the new container. So you never lost your data which was previously created. Docker Compose caches the configuration used to create containers. when you restart any services that have not changed, it reuses the existing container. Don't create again and again.

e) Docker composes support variables. so it's good to customize our compose file according to the environment. You can extend your compose file by using the extend function in docker-compose.

Docker Compose Commands:

a) docker-compose up: Used to start the docker container defined in docker-compose.yaml file.

Syntax: docker-compose up

b)docker-compose up -d: Used to start container in detached mode(run container in the background.

Syntax: docker-compose up -d

c) docker-compose down: Used to stop and remove the container, networks, and volumes defined in the docker-compose.yml.

Syntax: docker-compose down

d) docker-compose ps: It provides a list of the containers associated with your project, along with their status.

Syntax: docker-compose ps

e) docker-compose logs: Display the logs of the container which defined in docker-compose.yaml file.

Syntax: docker-compose logs

f) docker-compose exec: Runs a command within a specific service's container.

Syntax: docker-compose exec web app bash

g) docker-compose build: Builds or rebuilds the docker images specified in the docker-compose.yml file.

Syntax: docker-compose build

h) docker-compose pull: Pull the latest image for the service defined in the docker-compose.yml file without starting containers.

Syntax: docker-compose pull

i) docker-compose up --build: Starts container after rebuilding images, useful when you have made changes to your docker file.

Syntax: docker-compose up --build

j) docker-compose restart: Restart specific service within the defined service.

Syntax: docker-compose restart <service_name>

k) docker-compose pause <service_name> & docker-compose unpause <service_name>: pause and unpause service name is temporarily stopping its execution.

Syntax: Pause: docker-compose pause worker

Unpause: docker-compose unpause worker

l) docker-compose stop: Stop the containers without removing them.

Syntax: docker-compose stop

m) docker-compose rm: Remove stopped containers defined in the docker-compose.yml file.

Syntax: docker-compose stop

n) docker-compose scale =: scales: A specific service to run a specified number of instances.

Syntax: docker-compose scale web=3

o) docker-compose down -v: Stops and removes containers, networks, and volumes defined in the docker-compose.yml file.

Syntax: docker-compose down -v

p) docker-compose config: To validate the docker-compose file use this command.

Syntax: docker-compose config

What is YAML?

YAML stands out as Yet Another Markup Language. It is a data serialization language that is used to write configuration files. for creating a YAML file, the extension is .yml and .yaml.

If you want to study YAMl in deep, go through this article https://devunnatig.hashnode.dev/python-libraries-for-devops

โ„Tasks:

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.

Install docker-compose:

apt-get install docker-compose

Check version:

docker-compose -v

write docker-compose.yml:

check nginx service:

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 the instance after giving permission to the user.

How to run Docker commands without sudo?

  • Make sure docker is installed and the system is updated (This is already been completed as a part of previous tasks):

  • sudo usermod -a -G docker $USER

  • Reboot the machine.

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

expose port:

check on browser:

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

5.Use the docker stop and docker start commands to stop and start the container.

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

In the Next Article, we will explore Docker-Volume......

Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning !!!

0
Subscribe to my newsletter

Read articles from Unnati Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Unnati Gupta
Unnati Gupta

๐Ÿ‘จโ€๐Ÿ’ป DevOps Engineer at 6D Technology Passionate about bridging the gap between development and operations, I'm a dedicated DevOps Engineer at 6D Technology. With a strong belief in the power of automation, continuous integration, and continuous delivery, I thrive in optimizing software development pipelines for efficiency and reliability. ๐Ÿš€ Exploring the DevOps Universe In my articles, I delve into the fascinating world of DevOps, where I share insights, best practices, and real-world experiences. From containerization and orchestration to CI/CD pipelines and infrastructure as code, I'm here to demystify the complex and empower fellow developers and ops enthusiasts. ๐Ÿ“ Blogging for Knowledge Sharing As a tech enthusiast and a lifelong learner, I'm committed to sharing knowledge. My articles aim to simplify complex concepts and provide practical tips that help teams and individuals streamline their software delivery processes. ๐ŸŒ Connect with Me Let's connect and explore the ever-evolving landscape of DevOps together. Feel free to reach out, comment, or share your thoughts on my articles. Together, we can foster a culture of collaboration and innovation in the DevOps community. ๐Ÿ”— Social Links LinkedIn: https://www.linkedin.com/in/unnati-gupta-%F0%9F%87%AE%F0%9F%87%B3-a62563183/ GitHub: https://github.com/DevUnnati ๐Ÿ“ฉ Contact Have questions or looking to collaborate? You can reach me at unnatigupta527@gmail.com Happy Learning!!