Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the port to map to on the host machine when you build the image wiith Dockerfile

When I run Docker from command line I do the following:

docker run -it -d --rm --hostname rabbit1 --name rabbit1 -p 127.0.0.1:8000:5672 -p 127.0.0.1:8001:15672 rabbitmq:3-management

I publish the ports with -p in order to see the connection on the host. How can I do this automatically with a Dockerfile?


2 Answers

You cant specify ports in Dockerfile but you can use docker-compose to achieve that.

Docker Compose is a tool for running multi-container applications on Docker.

example for docker-compose.yml with ports:

version: "3.8"
services :
  rabbit1:
    image : mongo
    container_name : rabbitmq:3-management
    ports: 
      - 8000:5672
      - 8001:15672
like image 172
S.Sachith Avatar answered Nov 02 '25 13:11

S.Sachith


The Dockerfile provides the instructions used to build the docker image.

The docker run command provides instructions used to run a container from a docker image.

How can I do this automatically with a Dockerfile

You don't.

Port publishing is something you configure only when starting a container.

like image 40
Paolo Avatar answered Nov 02 '25 11:11

Paolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!