Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Docker Compose doesn't seem to read env variables

$
0
0

This is my docker-compose.yml

version: "3"services:    db:        build:            context: db        volumes:            - donazioni-vol:/var/lib/mysql        env_file:            - config.env    generatore:        build:            context: generatore        env_file:            - config.env        depends_on:            - db           volumes:    donazioni-vol:

This is my config.env file:

MYSQL_USER=dockerdevMYSQL_PASSWORD=topolinoMYSQL_ROOT_PASSWORD=topolinoMYSQL_DATABASE=solidarieta

This is the db dockerfile :

FROM mysql:5.7COPY init-db.sql /docker-entrypoint-initdb.dRUN chmod 755 /docker-entrypoint-initdb.d/init-db.sqlRUN chmod 755 /docker-entrypoint-initdb.d

And this is the MySQL code that i want run in a container:

USE solidarieta;CREATE TABLE donazioni(    id INT AUTO_INCREMENT PRIMARY KEY,    provincia VARCHAR(2) NOT NULL,    importo FLOAT);INSERT INTO donazioni (provincia, importo) VALUES ('NA', 350);INSERT INTO donazioni (provincia, importo) VALUES ('TO',75);

This is the Python dockerfile:

FROM python:slimWORKDIR /appCOPY src/script.py .RUN pip install mysql-connector sqlalchemyCMD ["python","script.py"]

And this is the python code that i want run in another container:

from sqlalchemy import create_enginefrom random import choice, randintimport osimport timeengine=create_engine(f"mysql+mysqlconnector://{os.environ['MYSQL_USER']}:{os.environ['MYSQL_PASSWORD']}@db/{os.environ['MYSQL_DATABASE']}")connection=engine.connect()province=['NA','TO','RM','FI','GE','MI']while(True):    connection.execute(f"INSERT INTO donazioni (provincia, importo) VALUES ('{choice(province)}',{randint(10,300)})")    time.sleep(5)

When I run the command docker-compose up --build -d everything works fine and reports the following:

Creating volume "solidarieta_donazioni-vol" with default driverBuilding dbfailed to fetch metadata: fork/exec /usr/local/lib/docker/cli-plugins/docker-buildx: no such file or directoryDEPRECATED: The legacy builder is deprecated and will be removed in a future release.            Install the buildx component to build images with BuildKit:            https://docs.docker.com/go/buildx/Sending build context to Docker daemon  3.072kBStep 1/4 : FROM mysql:5.75.7: Pulling from library/mysql20e4dcae4c69: Pull complete1c56c3d4ce74: Pull completee9f03a1c24ce: Pull complete68c3898c2015: Pull complete6b95a940e7b6: Pull complete90986bb8de6e: Pull completeae71319cb779: Pull completeffc89e9dfd88: Pull complete43d05e938198: Pull complete064b2d298fba: Pull completedf9a4d85569b: Pull completeDigest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bbStatus: Downloaded newer image for mysql:5.7 ---> 5107333e08a8Step 2/4 : COPY init-db.sql /docker-entrypoint-initdb.d ---> e57dc1118bf1Step 3/4 : RUN chmod 755 /docker-entrypoint-initdb.d/init-db.sql ---> Running in 0771c7ccc80a ---> Removed intermediate container 0771c7ccc80a ---> 37c315ed36e0Step 4/4 : RUN chmod 755 /docker-entrypoint-initdb.d ---> Running in be841d499442 ---> Removed intermediate container be841d499442 ---> d7857bc1207cSuccessfully built d7857bc1207cSuccessfully tagged solidarieta_db:latestBuilding generatorefailed to fetch metadata: fork/exec /usr/local/lib/docker/cli-plugins/docker-buildx: no such file or directoryDEPRECATED: The legacy builder is deprecated and will be removed in a future release.            Install the buildx component to build images with BuildKit:            https://docs.docker.com/go/buildx/Sending build context to Docker daemon  3.584kBStep 1/5 : FROM python:slimslim: Pulling from library/pythonb0a0cf830b12: Pull complete72914424168c: Pull complete80818bc858ed: Pull complete805c2b19c211: Pull complete7a89e8daeeae: Pull completeDigest: sha256:2be8daddbb82756f7d1f2c7ece706aadcb284bf6ab6d769ea695cc3ed6016743Status: Downloaded newer image for python:slim ---> f60baedb48a1Step 2/5 : WORKDIR /app ---> Running in fcf03cf2fdf3 ---> Removed intermediate container fcf03cf2fdf3 ---> 65ba76a2f856Step 3/5 : COPY src/script.py . ---> 54df5c1db40cStep 4/5 : RUN pip install mysql-connector sqlalchemy ---> Running in 7d2beae9fb67Collecting mysql-connector  Downloading mysql-connector-2.2.9.tar.gz (11.9 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.9/11.9 MB 2.4 MB/s eta 0:00:00  Preparing metadata (setup.py): started  Preparing metadata (setup.py): finished with status 'done'Collecting sqlalchemy  Downloading SQLAlchemy-2.0.30-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.6 kB)Collecting typing-extensions>=4.6.0 (from sqlalchemy)  Downloading typing_extensions-4.11.0-py3-none-any.whl.metadata (3.0 kB)Collecting greenlet!=0.4.17 (from sqlalchemy)  Downloading greenlet-3.0.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.metadata (3.8 kB)Downloading SQLAlchemy-2.0.30-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2/3.2 MB 2.5 MB/s eta 0:00:00Downloading greenlet-3.0.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (625 kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 625.7/625.7 kB 1.3 MB/s eta 0:00:00Downloading typing_extensions-4.11.0-py3-none-any.whl (34 kB)Building wheels for collected packages: mysql-connector  Building wheel for mysql-connector (setup.py): started  Building wheel for mysql-connector (setup.py): finished with status 'done'  Created wheel for mysql-connector: filename=mysql_connector-2.2.9-cp312-cp312-linux_x86_64.whl size=247951 sha256=732af5d753e0188deb326ac75bd95d0652bc9ffb99609db5efe062b250c6c97b  Stored in directory: /root/.cache/pip/wheels/03/17/fa/d7604c72dd3dd6d3eb3d249abf36cc532c9a9b4354b8f1bc4fSuccessfully built mysql-connectorInstalling collected packages: mysql-connector, typing-extensions, greenlet, sqlalchemySuccessfully installed greenlet-3.0.3 mysql-connector-2.2.9 sqlalchemy-2.0.30 typing-extensions-4.11.0WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv ---> Removed intermediate container 7d2beae9fb67 ---> cd6517bf66a8Step 5/5 : CMD ["python","script.py"] ---> Running in b752d68b9b05 ---> Removed intermediate container b752d68b9b05 ---> 024150ca822fSuccessfully built 024150ca822fSuccessfully tagged solidarieta_generatore:latestCreating solidarieta_db_1 ... doneCreating solidarieta_generatore_1 ... done

Everything seems to be fine, however I only find the db container started, and if I try to start the python container image in interactive mode, I get the following error:

Traceback (most recent call last):  File "/app/script.py", line 5, in <module>    engine=create_engine(f"mysql+mysqlconnector://{os.environ['MYSQL_USER']}:{os.environ['MYSQL_PASSWORD']}@db/{os.environ['MYSQL_DATABASE']}")                                                   ~~~~~~~~~~^^^^^^^^^^^^^^  File "<frozen os>", line 685, in __getitem__KeyError: 'MYSQL_USER'

It's like if it can't read the environment variables. What can I try next?


Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>