skip to Main Content

I am trying to work with multiple images from a single Dockerfile. The following succeeds when docker compose build is run, but I notice in the layers that both images have ENV KEY2=VALUE2. I am expecting image1 to have ENV KEY1=VALUE1.

What am I missing?

Dockerfile:

FROM ubuntu:23.04 AS image1
ENV KEY1=VALUE1

FROM ubuntu:23.04 AS image2
ENV KEY2=VALUE2

compose.yaml:

services:
  service1:
    container_name: container1
    build: .
    image: image1
  service2:
    container_name: container2
    build: .
    image: image2

4

Answers


  1. Added after comment :

    Then you better to create two Dockerfiles and use them in docker-compose

    docker-file1.yaml

    FROM ubuntu:23.04 AS image1
    ENV KEY1=VALUE1
    

    docker-file2.yaml

    FROM ubuntu:23.04 AS image2
    ENV KEY2=VALUE2
    

    You docker-compose file :

    services:
      service1:
        container_name: container1
        build: docker-file1.yaml
        image: image1
      service2:
        container_name: container2
        build: docker-file2.yaml
        image: image2
    
    Login or Signup to reply.
  2. The documentation describes the following behavior when you use image in the docker compose file:

    If the image does not exist, Compose attempts to pull it, unless you have also specified build, in which case it builds it using the specified options and tags it with the specified tag.

    so the Dockerfile is built and indeed it is tagged with image1, however given the multi-stage build nature of the Dockerfile you end up only with everything following the second FROM.

    Use separate Dockerfiles for the two images.

    Login or Signup to reply.
  3. Environment variables are not shared by default across stages in multi-stage Dockerfiles. However, as others have pointed out in the comments, behaviour is likely different when building via docker compose.

    If you intend to share the Dockerfile between the two images, and the only differences between the two are a few variables, you should use the args sub-option:

    Dockerfile

    FROM ubuntu:23.04
    ARG MY_VAR
    ENV KEY1 $MY_VAR
    

    Docker Compose

    build:
      context: .
      dockerfile: Dockerfile
      args:
        - MY_VAR=value1
    

    But if the images have different setups, you should use two separate Dockerfiles.

    Login or Signup to reply.
  4. I tested it, and it works.

    Please create 2 dockerfiles.

    Dockerfile1:

    FROM ubuntu:20.04 AS image1
    ENV KEY1=VALUE1
    

    Dockerfile2:

    FROM ubuntu:20.04 AS image2
    ENV KEY2=VALUE2
    

    docker-compose.yaml:

    version: '3'
    services:
      service1:
        container_name: container1
        build:
          context: .
          dockerfile: Dockerfile1
        image: image1
        command: tail -f /dev/null
      service2:
        container_name: container2
        build:
          context: .
          dockerfile: Dockerfile2
        image: image2
        command: tail -f /dev/null
    

    To run the containers up, "tail -f /dev/null".

    Print screens:

    • To build: docker-compose build
    • To run: docker-compose up -d
    • To enter to view environment variables: docker exec -it container1 bash, and env

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search