Skip to content

Commit 08d1e2b

Browse files
committed
✨ (Docker) Use EXPOSE in Dockerfiles
To make it easier to use and read the Dockerfiles, add the `EXPOSE` command. This commit also adds a small description of what the command does.
1 parent b4c4591 commit 08d1e2b

File tree

2 files changed

+10
-5
lines changed
  • docs/docs/04_docker_intro/03_run_docker_container
  • project/02-first-rest-api-docker

2 files changed

+10
-5
lines changed

docs/docs/04_docker_intro/03_run_docker_container/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Inside the `Dockerfile` we're going to write this:
2424

2525
```dockerfile
2626
FROM python:3.10
27+
EXPOSE 5000
2728
WORKDIR /app
2829
RUN pip install flask
2930
COPY . .
@@ -33,10 +34,11 @@ CMD ["flask", "run", "--host", "0.0.0.0"]
3334
Here's a quick breakdown of what each line does:
3435

3536
1. `FROM python:3.10` uses the `python:3.10` image as a base.
36-
2. `WORKDIR /app` does it so everything we do in the Docker image will happen in the image's `/app` directory.
37-
3. `RUN pip install flask` runs a command in the image. Here the command is `pip install flask`, which is what we need to run our app.
38-
4. `COPY . .` is a bit cryptic! It copies everything in the current folder (so `app.py`) into the image's current folder (so `/app`).
39-
5. `CMD ["flask", "run", "--host", "0.0.0.0"]` tells the image what command to run when you start a container. Here the command is `flask run --host=0.0.0.0`.
37+
2. `EXPOSE 5000` is basically documentation[^1]. It tells the user of the Dockerfile that port 5000 is something the running container will use.
38+
3. `WORKDIR /app` does it so everything we do in the Docker image will happen in the image's `/app` directory.
39+
4. `RUN pip install flask` runs a command in the image. Here the command is `pip install flask`, which is what we need to run our app.
40+
5. `COPY . .` is a bit cryptic! It copies everything in the current folder (so `app.py`) into the image's current folder (so `/app`).
41+
6. `CMD ["flask", "run", "--host", "0.0.0.0"]` tells the image what command to run when you start a container. Here the command is `flask run --host=0.0.0.0`.
4042

4143
:::tip
4244
We need `--host=0.0.0.0` to make Docker be able to do port forwarding, as otherwise the Flask app will only be accessible within the container, but not outside the container.
@@ -120,4 +122,6 @@ docker run -dp 5001:5000 rest-apis-flask-python
120122

121123
Try making requests using the URL `127.0.0.1:5000` with Insomnia REST Client or Postman, and you should see it working well!
122124

123-
![Insomnia REST Client successfully made a request to the API running in Docker](assets/running-app-docker.png)
125+
![Insomnia REST Client successfully made a request to the API running in Docker](assets/running-app-docker.png)
126+
127+
[^1]: [Docker `EXPOSE` command (Official Documentation)](https://docs.docker.com/engine/reference/builder/#expose)

project/02-first-rest-api-docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
FROM python:3.10
2+
EXPOSE 5000
23
WORKDIR /app
34
RUN pip install flask
45
COPY . .

0 commit comments

Comments
 (0)