Introduction to Docker Compose
Hello, folks! You're looking gorgeous today. As you know, I'm from Istanbul, and I'm currently sitting in a beautiful coffee shop near Kadıköy. While taking a break, I decided to continue the Docker Introduction series.
By this point, you should have a good understanding of Docker concepts. If you're new here or new to Docker, you can check out my Introduction to Docker post.
Today, we're diving into Docker Compose. Simply put, it's a tool provided by Docker to create and manage multiple containers. Let's look at the textbook definition and how they explain it:
Docker Compose is a tool for defining and running multi-container Docker applications. It uses YAML files to configure the application's services, networks, and volumes, allowing you to define the relationships between containers and their configurations in a single file.
It's pretty obvious that Docker Compose is essential when it comes to using services together. Let's say you're building your startup (we're all doing it, my fellow entrepreneurs) and you should have a tech stack.
For example, you might need MongoDB for the database, Redis for caching, Node.js for the API, and RabbitMQ for event distribution. As you can see, we have a lot of tools and projects for our startup, and this is called "infrastructure". In order to develop our application, we must run them with a certain configuration. Sometimes we add some configuration or new tools to our infrastructure, or sometimes we share the code with another colleague.
By now, you realize how challenging these tasks can be. And that's not all; imagine that you want to deploy your project and scale it. Houston, we have a lot of problems. But there's one thing that can easily solve all these problems, and that's Docker Compose.
The Example
Let's investigate docker-compose.yml to understand.
version: '3.5'
services:
api:
build: .
volumes:
- "./app:/src/app"
ports:
- "1338:1338"
depends_on:
- db
- cache
networks:
- test_nw
environment:
- DATABASE_HOST=mongodb://db:27017
- REDIS_CACHE_HOST=redis://cache:6379
- PORT=1338
db:
image: mongo:latest
ports:
- "27017:27017"
networks:
- test_nw
cache:
image: redis:latest
ports:
- "6379:6379"
networks:
- test_nw
networks:
test_nw:
driver: bridge
This is a simple docker-compse.yml
example to deploy a Nodejs backend with MongoDb and Redis.
The Commands
Of course docker-compose has commands. Whaat! Okay I' will give you the most basic commands. I'm not super duper software engineer and I can live my life with these commands.
docker compose up
- is a command that will look for
docker-compose.yml
by default and will process the docker-compose.yml, create the environment and run the services. -d
means that terminal is yours, it runs the command detachable mode-f #non-standard-compose.yml-name#
means that you can pass a compose.yml file with different name. Usaually projects contains more than one docker-compose files. You can have compose file for production and development or you can seperate applications and tools in a different compose files.
docker compose down
- is a command that will look for running compose.yml file and shutdown containers then remove all of them including networks, volumes etc.
docker compose log
- is a command that will look for running compose.yml file and displays log which are generated by the containers.
Top Level Definitions
version
: Defined by the Compose Specification for backward compatibility. It is only informative, and you'll receive a warning message that it is obsolete if used.
services
: A service is an abstract definition of a computing resource within an application which can be scaled or replaced independently from other components. These services run in their containers and can communicate with each other.
network
: A layer that allows containers to communicate with each other.
volume
: Volumes are persistent data stores implemented by the container engine. Compose offers a neutral way for services to mount volumes, and configuration parameters to allocate them to infrastructure.
Service Definitions
build
: Lets you define the Dockerfile path to build when the compose file is being processed.
volumes
: Lets you define service-level persisted volumes to mount local files and folders.
ports
: Lets you expose container ports. The left-hand of the definition is the localhost address and the right-hand will be the container port. It basically means binding port 1338 of the container to localhost:1338.
depends_on
: option in Docker Compose is used to specify the dependencies between different services defined in your docker-compose.yml
file.
networks
: option in Docker Compose is used to specify which network will be used by this container.
environment
: option in Docker Compose is used to specify environment variables which will be passed to container.
The End
In conclusion, Docker Compose is an indispensable tool for simplifying the development, deployment, and scaling of multi-container Docker applications. By leveraging its capabilities to define services, networks, and volumes in a single YAML file, developers can streamline the setup and management of complex application infrastructures.
Whether you're a seasoned developer or just starting with Docker, mastering Docker Compose opens up a world of possibilities for building robust and scalable applications. So, the next time you're faced with the challenge of orchestrating multiple services in your Docker environment, remember the power of Docker Compose to simplify and streamline your workflow.
I hope this introduction to Docker Compose has been helpful and informative. Stay tuned for more insights and tutorials in our Docker Introduction series.
Happy coding!