skip to Main Content

I have a 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

I want to change env_file for next_app from cypress service. I found solution like this

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

But this solution does not work. Is it even possible ?

2

Answers


  1. Try something like cp .env #docker/.env

    Login or Signup to reply.
  2. No. In Compose (or Docker, or even more generally in Linux/Unix) there is no way for one container (process) to specify environment variables for another.

    You can think of a docker-compose.yml file as a set of instructions only for running containers. If you need a specific set of containers for a specific context – you don’t normally need to run Cypress in production, but this is an integration-test setup – it’s fine to write a separate Compose file just for that setup.

    # 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
    

    This can also be a case where using multiple Compose files together can be a reasonable option. You can define a "standard" Compose setup that only defines the main service, and then an e2e-test Compose file that adds the Cypress container and the environment settings.

    # 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search