Development Tools Introduction to Docker Building Images Using Dockerfiles Building Images Using Dockerfiles

Denis Frunz
Denis Frunz
11,266 Points

Docker file EXPOSE port

Hi fellows,

Currently, I learn Docker by watching videos.During these videos the teacher run containers docker run -p 5000:5000 {image} as was said I have to expose a port ,but I found out that I can add EXPOSE 5000 to Dockerfile however I still must provide -p command and port to be able to connect to my flask app.What's more docker container runs with flask and shows which host and port I have to connect when I do that I fail and it runs only when I explicitly run -p argument and which port I want to expose.So EXPOSE command in Dockerfile doesn't work as I expected or I miss smth. Thank you.

Denis Frunz
Denis Frunz
11,266 Points

''

Base image

FROM python:3.7.2-alpine

Working directory

WORKDIR /usr/src/app

add and install requirements

COPY ./requirements.txt /usr/src/app/requirements.txt RUN pip install -r requirements.txt

add app

COPY . /usr/src/app

Make port available

EXPOSE 8080

ENTRYPOINT [ "python3" ]

Command to run when the container starts

CMD ["app.py"] '''

1 Answer

adrian miranda
adrian miranda
12,534 Points

The EXPOSE 8080 in your Docker file exposes it on the container, which is difficult to connect to directly. The -p argument on docker run, says that when you connect to particular port on your local system, you will be connected to a particular port on the container. So, yes, you will almost always want both (or something similar, anyway).