2

I am using pony.orm to connect to mysqldb using a python code:

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

And when I write the docker compose file:

  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 ?

If I pass the value there can I access the value through os.environ['PARAM'] in the Python code?

3
  • 1
    is the python program running in docker-compose as well? Commented May 22, 2019 at 16:55
  • Same question as above. Is your Python program running in Docker? And is it in the same Docker network as your database container? Commented May 22, 2019 at 16:58
  • @C.Nivs Yes. I have created an image of the python program and added it to the docker compose file, so it is running as a different service Commented May 22, 2019 at 17:02

1 Answer 1

4

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:

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:

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

And you'll need to create that network before running 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. 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 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.

Sign up to request clarification or add additional context in comments.

10 Comments

So, is the 'hostname' same with the service name?
Yes, I think you'll also want to specify the port when accessing db as well
So here in my case port should be 3308, please clarify?
Your port should be 3308, since that's the host port that it's mapped back to
Docker Compose will automatically create a network for you; you don’t need to do any of the manual networks: setup you describe. You always need to use the port the server inside the container is listening on (for MySQL/MariaDB, 3306), not the external ports your ports: declarations (if any) map to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.