简体   繁体   English

使用Python连接到Docker容器中的MySQL

[英]Connect to MySQL in Docker container using Python

I am using pony.orm to connect to mysqldb using a python code: 我正在使用pony.orm使用python代码连接到mysqldb:

db.bind(provider='mysql', user=username, password=password, host='0.0.0.0', database=database)

And when I write the docker compose file: 当我编写docker compose文件时:

  db:
    image: mariadb
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: db
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: ''

How can I pass the hostname to the python program by giving a value (in environment:) in the docker-compose.yml file ? 如何通过在docker-compose.yml文件中提供一个值(在环境:中)将主机名传递给python程序?

If I pass the value there can I access the value through os.environ['PARAM'] in the Python code? 如果传递值,是否可以通过Python代码中的os.environ ['PARAM']访问值?

Because you've named your service db in the docker-compose.yaml , you can use that as the host , provided you are on the same network: 因为您已经在docker-compose.yaml服务db命名为db ,所以可以将其用作host ,前提是您位于同一网络上:

db.bind(provider='mysql', user=username, password=password, host='db', database=database)

To ensure you are on that network, in your docker-compose.yaml , at the bottom, you'll want: 为了确保您位于该网络上,请在底部docker-compose.yaml ,您需要:

networks:
  default:
    external:
      name: <your-network>

And you'll need to create that network before running docker-compose up 而且您需要在运行docker-compose up之前创建该网络

docker network create <your-network>

This avoids the need for an environment variable, as the container name will be added to the routing table of the network. 由于容器名称将添加到网络的路由表中,因此避免了对环境变量的需要。

You don't need to define your own network, as docker-compose will handle that for you, but if you prefer to be a bit more explicit, it allows you the flexibility to do so. 不需要定义自己的网络,因为docker-compose会为您处理该网络,但是如果您希望更明确一些,它会为您提供灵活性。 Normally, you would reserve this for multiple compose solutions that you wanted to join together on a single network, which is not the case here. 通常,您可以将其保留给要在单个网络上连接在一起的多个组合解决方案,在这里不是这种情况。

It's handled in docker-compose the same way you would do it in vanilla docker : 它在docker-compose处理的方式与在香草docker处理的方式相同:

docker run -d -p 3308:3306 --network <your-network> --name db mariadb
docker run -it --network <your-network> ubuntu bash

# in the shell of the ubuntu container
apt-get update && apt-get install iputils-ping -y
ping -c 5 db

# here you will see the results of ping reaching container db
5 packets transmitted, 5 received, 0% packet loss, time 4093ms

Edit 编辑

As a note, per @DavidMaze's comment, the port you will be communicating with is 3306, since that's the port that the container is listening on, not 3308. 注意,根据@DavidMaze的评论,您要与之通信的端口是3306,因为这是容器正在侦听的端口,而不是3308。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Docker容器中的python脚本连接到localhost上的mysql数据库 - How to connect to a mysql database on localhost from a python script in a docker container 无法使用 Python 脚本连接到 Docker 上的 MySQL 数据库容器 - Cannot connect to MySQL database container on Docker with Python script Python 无法使用 docker 连接到 mysql - Python can't connect to mysql using docker 如何在docker容器中连接外部mysql服务器 - How to connect external mysql server in docker container 如何在Docker容器内使用python子进程执行mysql命令 - How to perform mysql command using python subprocess inside docker container 如何通过运行带有URL的docker容器来连接docker容器中的python应用 - how to connect python app in docker container with running docker container with url Python-Docker 容器无法连接到 MariaDB-Docker 容器 - Python-Docker container cannot connect to MariaDB-Docker container 将 python 脚本连接到 docker 中的 mysql - Connect python script to mysql in docker 无法使用 Dockerfile 连接到 docker 容器(python 服务器) - Can not connect to docker container(python server) with Dockerfile Mariadb docker容器无法使用Python连接到主机上的MySQL服务器(111连接被拒绝) - Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM