1
$\begingroup$

I have a simple Python script with this header

from flask import Flask
app = Flask(__name__)

@app.route('/')
import os
import pandas as pd
from statsmodels.formula.api import ols
import statsmodels.api as sm

I have installed GitHub Desktop, Docker Desktop

I have created a folder in that I have

(.venv) (base) angel@Angels-MacBook-Pro docker % ls
Dockerfile      app.py          compose.yaml        requirements.txt

In requirements.txt I have

(.venv) (base) angel@Angels-MacBook-Pro docker % cat requirements.txt 
blinker==1.6.2
click==8.1.6
colorama==0.4.6
Flask==2.3.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
Werkzeug==2.3.6
gunicorn==21.2.0

This is the commands I have run so far

docker init

Then this one returns error

(.venv) (base) angel@Angels-MacBook-Pro docker % docker compose up --build                         
[+] Building 3.9s (14/14) FINISHED                                                                                   docker:desktop-linux
 => [server internal] load .dockerignore                                                                                             0.1s
 => => transferring context: 680B                                                                                                    0.1s
 => [server internal] load build definition from Dockerfile                                                                          0.1s
 => => transferring dockerfile: 1.63kB                                                                                               0.1s
 => [server] resolve image config for docker.io/docker/dockerfile:1                                                                  2.3s
 => [server auth] docker/dockerfile:pull token for registry-1.docker.io                                                              0.0s
 => CACHED [server] docker-image://docker.io/docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31b  0.0s
 => [server internal] load metadata for docker.io/library/python:3.9.12-slim                                                         1.3s
 => [server auth] library/python:pull token for registry-1.docker.io                                                                 0.0s
 => [server base 1/5] FROM docker.io/library/python:3.9.12-slim@sha256:364ada4cb4f943b603d330cbb3d26f689c8b54de51cb12bad1202de7dba3  0.0s
 => [server internal] load build context                                                                                             0.0s
 => => transferring context: 1.39kB                                                                                                  0.0s
 => CACHED [server base 2/5] WORKDIR /app                                                                                            0.0s
 => CACHED [server base 3/5] RUN adduser     --disabled-password     --gecos ""     --home "/nonexistent"     --shell "/sbin/nologi  0.0s
 => CACHED [server base 4/5] RUN --mount=type=cache,target=/root/.cache/pip     --mount=type=bind,source=requirements.txt,target=re  0.0s
 => [server base 5/5] COPY . .                                                                                                       0.0s
 => [server] exporting to image                                                                                                      0.0s
 => => exporting layers                                                                                                              0.0s
 => => writing image sha256:c207ecf929eb3dc4f172db22e56a19e7b89892fcdc31da80501cb3524e137f9d                                         0.0s
 => => naming to docker.io/library/docker-server                                                                                     0.0s
[+] Running 1/1
 ✔ Container docker-server-1  Recreated                                                                                              0.1s 
Attaching to docker-server-1
docker-server-1  | [2023-09-22 11:09:20 +0000] [8] [INFO] Starting gunicorn 21.2.0
docker-server-1  | [2023-09-22 11:09:20 +0000] [8] [INFO] Listening at: http://0.0.0.0:8000 (8)
docker-server-1  | [2023-09-22 11:09:20 +0000] [8] [INFO] Using worker: sync
docker-server-1  | [2023-09-22 11:09:20 +0000] [9] [INFO] Booting worker with pid: 9
docker-server-1  | [2023-09-22 11:09:20 +0000] [9] [ERROR] Exception in worker process
docker-server-1  | Traceback (most recent call last):
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
docker-server-1  |     worker.init_process()
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
docker-server-1  |     self.load_wsgi()
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
docker-server-1  |     self.wsgi = self.app.wsgi()
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
docker-server-1  |     self.callable = self.load()
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
docker-server-1  |     return self.load_wsgiapp()
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
docker-server-1  |     return util.import_app(self.app_uri)
docker-server-1  |   File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app
docker-server-1  |     mod = importlib.import_module(module)
docker-server-1  |   File "/usr/local/lib/python3.9/importlib/__init__.py", line 122, in import_module
docker-server-1  |     raise TypeError(msg.format(name))
docker-server-1  | TypeError: the 'package' argument is required to perform a relative import for '.venv.lib.python3.9.site-packages.eventlet.wsgi'
docker-server-1  | [2023-09-22 11:09:20 +0000] [9] [INFO] Worker exiting (pid: 9)
docker-server-1  | [2023-09-22 11:09:20 +0000] [8] [ERROR] Worker (pid:9) exited with code 3
docker-server-1  | [2023-09-22 11:09:20 +0000] [8] [ERROR] Shutting down: Master
docker-server-1  | [2023-09-22 11:09:20 +0000] [8] [ERROR] Reason: Worker failed to boot.
docker-server-1 exited with code 3

yaml

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

# 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 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

FROM python:3.9
WORKDIR /docker
COPY . /docker
RUN pip install -r requirements.txt
CMD ["python", "./app.py"]
$\endgroup$
4
  • 2
    $\begingroup$ Add the contents of your compose.yaml and Dockerfile here as well. The latter can be deduced from your log but it'd be nice to view the former. $\endgroup$
    – Ram RS
    Commented Sep 22, 2023 at 17:48
  • $\begingroup$ Thank you, I have added these to my main post $\endgroup$
    – Zizogolu
    Commented Sep 23, 2023 at 10:26
  • $\begingroup$ I can't tell what to do next, my experience ends here. Hopefully someone else can help. It might be worth consulting StackOverflow as this looks like a programming problem. $\endgroup$
    – Ram RS
    Commented Sep 25, 2023 at 14:04
  • $\begingroup$ I’m voting to close this question because it belongs on StackOverflow $\endgroup$
    – Ram RS
    Commented Sep 25, 2023 at 14:06

1 Answer 1

4
$\begingroup$

Not sure which tutorial you're following, but I think it's overly complicated. You do not need the compose file, and the Dockerfile has some issues:

First, specify what you want to copy into the container. That is requirements.txt and app.py. Then also, tell pip where the requirements file is. This works in terms of putting a container together. I am not testing the python script itself, this is on you.

$ cat Dockerfile
FROM python:3.9
WORKDIR /docker
COPY ["app.py", "requirements.txt" ,"/docker/"]
RUN pip install -r /docker/requirements.txt
CMD ["python", "./app.py"]

$ cat app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
import os
import pandas as pd
from statsmodels.formula.api import ols
import statsmodels.api as sm

$ cat requirements.txt
blinker==1.6.2
click==8.1.6
colorama==0.4.6
Flask==2.3.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
Werkzeug==2.3.6
gunicorn==21.2.0

$ docker build -t atpoint/foo:1.0 .
[+] Building 5.8s (9/9) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                       0.0s
 => => transferring dockerfile: 38B                                                                                                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                            0.0s
 => [internal] load metadata for docker.io/library/python:3.9                                                                                                                                                                                                                              5.7s
 => [1/4] FROM docker.io/library/python:3.9@sha256:031b2a713079b2436e6c2102dbffbce109c9c2582ad0867c380e3c8d7fed477f                                                                                                                                                                        0.0s
 => [internal] load build context                                                                                                                                                                                                                                                          0.0s
 => => transferring context: 64B                                                                                                                                                                                                                                                           0.0s
 => CACHED [2/4] WORKDIR /docker                                                                                                                                                                                                                                                           0.0s
 => CACHED [3/4] COPY [app.py, requirements.txt ,/docker/]                                                                                                                                                                                                                                 0.0s
 => CACHED [4/4] RUN pip install -r /docker/requirements.txt                                                                                                                                                                                                                               0.0s
 => exporting to image                                                                                                                                                                                                                                                                     0.0s
 => => exporting layers                                                                                                                                                                                                                                                                    0.0s
 => => writing image sha256:6a6f1b08c8343608d2eacaa8463a1638491b504a3230ab00d45a7b32a41e1a43                                                                                                                                                                                               0.0s
 => => naming to docker.io/atpoint/foo:1.0                                                                                                                                                                                                                                                 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

Your container is now available as atpoint/foo:1.0 which reads as username (atpoint), name of container (foo) and a version number / tag (1.0), all of which is free to choose. This is the most basic way of packing a container.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.