[Python] Why does a python-on-whales docker mysql command not work, but issuing the command in...

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am using python-on-whales to run a mysql command in a mysql docker conatiner.

    I use docker.execute to call the command inside the container from a python script, and it returns an error:


    import mysql.connector
    import os
    from mysql.connector.errors import DatabaseError
    from python_on_whales import docker

    envs={"MYSQL_ROOT_PASSWORD":"TempPassword",
    "MYSQL_USER":"root",
    "MYSQL_ROOT_PASSWORD":"TempPassword"
    }
    #Start the mysql container
    docker.run("mysql/mysql-server",name="test_mysql",
    envs=envs,
    publish=[(3306,3306)],
    detach=True)
    #copy scripts to container
    docker.copy("./permissions.mysql","test_mysql:/etc/")
    docker.copy("./permissions.sh","test_mysql:/etc/")
    #try calling permissions.sh on the container
    docker.execute("test_mysql", ["/etc/permissions.sh"])


    This produced the following error:

    result = run(full_cmd, tty=tty)
    File "/usr/local/lib/python3.9/site-packages/python_on_whales/utils.py", line 150, in run
    raise DockerException(
    python_on_whales.exceptions.DockerException: The docker command executed was `/usr/local/bin/docker exec test_mysql /etc/permissions.sh`.
    It returned with code 1
    The content of stdout is ''
    The content of stderr is 'mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)


    In the error python-on-whales reports the command that it called:

    /usr/local/bin/docker exec test_mysql /etc/permissions.sh

    But if I issue the command reported back from python-on-whales directly in the bash shell, rather than using python-on-whales the command executes.

    Can anyone explain why?

    Continue reading...

Compartilhe esta Página