.Env wird nicht im Docker -Container gelesenPython

Python-Programme
Anonymous
 .Env wird nicht im Docker -Container gelesen

Post by Anonymous »

schrieb ein Python -Skript, das eine Einstellung von .Env liest. Beim Arbeiten und Debuggen in VSCODE funktioniert dies. Ich erstelle dann einen Container davon mit Docker Build -t MyApp . Wenn ich diesen Befehl ausführe, bin ich im Verzeichnis, in dem auch die .Env -Datei enthält.

Code: Select all

docker run -d -p 5000:5000 \
-v "$(pwd)/.env:/app/.env" \
-v "$(pwd)/database:/app/database" \
myapp:latest
< /code>
Es startet eine Flask -Website und ich kann in der GUI sehen, dass die Umgebung var leer ist. Wenn ich in den Container ausführte, ist der .Env da und enthält die var, die ich brauche.import os
from dotenv import load_dotenv

load_dotenv()
und Ich lade die var mit os.getenv ('envtarif') .

Code: Select all

FROM python:3.11-slim-buster
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt && \
apt-get update && \
apt-get install -y cron

COPY crontab /etc/cron.d/reporting-cron
RUN chmod 0644 /etc/cron.d/reporting-cron
RUN crontab /etc/cron.d/reporting-cron

EXPOSE 5000
CMD ["sh", "-c", "cron && python app.py"]
< /code>
Ich führe den Container so aus:
sudo docker run -d -p 5000:5000 -v "$(pwd)/.env:/app/.env" -v "$(pwd)/database:/app/database" chargelogreporter:latest

Dann sudo docker exec -it flamboyant_burnell/bin/sh

Code: Select all

pwd
Ergebnis/App

Code: Select all

# ls -la
total 132
drwxr-xr-x 1 root root  4096 May 14 23:30 .
drwxr-xr-x 1 root root  4096 May 14 23:30 ..
-rw-rw-r-- 1 root root  3474 May 14 20:56 .dockerignore
-rw-r--r-- 1 root root   568 Apr 28 20:52 .env
drwxrwxr-x 8 root root  4096 May 14 21:40 .git
-rw-rw-r-- 1 root root  3474 Jan 24 00:24 .gitignore
drwxrwxr-x 2 root root  4096 May 14 21:31 .vscode
-rw-rw-r-- 1 root root  1009 May 14 22:07 Dockerfile
-rw-rw-r-- 1 root root 35149 Dec 23 15:08 LICENSE
-rw-rw-r-- 1 root root  2753 May 14 21:22 README.md
drwxr-xr-x 2 root root  4096 May 14 23:30 __pycache__
-rw-r--r-- 1 root root  1049 May 14 23:30 app.log
-rw-rw-r-- 1 root root  3707 May 14 22:57 app.py
-rw-rw-r-- 1 root root    86 May 14 21:08 crontab
drwxrwxr-x 2 1000 1000  4096 May 14 21:23 database
drwxrwxr-x 2 root root  4096 Jan 23 23:46 download
-rw-r--r-- 1 root root   466 Feb  4 22:01 env-example
-rw-r--r-- 1 root root   349 May 14 22:45 requirements.txt
-rw-rw-r-- 1 root root  1224 Apr 28 21:22 schedule.py
-rw-rw-r-- 1 root root 15362 May 14 22:42 sharedcomps.py
drwxrwxr-x 2 root root  4096 Apr  8 19:50 static
drwxrwxr-x 2 root root  4096 May 14 21:31 templates
< /code>
Dann ist zu beweisen .Env ist korrekt geladen: < /p>
tarif=0.32
reportday=13

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post