1

I have a Docker image webshop (containing a Python Flask web application) that only runs when executed with the additional option:

docker run -p 80:80 webshop

-p 80:80 maps port 80 in the host to port 80 inside the docker container. Without this mapping, the image doesn't execute correctly.

Is there a way to specify this mapping inside the Dockerfile, so I don't need to include this additional option every time I try to run the image? In particular, I want others to be able to execute this image without additional commands.

3 Answers3

2

No, and it wouldn't make any sense.

Your Dockerfile is a blueprint for how your environment should look like. It can't define things that are outside of its environment, for example, mapping ports on the host machine.

You can use EXPOSE as metadata in your Dockerfile. EXPOSE is for readability and tells the reader, what ports are being exposed from running your image. Again, it's strictly for readability purposes and doesn't actually do anything, but it's still a really good idea to have it.

But for host port mapping, that's done exclusively outside of the image itself and durning the runtime of your container.

alex067
  • 3,159
  • 1
  • 11
  • 17
0

You might want to use docker-compose.

jurez
  • 4,436
  • 2
  • 12
  • 20
0

As @jurez said, you should write a docker-compose.yml file and specify the execution configurations such as the port mappings inside that file, then you can use one command to run your container with the specified configs. Your docker-compose file will be something like this:

version: '3.8'
services:
  web:
    build: ./app
    ports:
      - 80:80

and you can run it with command:

docker-compose up -d --build
David Maze
  • 130,717
  • 29
  • 175
  • 215
Erfan Saberi
  • 134
  • 8