2

I have a simple docker compose file to create a Mysql database for my app. But I cannot interpolate the environment variable MYSQL_PORT to set a custom port. Running docker compose up with the configuration below results in a random port being assigned to mysql.

The path to the env file does work, since I have env variables configuring the database.

docker-compose.yml

version: '3'
services:
  mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - mysql_data:/var/lib/mysql
    env_file:
      - ../../.env
    ports:
      - ${MYSQL_PORT}:3306

volumes:
  mysql_data:

.env

MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=final_project_database
MYSQL_USER=db_user
MYSQL_PASSWORD=some_db_user_password
0

2 Answers 2

2

Use --env-file option with docker-compose up command. env_file declared in your MySQL service applies only for container env

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

1 Comment

I tried that previously with docker-compose --env-file ../../.env up. If I do this I get an error telling me to specify either MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD or MYSQL_RANDOM_ROOT_PASSWORD. So I'm assuming it is not reading the .env file.
0

Move your .env file to the same directory as the docker-compose file and change the env_file to point to it. That way both docker-compose and the container will use the same environment file.

Right now it's only the container that's using it.

version: '3'
services:
  mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - mysql_data:/var/lib/mysql
    env_file:
      - ./.env
    ports:
      - ${MYSQL_PORT}:3306

volumes:
  mysql_data:

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.