DEV Community

Uzeyr OZ
Uzeyr OZ

Posted on

1

How to create a docker-compose.yml file for PostgreSQL and RabbitMQ

Step 1: Install Docker and Docker Compose
Make sure you have Docker and Docker Compose installed on your machine. If you don't, you can download and install them from the Docker website.

Step 2: Create a new directory
Create a new directory for your project and navigate to it in your terminal.

$ mkdir myproject
$ cd myproject

Enter fullscreen mode Exit fullscreen mode

Step 3: Create a new docker-compose.yml file
Create a new file called docker-compose.yml in your project directory.

$ touch docker-compose.yml

Enter fullscreen mode Exit fullscreen mode

Step 4: Define services in the docker-compose.yml file
Add the following code to the docker-compose.yml file to define the services for PostgreSQL and RabbitMQ.

version: '3'

services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydatabase

  rabbitmq:
    image: rabbitmq
    restart: always
    ports:
      # AMQP protocol port
      - "5672:5672"
      # HTTP management UI
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: myuser
      RABBITMQ_DEFAULT_PASS: mypassword

Enter fullscreen mode Exit fullscreen mode

In the above code, we define two services: db for PostgreSQL and rabbitmq for RabbitMQ. We use the official Docker images for both services, and we set some environment variables to configure them. You can change the values of these variables to suit your needs.

Step 5: Run the services with Docker Compose
Save the docker-compose.yml file and run the services with Docker Compose.

$ docker-compose up -d

Enter fullscreen mode Exit fullscreen mode

The -d flag runs the services in the background. You should see output similar to the following:

Creating network "myproject_default" with the default driver
Creating myproject_db_1 ... done
Creating myproject_rabbitmq_1 ... done

Enter fullscreen mode Exit fullscreen mode

Step 6: Verify the services
You can now verify that the services are running by checking the logs or connecting to them. For example, to connect to PostgreSQL:

$ docker exec -it myproject_db_1 psql -U myuser mydatabase

Enter fullscreen mode Exit fullscreen mode

And to connect to RabbitMQ:

$ docker exec -it myproject_rabbitmq_1 rabbitmqctl list_users

Enter fullscreen mode Exit fullscreen mode

That's it! You now have a docker-compose.yml file for PostgreSQL and RabbitMQ. You can add more services to the file as needed, and run them all with a single command using Docker Compose.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay