简体   繁体   中英

Defining ENV variables in a Dockerfile that is used in a docker-compose file

My situation is the following, I have a project with this structure:

project/
|
|______docker-compose.yml
|______database/
       |______Dockerfile
|______webapp/
       |______Dockerfile

My problem is with the database container build. My database/Dockerfile is the following:

FROM mysql:8.0.3
ENV MYSQL_ROOT_PASSWORD=secret
ENV MYSQL_DATABASE=clients
EXPOSE 3306

My docker-compose.yml file looks like this:

version: "3"
services:
database-server:
  build: ./database
  ports:
    - 3306:3306

So as you see above, I defined my ENV variables ( MYSQL_ROOT_PASSWORD and MYSQL_DATABASE ) in my Dockerfile , so I expect them to be used during the build when I use docker-compose . But they don't!

My ENV variables only get used when I build the container manually in project/database using the Dockerfile and the docker build command.

Dockerfile
|
|
| [Variables are available here]
|
|
docker-compose.yml
|
|
| [Variables are NOT available here]
|
docker run

Is it possible to do what I want to do? I want to define everything relative to a specific container in it's Dockerfile , and keep the docker-compose.yml to define the relationships and links between the containers.

Thanks!

UPDATE

As asked by @Tim, here is the output of my docker-compose up :

Starting root_database-server_1 ... done
Starting root_webapp_1 ... done
Attaching to root_database-server_1, root_webapp_1
database-server_1  | error: database is uninitialized and password option is         not specified
database-server_1  |   You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
...
... Rest of the log is fine..

There is no need to build a local image for just that purpose.

version: '3'

services:

   database-server:
     image: mysql:8.0.3
     ports:
       - "3306:3306"
     environment:
       MYSQL_ROOT_PASSWORD: password
       MYSQL_DATABASE: clients

It's better documented and easier to maintain.

I suspect docker-compose is using an older image than the updated image you are expecting. Try this:

docker-compose down to stop and remove any existing containers being used in the stack. You can use docker-compose rm if everything is already stopped.

docker-compose build to make docker-compose rebuild everything with a build direction in the yml file.

docker-compose up to restart your stack. docker-compose should pick up the newly-built database-server image with the ENV set.

You can use them like this:
version: "3"
services:
database-server:
  build: ./database
  ports:
    - 3306:3306
  environment:
    - MYSQL_ROOT_PASSWORD_VARIABLE_FOR_DOCKER_ENV=${MYSQL_ROOT_PASSWORD}
    - MYSQL_DATABASE_VARIABLE_FOR_DOCKER_ENV=${MYSQL_DATABASE}

Update: In your case you can easily do:

environment:
  - MYSQL_ROOT_PASSWORD_VARIABLE_FOR_DOCKER_ENV=${MYSQL_ROOT_PASSWORD}
  - MYSQL_DATABASE_VARIABLE_FOR_DOCKER_ENV=${MYSQL_DATABASE}

However, keeping them in the docker compose is neater, as Andreas said. unless these values will change frequently. I doubt it that's the case with passwords though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM