Level Up Your Development Experience with Docker Compose
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
dbservice 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.
5432on the host machine is mapped to port
dbservice container, allowing us to access the PostgreSQL database externally.
Environment variables are set to configure the PostgreSQL instance.
redisservice is based on the latest Redis image. It also restarts automatically, and port
6379on the host machine is mapped to port
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:
Similarly, you can connect to the Redis cache using
localhost and port
To verify connectivity, you can use the following URLs:
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!