Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker rabbitmq how to expose port and reuse container with a docker file

Hi I am finding it very confusing how I can create a docker file that would run a rabbitmq container, where I can expose the port so I can navigate to the management console via localhost and a port number.

I see someone has provided this dockerfile example, but unsure how to run it?

version: "3"
services:
 rabbitmq:
    image: "rabbitmq:3-management"
    ports:
      - "5672:5672"
      - "15672:15672"
     volumes:
      - "rabbitmq_data:/data"
     volumes:
      rabbitmq_data:

I have got rabbit working locally fine, but everyone tells me docker is the future, at this rate I dont get it.

Does the above look like a valid way to run a rabbitmq container? where can I find a full understandable example?

  1. Do I need a docker file or am I misunderstanding it?
  2. How can I specify the port? in the example above what are first numbers 5672:5672 and what are the last ones?
  3. How can I be sure that when I run the container again, say after a machine restart that I get the same container?

Many thanks

Andrew

like image 584
Andrew Avatar asked Jul 29 '18 09:07

Andrew


1 Answers

Docker-compose

What you posted is not a Dockerfile. It is a docker-compose file.

To run that, you need to

1) Create a file called docker-compose.yml and paste the following inside:

version: "3"
services:
 rabbitmq:
  image: "rabbitmq:3-management"
  ports:
    - "5672:5672"
    - "15672:15672"
  volumes:
    - "rabbitmq_data:/data"
volumes:
  rabbitmq_data:

2) Download docker-compose (https://docs.docker.com/compose/install/)

3) (Re-)start Docker.

4) On a console run:

cd <location of docker-compose.yml>
docker-compose up

Do I need a docker file or am I misunderstanding it?

You have a docker-compose file. The rabbitmq:3-management is the Docker image built using the RabbitMQ Dockerfile (which you don't need. The image will be downloaded the first time you run docker-compose up.

How can I specify the port? In the example above what are the first numbers 5672:5672 and what are the last ones?

"5672:5672" specifies the port of the queue.

"15672:15672" specifies the port of the management plugin.

The numbers on the left-hand-side are the ports you can access from outside of the container. So, if you want to work with different ports, change the ones on the left. The right ones are defined internally.

This means you can access the management plugin after at http:\\localhost:15672 (or more generically http:\\<host-ip>:<port exposed linked to 15672>).

You can see more info on the RabbitMQ Image on the Docker Hub.

How can I be sure that when I rerun the container, say after a machine restart that I get the same container?

I assume you want the same container because you want to persist the data. You can use docker-compose stop restart your machine, then run docker-compose start. Then the same container is used. However, if the container is ever deleted you lose the data inside it.

That is why you are using Volumes. The data collected in your container gets also stored in your host machine. So, if you remove your container and start a new one, the data is still there because it was stored in the host machine.

like image 163
Bruno Lubascher Avatar answered Oct 30 '22 18:10

Bruno Lubascher