1

Trying to use docker-compose.yml instead of starting the containers separately like so:

docker container run --name mysql -d -e MYSQL_RANDOM_ROOT_PASSWORD=yes -e MYSQL_DATABASE=currency -e MYSQL_USER=currency -e MYSQL_PASSWORD=currency mysql/mysql-server:5.7


docker run --name currency -d -p 8000:5000 --rm --link mysql:dbserver -e DATABASE_URL=mysql+pymysql://currency:currency@dbserver/currency currency:latest

When run separately, the flask app correctly finds the DATABASE_URL and uses mysql db.

The docker-compose.yml file which I used is below:

version: "2"
services:
    mysql:
        image: mysql/mysql-server:5.7
        environment:
            - MYSQL_RANDOM_ROOT_PASSWORD:yes
            - MYSQL_DATABASE:currency
            - MYSQL_USER:currency
            - MYSQL_PASSWORD:currency
    currency:
        build: .
        links:
            - "mysql:dbserver"
        ports:
            - "8000:5000"
        environment:
            - DATABASE_URL:"mysql+pymysql://currency:currency@dbserver/currency"

When I use docker-compose.yml, it is not using the mysql db. I believe for some mistake in docker-compose.yml, the flask app is not able see the environment variable "DATABASE_URL". What is going wrong in the docker-compose.yml?

1 Answer 1

1

First: make sure to not use links

The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.
One feature that user-defined networks do not support that you can do with --link is sharing environmental variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.

Second, do a docker exec -it currency bash to open a session in your currency container, and do an echo "DATABASE_URL='${DATABASE_URL}'" in it, to check the environment variable has been set.

Finally, check in your case if this is an ordering issue (currency starting before the database is fully up)

See "Control startup order in Compose".
For instance:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
  db:
    image: postgres
Sign up to request clarification or add additional context in comments.

Comments

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.