我为docker-compose 3创建了一个使用许多环境变量的服务:
version: "3"
services:
myservice:
build:
context: ./myservice
command: ./something
environment:
VAR1: "val1"
VAR2: "val2"
VAR3: "val3"
Run Code Online (Sandbox Code Playgroud)
现在我想添加一个使用相同环境变量值的服务,除了VAL1,并且具有不同的命令:
myotherservice:
build:
context: ./myservice
command: ./somethingelse
environment:
VAR1: "val1-bis"
VAR2: "val2"
VAR3: "val3"
Run Code Online (Sandbox Code Playgroud)
有没有办法避免docker-compose.yml文件中的环境变量重复?在docker-compose 2中,可以使用extends关键字,但在docker-compose 3中不再是这种情况.
编辑:2017年10月,扩展字段被添加到docker-compose 3.4语法:https://docs.docker.com/compose/compose-file/#extension-fields这是正确的方法:
version: "3"
x-env:
&default-env
VAR1: "val1"
VAR2: "val2"
VAR3: "val3"
services:
myservice:
build:
context: ./myservice
command: ./something
environment: *default-env
myotherservice:
build:
context: ./myservice
command: ./somethingelse
environment:
<< : *default-env
VAR1: "val1-bis"
Run Code Online (Sandbox Code Playgroud) 因此,我目前可以使用仅运行数据库和测试脚本的“ docker-compose up test”。我想让我们说“ docker-compose up app”或类似的东西可以运行除测试之外的所有方法。这样我就不会运行不必要的容器。我不确定是否有办法,但这就是我想知道的。某些链接已经执行了一些操作,我可以找出其余的链接。基本上,我只能使用一个命令运行某些容器,而不运行其他命令。
Yaml
version: '3'
services:
webapp:
build: ./literate-app
command: nodemon -e vue,js,css start.js
depends_on:
- postgres
links:
- postgres
environment:
- DB_HOST=postgres
ports:
- "3000:3000"
networks:
- literate-net
server:
build: ./readability-server
command: nodemon -L --inspect=0.0.0.0:5555 server.js
networks:
- literate-net
redis_db:
image: redis:alpine
networks:
- literate-net
postgres:
restart: 'always'
#image: 'bitnami/postgresql:latest'
volumes:
- /bitnami
ports:
- "5432:5432"
networks:
- literate-net
environment:
- "FILLA_DB_USER=my_user"
- "FILLA_DB_PASSWORD=password123"
- "FILLA_DB_DATABASE=my_database"
- "POSTGRES_PASSWORD=password123"
build: './database-creation'
test:
image: node:latest
build: …Run Code Online (Sandbox Code Playgroud)