Docker-Compose Cheat Sheet

 

Docker-Compose is an indispensable tool for managing multi-container Docker applications. With a simple YAML file, you can define, configure, and deploy all the services your application needs. This cheat sheet covers the essential commands, tips, and examples to help you master Docker-Compose, and taking a DevOps course can further enhance your understanding and proficiency with this tool.

 

What is Docker-Compose?

Docker-Compose lets you define a multi-container application with all its dependencies in a single `docker-compose.yml` file. This file can then be used to create and start all the services with a single command. It's particularly useful for managing microservices, testing environments, and development setups.

 

Basic Structure of docker-compose.yml

A `docker-compose.yml` file typically contains three main sections: `services`, `networks`, and `volumes`. Here’s a basic example:


Explanation

- version: Specifies the version of the Docker-Compose file format.

- services: Lists the containers to be run.

  • web: A service named 'web' that uses the latest Nginx image and maps port 80 on the host to port 80 in the container.
  • db: A service named 'db' that uses the latest PostgreSQL image and sets an environment variable for the PostgreSQL password.

 

Essential Docker-Compose Commands

 Here are some of the most commonly used Docker-Compose commands:

 `docker-compose up`

 Builds, recreates, starts, and attaches to containers defined in the Compose file.

docker-compose up                                                    

`docker-compose down`

Stops and removes resources created by docker-compose up, including containers, networks, and volumes.

docker-compose down                                                  

`docker-compose build`

Builds or rebuilds services defined in the `docker-compose.yml` file

docker-compose build                                                 

`docker-compose start`

Starts existing containers for a service.

docker-compose start                                                 

`docker-compose stop`

 Stops running containers without removing them.

docker-compose stop                                                  

`docker-compose ps`

Lists containers for a service.

docker-compose ps                                                    

`docker-compose logs` 

Views output from containers.

docker-compose logs                                                  


Advanced Docker-Compose Features

Volumes

Docker volumes provide persistent storage for data created and used by containers.


Networks 

Define custom networks for your services to connect to each other.




Environment Variables

You can define environment variables directly in the `docker-compose.yml` file or in an `.env` file.

 Using docker-compose.yml



Using .env file

Create a `.env` file:

POSTGRES_USER=user                                                   
POSTGRES_PASSWORD=pass                                               

Then, reference it in your docker-compose.yml:




Docker-Compose Best Practices

Use .env Files for Configuration

Storing configuration in `.env` files keeps your `docker-compose.yml` clean and allows you to easily switch between different environments.

Keep Services Stateless

Ensure your services are stateless and use volumes or external storage for persistence. This allows for easier scaling and redeployment.

Version Control Your docker-compose.yml

Always version control your `docker-compose.yml` file to track changes and collaborate with your team.

Use Docker Networks

Utilize Docker networks to isolate and secure your services. By default, Docker-Compose creates a network for your services, but you can also define custom networks for more control.

Keep Images Updated

Regularly update your Docker images to incorporate the latest security patches and features. Use specific tags (e.g., `nginx:1.19`) rather than `latest` to ensure consistency across deployments.


Conclusion

Docker-Compose is a powerful tool that can significantly simplify the management of multi-container applications. By understanding its basic structure, essential commands, and advanced features, you can streamline your development and deployment processes. This cheat sheet provides a quick reference to get you started and keep you productive with Docker-Compose. Incorporating these practices, often emphasized in DevOps training, will ensure you maximize efficiency and effectiveness. Happy containerizing!

For more: Essential Docker Commands - Cheat Sheet


Comments

Popular posts from this blog

Docker Basic Commands Cheat Sheet

How to Install JUnit in Eclipse