<p>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:</p>
<p>First, specify what you want to copy into the container. That is <code>requirements.txt</code> and <code>app.py</code>. 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.</p>
<pre class="lang-bash prettyprint-override"><code>$ cat Dockerfile
FROM python:3.9
WORKDIR /docker
COPY [&quot;app.py&quot;, &quot;requirements.txt&quot; ,&quot;/docker/&quot;]
RUN pip install -r /docker/requirements.txt
CMD [&quot;python&quot;, &quot;./app.py&quot;]

$ 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
 =&gt; [internal] load build definition from Dockerfile                                                                                                                                                                                                                                       0.0s
 =&gt; =&gt; transferring dockerfile: 38B                                                                                                                                                                                                                                                        0.0s
 =&gt; [internal] load .dockerignore                                                                                                                                                                                                                                                          0.0s
 =&gt; =&gt; transferring context: 2B                                                                                                                                                                                                                                                            0.0s
 =&gt; [internal] load metadata for docker.io/library/python:3.9                                                                                                                                                                                                                              5.7s
 =&gt; [1/4] FROM docker.io/library/python:3.9@sha256:031b2a713079b2436e6c2102dbffbce109c9c2582ad0867c380e3c8d7fed477f                                                                                                                                                                        0.0s
 =&gt; [internal] load build context                                                                                                                                                                                                                                                          0.0s
 =&gt; =&gt; transferring context: 64B                                                                                                                                                                                                                                                           0.0s
 =&gt; CACHED [2/4] WORKDIR /docker                                                                                                                                                                                                                                                           0.0s
 =&gt; CACHED [3/4] COPY [app.py, requirements.txt ,/docker/]                                                                                                                                                                                                                                 0.0s
 =&gt; CACHED [4/4] RUN pip install -r /docker/requirements.txt                                                                                                                                                                                                                               0.0s
 =&gt; exporting to image                                                                                                                                                                                                                                                                     0.0s
 =&gt; =&gt; exporting layers                                                                                                                                                                                                                                                                    0.0s
 =&gt; =&gt; writing image sha256:6a6f1b08c8343608d2eacaa8463a1638491b504a3230ab00d45a7b32a41e1a43                                                                                                                                                                                               0.0s
 =&gt; =&gt; 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
</code></pre>
<p>Your container is now available as <code>atpoint/foo:1.0</code> 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.</p>

CC BY-SA 4.0
2023-09-26T07:17:36.523Z
2023-09-26T07:17:36.523Z
ATpoint
<p>I have a simple Python script with this header</p>
<pre class="lang-py prettyprint-override"><code>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
</code></pre>
<p>I have installed GitHub Desktop, Docker Desktop</p>
<p>I have created a folder in that I have</p>
<pre><code>(.venv) (base) angel@Angels-MacBook-Pro docker % ls
Dockerfile      app.py          compose.yaml        requirements.txt
</code></pre>
<p>In <code>requirements.txt</code> I have</p>
<pre><code>(.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
</code></pre>
<p>This is the commands I have run so far</p>
<pre><code>docker init
</code></pre>
<p>Then this one returns error</p>
<pre><code>(.venv) (base) angel@Angels-MacBook-Pro docker % docker compose up --build                         
[+] Building 3.9s (14/14) FINISHED                                                                                   docker:desktop-linux
 =&gt; [server internal] load .dockerignore                                                                                             0.1s
 =&gt; =&gt; transferring context: 680B                                                                                                    0.1s
 =&gt; [server internal] load build definition from Dockerfile                                                                          0.1s
 =&gt; =&gt; transferring dockerfile: 1.63kB                                                                                               0.1s
 =&gt; [server] resolve image config for docker.io/docker/dockerfile:1                                                                  2.3s
 =&gt; [server auth] docker/dockerfile:pull token for registry-1.docker.io                                                              0.0s
 =&gt; CACHED [server] docker-image://docker.io/docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31b  0.0s
 =&gt; [server internal] load metadata for docker.io/library/python:3.9.12-slim                                                         1.3s
 =&gt; [server auth] library/python:pull token for registry-1.docker.io                                                                 0.0s
 =&gt; [server base 1/5] FROM docker.io/library/python:3.9.12-slim@sha256:364ada4cb4f943b603d330cbb3d26f689c8b54de51cb12bad1202de7dba3  0.0s
 =&gt; [server internal] load build context                                                                                             0.0s
 =&gt; =&gt; transferring context: 1.39kB                                                                                                  0.0s
 =&gt; CACHED [server base 2/5] WORKDIR /app                                                                                            0.0s
 =&gt; CACHED [server base 3/5] RUN adduser     --disabled-password     --gecos &quot;&quot;     --home &quot;/nonexistent&quot;     --shell &quot;/sbin/nologi  0.0s
 =&gt; CACHED [server base 4/5] RUN --mount=type=cache,target=/root/.cache/pip     --mount=type=bind,source=requirements.txt,target=re  0.0s
 =&gt; [server base 5/5] COPY . .                                                                                                       0.0s
 =&gt; [server] exporting to image                                                                                                      0.0s
 =&gt; =&gt; exporting layers                                                                                                              0.0s
 =&gt; =&gt; writing image sha256:c207ecf929eb3dc4f172db22e56a19e7b89892fcdc31da80501cb3524e137f9d                                         0.0s
 =&gt; =&gt; 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 &quot;/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py&quot;, line 609, in spawn_worker
docker-server-1  |     worker.init_process()
docker-server-1  |   File &quot;/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py&quot;, line 134, in init_process
docker-server-1  |     self.load_wsgi()
docker-server-1  |   File &quot;/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py&quot;, line 146, in load_wsgi
docker-server-1  |     self.wsgi = self.app.wsgi()
docker-server-1  |   File &quot;/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py&quot;, line 67, in wsgi
docker-server-1  |     self.callable = self.load()
docker-server-1  |   File &quot;/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py&quot;, line 58, in load
docker-server-1  |     return self.load_wsgiapp()
docker-server-1  |   File &quot;/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py&quot;, line 48, in load_wsgiapp
docker-server-1  |     return util.import_app(self.app_uri)
docker-server-1  |   File &quot;/usr/local/lib/python3.9/site-packages/gunicorn/util.py&quot;, line 371, in import_app
docker-server-1  |     mod = importlib.import_module(module)
docker-server-1  |   File &quot;/usr/local/lib/python3.9/importlib/__init__.py&quot;, 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
</code></pre>
<p><strong>yaml</strong></p>
<pre><code># 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 &quot;server&quot;.
# 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: [ &quot;CMD&quot;, &quot;pg_isready&quot; ]
#       interval: 10s
#       timeout: 5s
#       retries: 5
# volumes:
#   db-data:
# secrets:
#   db-password:
#     file: db/password.txt
</code></pre>
<p><strong>Dockerfile</strong></p>
<pre><code>FROM python:3.9
WORKDIR /docker
COPY . /docker
RUN pip install -r requirements.txt
CMD [&quot;python&quot;, &quot;./app.py&quot;]
</code></pre>

CC BY-SA 4.0
2023-09-22T17:48:01.723Z
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.
Ram RS
CC BY-SA 4.0
2023-09-23T10:26:00.673Z
Thank you, I have added these to my main post
Zizogolu
CC BY-SA 4.0
2023-09-25T14:04:21.020Z
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.
Ram RS
CC BY-SA 4.0
2023-09-25T14:06:57.693Z
I’m voting to close this question because it belongs on StackOverflow
Ram RS
<python><docker><git>
How to pack up a Python script in the form a Dockerfile
# How to pack up a Python script in the form a Dockerfile

(Asked by: username_0 on 2023-09-22)

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"]

**Tags:** python, docker, git
> **Comments:**
> **username_1**: 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.  
> **username_0**: Thank you, I have added these to my main post  
> **username_1**: 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.  
> **username_1**: I’m voting to close this question because it belongs on StackOverflow  

---

## 1 answers

### Answer by username_2 on 2023-09-26. Score: 4 | Accepted answer

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.


---

