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
Answers
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.
You might want to use docker-compose.
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 (not exactly this, you might need to remove the command part because it’s already defined in your image, I’m not sure):
and you can run it with command: