Intro to Docker Compose
In this session we will learn the basics of Docker Compose and how to manage multi-container applications easily.
What are Multi-Container Applications?
Modern cloud-native applications are often composed of multiple smaller services that work together to form a complete application.
This is known as the microservices pattern. These could include:
Web front-end
Database
Authentication service
and so on.
Challenges with Microservices:
Managing and deploying multiple microservices can be complex and cumbersome, requiring careful orchestration of each service.
What is Docker Compose?
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a declarative configuration file, typically in YAML format, to specify the services, networks, and volumes required for the application.
Benefits of Docker Compose:
Simplifies the orchestration of multi-container applications.
Allows for a single configuration file to define and manage all services.
Integrates with version control systems for better management.
Basic Docker Compose Commands
Check Installation:
docker compose version
Start an Application:
docker compose up
Stop an Application:
docker compose down
View Container Status:
docker compose ps
Simple Docker Compose Example
Let's create a basic Docker Compose setup with a web server using nginx:alpine image and a redis service using redis:alpine image.
Step 1: Create a Directory
Create a directory for your project.
mkdir myapp
cd myapp
Step 2: Create Docker Compose File
Create a file named docker-compose.yml
and add the following content:
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"
redis:
image: redis:alpine
This file defines two services: a web server (nginx) and a Redis server.
Step 3: Start the Application
Run the following command to start the services:
docker compose up
You should see output indicating the services are starting.
Expected output:
Step 4: Verify the Setup
Check localhost:
curl localhost
You should see the default Nginx welcome page.
Expected output:
Step 5: View Status
Check the status of the running containers:
docker compose ps
#or
docker ps
Expected output:
Step 6: Stop the Application
Stop the services with:
docker compose down
=====####====
Let's discuss Another 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. Usually 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.
Conclusion
You have learned the basics of Docker Compose, created a simple multi-container application, and practiced using basic commands. This knowledge will help you manage more complex applications in the future.
Top comments (0)