Guide: Containerize a Python application browser network connection lost

Hi,
Trying to learn Docker and starting from ground zero. I am using a M3 MacBookPro running macOS 15.3.1.

I am starting with the Guide “Containerize a Python application”. I successfully cloned the repository with the sample application, I then initialized the Docker assets and created the file named .gitignore. All seems to have been done correctly.

Next I run the application and open the Safari browser to view the application at http://localhost:8000. Safari tells me that it can’t open the page because the server unexpectedly dropped the connection. I have tried this with Firefox and Orion. Orion tells me the network connection was lost and Firefox tells me the connection was reset.

Havein’t found anything in the forums yet that hits the mark, will keep looking.

1 Like

Please, show how you start the container. Localhost can work only if you forward ports to the container.

Also please, share the link of the guide you follow if there is a link.

First, here is the link to the guide. Docker guide link

  1. I cloned (using Tower) the sample application from the repository at GitHub - estebanx64/python-docker-example
  2. I initialized the assets by running the command docker init from the directory created when I cloned the sample application.
  3. I created the file .gitignore and populated it with the content included in the guide.
  4. I started the application using the command docker compose up --build

The only step I see that includes ports is when I initialized the assets.

Thanks!

What’s in your Dockerfile and Docker compose file?

Check the container logs.

docker compose logs

In another terminal of course if you followed the guide to start the container, but you can do it from the GUI as well.

I have the same MacOS version, and it worked for me, so I see three possible causes:

  • You either missed something and the Dockerfile that @bluepuma77 asked for can be a good first step to check
  • Or the Docker image is not compatible with the CPU of the M3 CPU. Docker is running in a virtual machine, but I we have seen reports about issues with the new CPUs
  • You are using an older Docker Desktop and you should update it

Intended to upload a text file but I am not permitted. So now you get the content in markdown, sorry. I also had to take all of the links and alter them and I did this by adding spaces to https so https becomes h t t p s.

  • Let me know if I missed something. I used the information precisely as I saw it in the guide.
  • How would I determine if the Docker image is not compatible with the M3?
  • My Docker Desktop is the most current version.

Here are the contents of the files:

  • compose.yaml (assume is what @bluepuma77 asked for as the Docker compose file)
# If you need more help, visit the Docker Compose reference guide at
# https://docs.docker.com/go/compose-spec-reference/
# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
  server:
    build:
      context: .
    ports:
      - 8000:8000

# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the
[details="Summary"]
This text will be hidden
[/details]
 database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker compose up`.
#     depends_on:
#       db:
#         condition: service_healthy
#   db:
#     image: postgres
#     restart: always
#     user: postgres
#     secrets:
#       - db-password
#     volumes:
#       - db-data:/var/lib/postgresql/data
#     environment:
#       - POSTGRES_DB=example
#       - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
#     expose:
#       - 5432
#     healthcheck:
#       test: [ "CMD", "pg_isready" ]
#       interval: 10s
#       timeout: 5s
#       retries: 5
# volumes:
#   db-data:
# secrets:
#   db-password:
#     file: db/password.txt
  • Dockerfile
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.4
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8000

# Run the application.
CMD python3 -m uvicorn app:app --
  • Docker Compose logs
server-1  | INFO:     Started server process [7]
server-1  | INFO:     Waiting for application startup.
server-1  | INFO:     Application startup complete.
server-1  | INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Please format your content, place 3 backticks before and after code/config.

Thanks, edited my post,

Technical forums I know are all using markdown and the code block feature is part of it, so it is a good thing to remember it on GitHub or other forums.

Code blocks also make it unneccessary.

This is not right. The service has to listen on 0.0.0.0, meaning on every IP address in the container. If it listens on a loopback interface (meaning IP pointing back to the same network namespace) it won’t be available outside the container. Following the guide should give you 0.0.0.0 at the end as it did to me.

This is my last line in the Dockefile

# Run the application.
CMD uvicorn 'app:app' --host=0.0.0.0 --port=8000

And this is yours (looks like the end is missing too)

Hi rimelak Ákos Takács,
Thank you very much for the insights. I am just retired so have not used Markdown as much as I obviously should have. Combined with the end of the CMD statement I missed and it shows I obviously have quite a bit of homework to do.

I also do not know how the localhost URL is used instead of 0.0.0.0. I do not recall doing this in my own so more research is needed.

I will be following your instructions after I get my niece on a plane back to Boston and will report back.

Again I thank you very much for taking the time to help me!

AgingKeeper

Unless you accidentally removed the end of the code only when you shared it, it is possible you did it somehow when generating the Dockerfile. Or the code generation was stopped before finishing. “--” at the end indicates parameters would have been there and only the dashes were kept instead of the full host and port setting. The default of unicorn must be the loopback IP. Why it starts with python3 in your case, I’m not sure. Maybe you were using a different version of docker init which depends on the version of your Docker Desktop.

Looking at my terminal everything after “app --” was indeed missing. I re-ran docker init fixing this issue and eveything worked as expected. :grinning_face:

Again many thanks!