如何将环境文件交换为另一个docker服务 [英] How to swap env file for another docker service

查看:11
本文介绍了如何将环境文件交换为另一个docker服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker-compose.yml

services:
  nextjs:
    container_name: next_app
    build:
      context: ./
    restart: on-failure
    command: npm run dev
    volumes:
      - ./:/app
      - /app/node_modules
      - /app/.next
    ports:
      - "3000:3000"
  cypress:
    image: "cypress/included:9.4.1"
    depends_on:
      - next_app
    environment:
      - CYPRESS_baseUrl=http://nextjs:3000
    working_dir: /e2e
    volumes:
      - ./e2e:/e2e

我想从Cypress服务更改Next_app的env_file。我找到了这样的解决方案

cypress:
    image: "cypress/included:9.4.1"
    depends_on:
      - next_app
    environment:
      - CYPRESS_baseUrl=http://nextjs:3000
    working_dir: /e2e
    volumes:
      - ./e2e:/e2e
    next_app:
      env_file: .env.test

但此解决方案不起作用。这有可能吗?

推荐答案

否。在Compose(或Docker,或者更一般的Linux/Unix)中,一个容器(进程)无法为另一个容器(进程)指定环境变量。

您可以将docker-compose.yml文件视为仅用于运行容器的一组指令。如果您需要针对特定上下文的一组特定容器-您通常不需要在生产中运行Cypress,但这是一个集成测试设置-只为该设置编写一个单独的合成文件就可以了。

# docker-compose.cypress.yml
# Used only for integration testing
version: '3.8'
services:
  nextjs:
    build: .
    restart: on-failure
    ports:
      - "3000:3000"
    env_file: .env.test # <-- specific to this test-oriented Compose file
  cypress:
    build: ./e2e
    depends_on:
      - nextjs
    environment:
      - CYPRESS_baseUrl=http://nextjs:3000
docker-compose -f docker-compose.cypress.yml up --build
在这种情况下,使用multiple Compose files together可能是一个合理的选择。您可以定义仅定义主服务的标准合成设置,然后定义添加Cypress容器和环境设置的e2e-test合成文件。

# docker-compose.yml
version: '3.8'
services:
  nextjs:
    image: registry.example.com/nextjs:${NEXTJS_TAG:-latest}
    restart: on-failure
    ports:
      - '3000:3000'
# docker-compose.e2e.yaml
version: '3.8'
services:
  nextjs:
    # These add to the definitions in the base `docker-compose.yml`
    build: .
    env_file: .env.test
  cypress:
    # This is a brand new container for this specific setup
    depends_on: [nextjs]
    et: cetera # copy from question or previous Compose setup
docker-compose 
  -f docker-compose.yml 
  -f docker-compose.e2e.yml 
  up --build

这篇关于如何将环境文件交换为另一个docker服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆