Frmatt
Frmatt

Reputation: 21

Connect java application in docker container to rabbitmq

I have a Java application running in a Docker container and rabbitmq in another container.

How can I connect the containers to use rabbitmq in my Java application?

Upvotes: 1

Views: 1737

Answers (1)

zlaval
zlaval

Reputation: 2027

You have to set up a network and attach the running containers to the network.

Then you have to set the connection URL of your app to the name of the rabbitmq's network name in Docker container.

The easiest way is to create docker-compose file because it will create the network and attach the containers automatically.

Create a network

Connect the container

Or

Docker compose file

Example of docker-compose.yml

    version: '3.7'
    services:
      yourapp:
        image: image_from_dockerhub_or_local // or use "build: ./myapp_folder_below_this_where_is_the_Dockerfile" to build container from scratch
        hostname: myapp
        ports:
          - 8080:8080
      rabbitmq:
        image: rabbitmq:3.8.3-management-alpine
        hostname: rabbitmq
        environment:
          RABBITMQ_DEFAULT_USER: user
          RABBITMQ_DEFAULT_PASS: pass
        ports:
          - 5672:5672
          - 15672:15672

You can run it with docker-compose up command.

Then in your connection url use host:rabbitmq, port:5672.

Note that you don't have to create a port forward if you don't want to reach rabbitmq from your host machine.

Upvotes: 2

Related Questions