Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Denis Frunz
Denis Frunz
15,929 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
15,929 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

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).