Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose env file not working

Tags:

I'm writing as docker-compose file to up MySQL instance and want to use few variable from env file: here are the files actually look like:

docker-compose.yml

version: '3.3' services:   db:     image: mysql     restart: always     env_file:       - ./imran.env     environment:       MYSQL_ROOT_PASSWORD: ${PASS}     ports:       - ${PORT1}: ${PORT2} 

imran.env

PASS=imran123 PORT1=3306 PORT2=3306 

Instead of working correct i'm getting following errors:

WARNING: The PASS variable is not set. Defaulting to a blank string. WARNING: The PORT2 variable is not set. Defaulting to a blank string. ERROR: The Compose file './docker-compose.yml' is invalid because: services.db.ports contains unsupported option: '${PORT1} 

Please Help

like image 740
imran ahmedani Avatar asked Jan 29 '18 06:01

imran ahmedani


People also ask

Does docker use .env file?

The .env file, is only used during a pre-processing step when working with docker-compose.yml files. Dollar-notation variables like $HI are substituted for values contained in an “.env” named file in the same directory.

Can Docker Compose use environment variables?

Several environment variables are available for you to configure the Docker Compose command-line behavior.

How do I pass environment variables to docker containers?

Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

Is Docker compose deprecated?

Compose V1 is marked as deprecated, and we'll begin patching only high-severity vulnerabilities or fixing critical bugs until the next milestone. Developers can continue to alias docker-compose to use docker compose.


1 Answers

You have some issues in your docker-compose.yaml file:

A:

A space symbol between ports values. It should be without a space:

ports:   - ${PORT1}:${PORT2} 

B:

You need to use .env file in folder where docker-compose.yaml is in order to declaring default environment variables for both docker-compose.yaml file and docker container. env_file section is used to put values into container only.

So, you should do the following:

1.

Re-name file with ENV variables to .env:

mv imran.env .env 

2.

Use the following docker-compose.yaml after:

version: '3.3' services:   db:     image: mysql     restart: always     environment:       MYSQL_ROOT_PASSWORD: ${PASS}     ports:       - ${PORT1}:${PORT2} 
like image 123
nickgryg Avatar answered Sep 24 '22 07:09

nickgryg