这是我的 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:
这是我的config.env文件:
MYSQL_USER=dockerdev
MYSQL_PASSWORD=topolino
MYSQL_ROOT_PASSWORD=topolino
MYSQL_DATABASE=solidarieta
这是数据库dockerfile:
FROM mysql:5.7
COPY init-db.sql /docker-entrypoint-initdb.d
RUN chmod 755 /docker-entrypoint-initdb.d/init-db.sql
RUN chmod 755 /docker-entrypoint-initdb.d
这是我想在容器中运行的 MySQL 代码:
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);
这是Python dockerfile:
FROM python:slim
WORKDIR /app
COPY src/script.py .
RUN pip install mysql-connector sqlalchemy
CMD ["python","script.py"]
这是我想在另一个容器中运行的Python代码:
from sqlalchemy import create_engine
from random import choice, randint
import os
import time
engine=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)
当我运行命令
docker-compose up --build -d
时,一切正常并报告以下内容:
Creating volume "solidarieta_donazioni-vol" with default driver
Building db
failed to fetch metadata: fork/exec /usr/local/lib/docker/cli-plugins/docker-buildx: no such file or directory
DEPRECATED: 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.072kB
Step 1/4 : FROM mysql:5.7
5.7: Pulling from library/mysql
20e4dcae4c69: Pull complete
1c56c3d4ce74: Pull complete
e9f03a1c24ce: Pull complete
68c3898c2015: Pull complete
6b95a940e7b6: Pull complete
90986bb8de6e: Pull complete
ae71319cb779: Pull complete
ffc89e9dfd88: Pull complete
43d05e938198: Pull complete
064b2d298fba: Pull complete
df9a4d85569b: Pull complete
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7
---> 5107333e08a8
Step 2/4 : COPY init-db.sql /docker-entrypoint-initdb.d
---> e57dc1118bf1
Step 3/4 : RUN chmod 755 /docker-entrypoint-initdb.d/init-db.sql
---> Running in 0771c7ccc80a
---> Removed intermediate container 0771c7ccc80a
---> 37c315ed36e0
Step 4/4 : RUN chmod 755 /docker-entrypoint-initdb.d
---> Running in be841d499442
---> Removed intermediate container be841d499442
---> d7857bc1207c
Successfully built d7857bc1207c
Successfully tagged solidarieta_db:latest
Building generatore
failed to fetch metadata: fork/exec /usr/local/lib/docker/cli-plugins/docker-buildx: no such file or directory
DEPRECATED: 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.584kB
Step 1/5 : FROM python:slim
slim: Pulling from library/python
b0a0cf830b12: Pull complete
72914424168c: Pull complete
80818bc858ed: Pull complete
805c2b19c211: Pull complete
7a89e8daeeae: Pull complete
Digest: sha256:2be8daddbb82756f7d1f2c7ece706aadcb284bf6ab6d769ea695cc3ed6016743
Status: Downloaded newer image for python:slim
---> f60baedb48a1
Step 2/5 : WORKDIR /app
---> Running in fcf03cf2fdf3
---> Removed intermediate container fcf03cf2fdf3
---> 65ba76a2f856
Step 3/5 : COPY src/script.py .
---> 54df5c1db40c
Step 4/5 : RUN pip install mysql-connector sqlalchemy
---> Running in 7d2beae9fb67
Collecting 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:00
Downloading 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:00
Downloading 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/d7604c72dd3dd6d3eb3d249abf36cc532c9a9b4354b8f1bc4f
Successfully built mysql-connector
Installing collected packages: mysql-connector, typing-extensions, greenlet, sqlalchemy
Successfully installed greenlet-3.0.3 mysql-connector-2.2.9 sqlalchemy-2.0.30 typing-extensions-4.11.0
WARNING: 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
---> cd6517bf66a8
Step 5/5 : CMD ["python","script.py"]
---> Running in b752d68b9b05
---> Removed intermediate container b752d68b9b05
---> 024150ca822f
Successfully built 024150ca822f
Successfully tagged solidarieta_generatore:latest
Creating solidarieta_db_1 ... done
Creating solidarieta_generatore_1 ... done
一切似乎都很好,但是我只发现 db 容器已启动,如果我尝试以交互模式启动 python 容器映像,则会收到以下错误:
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'
就像无法读取环境变量一样。接下来我可以尝试什么?
当您说以交互模式启动容器时,似乎您没有使用 compose,因此除非您还将 --env-file=config.env 标志传递给 docker run -i ..启动容器的命令。
现在,您的脚本也是在启动时运行的一次性命令,因此它运行然后停止似乎也很正常。
您需要做的是弄清楚该容器中发生了什么。为此,您需要使用
docker logs solidarieta_generatore_1
。
如果脚本运行并有效,那么您的数据库应该根据需要进行初始化。
要在另一个容器中运行其他客户端代码,只需确保将 --env-file=config.env 标志传递给 docker run 命令即可。