Docker has revolutionized the way developers build, ship, and run applications. It provides a lightweight and portable environment for isolating applications and their dependencies. One of the powerful tools in the Docker ecosystem is Docker Compose, which allows you to define and manage multi-container applications. In this blog post, we will explore how to use Docker Compose for development, focusing on a simple example with a PostgreSQL database and a Redis cache.
I have been working with these tricks to avoid problems like slow connection to remote databases, clean install, forgetting the passwords etc. Let's start!
Make sure you have Docker and Docker Compose installed on your machine.
$ docker -v
> Docker version 24.0.2, build cb74dfcd85
$ docker-compose -v
> Docker Compose version v2.12.1
Docker Compose comes bundled with Docker Desktop for Windows and macOS, while Linux users can install it separately. Once you have everything set up, create a new directory for your project and navigate into it.
Defining the Docker Compose File: In your project directory, create a file called docker-compose.yml
and open it in a text editor. This file will contain the configuration for your Docker Compose environment. Start by specifying the version at the top of the file:
Let's define the services we want to run using Docker Compose. In our example, we'll have two services: a PostgreSQL database and a Redis cache. Add the following code to your docker-compose.yml
file:
version: '3'
services:
db:
image: postgres:latest
restart: always
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
redis:
image: redis:latest
restart: always
ports:
- "6379:6379"
Explanation:
The
db
service is based on the latest PostgreSQL image. We specify that it should always restart and mount a local directory (./postgres-data
) as the data volume for persistent storage.Port
5432
on the host machine is mapped to port5432
on thedb
service container, allowing us to access the PostgreSQL database externally.Environment variables are set to configure the PostgreSQL instance.
The
redis
service is based on the latest Redis image. It also restarts automatically, and port6379
on the host machine is mapped to port6379
on theredis
service container.
Save the docker-compose.yml
file and return to your terminal. To start the defined services, use the following command in your project directory:
$ docker-compose up
$ # docker-compose up --build -d
Docker Compose will download the necessary images (if not already present) and start the containers. You'll see the output from each service, and you can monitor their logs in real-time.
Accessing the Services: With the services up and running, you can access them from your development environment. For example, you can connect to the PostgreSQL database using your preferred database client and the following connection details:
Host:
localhost
Port:
5432
Username:
postgres
Password:
postgres
Similarly, you can connect to the Redis cache using localhost
and port 6379
.
To verify connectivity, you can use the following URLs:
Redis:
redis://localhost:6379
PostgreSQL:
postgresql://postgres:
postgres@localhost:5432/postgres
These URLs can be directly used in your application code or database clients to establish connections with the Redis and PostgreSQL databases. Make sure that the Docker Compose services are running, and then test the connections. If you encounter any connection errors, double-check the URLs and ensure that the services are running correctly. Verifying connectivity at this stage will help ensure smooth development without any connectivity issues.
Docker Compose is a powerful tool for managing multi-container applications in a development environment. It simplifies the setup and configuration of complex application stacks, allowing developers to focus on building their applications rather than managing dependencies. In this blog post, we explored a simple example with a PostgreSQL database and a Redis cache, but Docker Compose can be used for a wide range of application architectures.
Take care of your health!