skip to Main Content

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=dockerdev
MYSQL_PASSWORD=topolino
MYSQL_ROOT_PASSWORD=topolino
MYSQL_DATABASE=solidarieta

This is the db 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

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:slim
WORKDIR /app
COPY src/script.py .
RUN pip install mysql-connector sqlalchemy
CMD ["python","script.py"]

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

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)

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

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

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?

2

Answers


  1. Chosen as BEST ANSWER

    If i run the container with env-file=config.env i got this new error, it seems like if it can't connect to mysql. What is happening?

    Traceback (most recent call last):
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/network.py", line 482, in open_connection
        addrinfos = socket.getaddrinfo(self.server_host,
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/socket.py", line 963, in getaddrinfo
        for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    socket.gaierror: [Errno -2] Name or service not known
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 146, in __init__
        self._dbapi_connection = engine.raw_connection()
                                 ^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3300, in raw_connection
        return self.pool.connect()
               ^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 449, in connect
        return _ConnectionFairy._checkout(self)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1263, in _checkout
        fairy = _ConnectionRecord.checkout(pool)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 712, in checkout
        rec = pool._do_get()
              ^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 179, in _do_get
        with util.safe_reraise():
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
        raise exc_value.with_traceback(exc_tb)
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 177, in _do_get
        return self._create_connection()
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 390, in _create_connection
        return _ConnectionRecord(self)
               ^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 674, in __init__
        self.__connect()
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 900, in __connect
        with util.safe_reraise():
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
        raise exc_value.with_traceback(exc_tb)
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 896, in __connect
        self.dbapi_connection = connection = pool._invoke_creator(self)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 643, in connect
        return dialect.connect(*cargs, **cparams)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 620, in connect
        return self.loaded_dbapi.connect(*cargs, **cparams)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/__init__.py", line 179, in connect
        return MySQLConnection(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/connection.py", line 95, in __init__
        self.connect(**kwargs)
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/abstracts.py", line 716, in connect
        self._open_connection()
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/connection.py", line 206, in _open_connection
        self._socket.open_connection()
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/network.py", line 500, in open_connection
        raise errors.InterfaceError(
    mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306' (-2 Name or service not known)
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/app/script.py", line 6, in <module>
        connection=engine.connect()
                   ^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3276, in connect
        return self._connection_cls(self)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 148, in __init__
        Connection._handle_dbapi_exception_noconnection(
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2440, in _handle_dbapi_exception_noconnection
        raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 146, in __init__
        self._dbapi_connection = engine.raw_connection()
                                 ^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3300, in raw_connection
        return self.pool.connect()
               ^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 449, in connect
        return _ConnectionFairy._checkout(self)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1263, in _checkout
        fairy = _ConnectionRecord.checkout(pool)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 712, in checkout
        rec = pool._do_get()
              ^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 179, in _do_get
        with util.safe_reraise():
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
        raise exc_value.with_traceback(exc_tb)
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 177, in _do_get
        return self._create_connection()
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 390, in _create_connection
        return _ConnectionRecord(self)
               ^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 674, in __init__
        self.__connect()
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 900, in __connect
        with util.safe_reraise():
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
        raise exc_value.with_traceback(exc_tb)
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 896, in __connect
        self.dbapi_connection = connection = pool._invoke_creator(self)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 643, in connect
        return dialect.connect(*cargs, **cparams)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 620, in connect
        return self.loaded_dbapi.connect(*cargs, **cparams)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/__init__.py", line 179, in connect
        return MySQLConnection(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/connection.py", line 95, in __init__
        self.connect(**kwargs)
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/abstracts.py", line 716, in connect
        self._open_connection()
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/connection.py", line 206, in _open_connection
        self._socket.open_connection()
      File "/usr/local/lib/python3.12/site-packages/mysql/connector/network.py", line 500, in open_connection
        raise errors.InterfaceError(
    sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) 2003: Can't connect to MySQL server on 'db:3306' (-2 Name or service not known)
    (Background on this error at: https://sqlalche.me/e/20/rvf5)
    

  2. When you say you start the container in interactive mode, it seems like you are not using compose, so this error is expected unless you are also passign the –env-file=config.env flag to your docker run -i … command to start the container.

    Now, your script is also a one-time command to run at start up, so it also seems normal that it runs and then stops.

    What you need to do is figure what happened in that container. For that, you want to use docker logs solidarieta_generatore_1.

    If the script ran and worked, then your DB should be initialized as needed.

    To run some other client code in another container, just make sure to pass the –env-file=config.env flag to your docker run command.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search