user17219965
user17219965

Reputation:

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?

Upvotes: 4

Views: 16026

Answers (2)

S.Sachith
S.Sachith

Reputation: 596

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

Upvotes: 5

Paolo
Paolo

Reputation: 26220

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.

Upvotes: 4

Related Questions