skip to Main Content

I have a docker-compose that looks like this, in part:

  nginx:
    container_name: ${NGINX_CONTAINER_NAME}
    ports:
      - ${NGINX_HTTP_PORT1}:${NGINX_HTTP_PORT2}
      - ${NGINX_HTTPS_PORT1}:${NGINX_HTTPS_PORT2}
    build: 
      dockerfile: Dockerfile
      args: 
       - NGINX_VERSION=${NGINX_VERSION}
    volumes:
      - ${NGINX_CONF_DIR:-./nginx}:/etc/nginx/conf.d
      - ${NGINX_LOG_DIR:-./logs/nginx}:/var/log/nginx
      - ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
    depends_on:
      - wordpress
    restart: always

As you can see, I’m trying to pass a variable called NGINX_VERSION into the Dockerfile.

Here’s what the .env has:

# nginx
NGINX_VERSION=1.21.3
NGINX_HTTP_PORT1=8085
NGINX_HTTP_PORT2=8085
NGINX_HTTPS_PORT1=443
NGINX_HTTPS_PORT2=443
NGINX_CONTAINER_NAME=dev_nginx

And this is what the working Dockerfile looks like:

FROM nginx:1.21.3

RUN apt-get update 
  && apt-get -y install openssl 
  && apt-get -y install vim

RUN mkdir -p /etc/openssl/certs
RUN mkdir -p /etc/nginx/snippets
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/openssl/nginx-selfsigned.key -out /etc/openssl/nginx-selfsigned.crt -subj "/C=US/ST=NY/L=NY/O=ACME/OU=CD/CN=WPDeveloper"

Problem

When I change image name in the Dockerfile to look like this:

 FROM nginx:$NGINX_VERSION

or this:

 FROM nginx:${NGINX_VERSION}

I get the following error:

failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "nginx:": invalid reference format

When I leave the version number hardcoded in the Dockerfile, everything works.
Also, in case it helps, I verify that the values in env are being read by running this command:

  docker-compose -f .docker-compose.yml config

In part, this is what the output looks like – notice that it finds the correct value for NGINX VERSION

nginx:
    build:
      dockerfile: Dockerfile
      args:
        NGINX_VERSION: 1.21.3
    container_name: dev_nginx
    depends_on:
      wordpress:
        condition: service_started
    networks:
      default: null
    ports:
    - mode: ingress
      target: 8085
      published: 8085
    - mode: ingress
      target: 443
      published: 443
      protocol: tcp
    restart: always
    volumes:
      source: ./nginx
      target: /etc/nginx/conf.d
      bind:
        create_host_path: true
    - type: bind
      source: ./logs/nginx
      target: /var/log/nginx
      bind:
        create_host_path: true
    - type: bind
      source: ./wordpress
      target: /var/www/html
      bind:
        create_host_path: true

Any tips would be appreciated.

2

Answers


  1. You’ve specified args in your docker-compose file but I don’t see the corresponding ARG in your Dockerfile? You’ll also need to be aware of how ARG and FROM interact

    Here is a minimal example where I was able to recreate your error and then fix it with the addition of ARG before the FROM statement

    version: "3.8"
    services:
      test:
        build:
          context: ./
          dockerfile: Dockerfile
          args:
            - ALPINE_VERSION=3.14
    
    ARG ALPINE_VERSION=3
    
    FROM alpine:${ALPINE_VERSION}
    
    ENTRYPOINT [ "sh" ]
    

    Error with missing ARG

    failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "alpine:": invalid reference format

    Login or Signup to reply.
  2. if you want your docker-compose file to read variables from .env file and then pass it to Dockerfile, you can follow along with the settings below:

    this is what your .env file contains:

    # nginx
    NGINX_VERSION=1.21.3
    

    this can be your Dockerfile:

    ARG NGINX_VERSION
    FROM nginx:${NGINX_VERSION}
    ...
    

    and finally this will be your docker-compose.yml file:

    nginx:
      build:
        context: .
        dockerfile: Dockerfile
        args:
          NGINX_VERSION: ${NGINX_VERSION}
    
      ...
    

    and finally run:

    docker-compose up
    

    be sure that your .env file is inside the same directory as your Dockerfile and docker-compose.yml files.

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