简体   繁体   English

在docker-compose文件中使用的Dockerfile中定义ENV变量

[英]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. 我的问题是database容器的构建。 My database/Dockerfile is the following: 我的database/Dockerfile如下:

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

My docker-compose.yml file looks like this: docker-compose.yml文件如下所示:

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 . 如上所述,我在Dockerfile定义了我的ENV变量( MYSQL_ROOT_PASSWORDMYSQL_DATABASE ),因此我希望在使用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. 我使用DockerfileDockerfile docker build命令在project/database中手动构建容器时,才会使用我的ENV变量。

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. 我想在它的Dockerfile定义与特定容器相关的Dockerfile ,并保持docker-compose.yml来定义容器之间的关系和链接。

Thanks! 谢谢!

UPDATE UPDATE

As asked by @Tim, here is the output of my docker-compose up : 正如@Tim所问,这是我的docker 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. 我怀疑docker-compose正在使用比您期望的更新图像更旧的图像。 Try this: 尝试这个:

docker-compose down to stop and remove any existing containers being used in the stack. docker-compose down停止并删除堆栈中使用的任何现有容器。 You can use docker-compose rm if everything is already stopped. 如果所有内容都已停止,您可以使用docker-compose rm

docker-compose build to make docker-compose rebuild everything with a build direction in the yml file. docker-compose build使docker-compose在yml文件中使用构建方向重建所有内容。

docker-compose up to restart your stack. docker-compose up以重新启动堆栈。 docker-compose should pick up the newly-built database-server image with the ENV set. docker-compose应该使用ENV集合来获取新构建的database-server映像。

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 我怀疑密码就是这种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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