Create directory in Dockerfile using ENV variable

11,228

You may successfully use ARG with ENV in your Dockerfile. See the docs for this:

You can use an ARG or an ENV instruction to specify variables that are available to the RUN instruction. Environment variables defined using the ENV instruction always override an ARG instruction of the same name. Consider this Dockerfile with an ENV and ARG instruction.

FROM ubuntu
ARG CONT_IMG_VER
ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
RUN echo $CONT_IMG_VER

So, in your case it should be

FROM python:3.6.5 as my_base
ARG CUSTOM_PATH=<some default path if you wish>
ENV CUSTOM_PATH=$CUSTOM_PATH
RUN mkdir $CUSTOM_PATH

After that you may build the image with docker build --build-arg CUSTOM_PATH=/var/log/whatever . and it should work.

Share:
11,228
user3535074
Author by

user3535074

Updated on June 04, 2022

Comments

  • user3535074
    user3535074 almost 2 years

    I'm trying to use docker and docker-compose to build a set of containers with some customized parameters using environment variables - for example, I want to mount a directory at a specific location in the container. This specific location is stored in an environment variable and created in the Dockerfile using a command like:

    RUN mkdir $custom_location
    

    I keep the parameters in an .env file which populates the docker-compose file.

    Once the containers are running, using the printenv command I can see that the transfer of the env variables has worked and I can manually run the command mkdir $custom_location but the command hasn't been successful during the dockerfile build process.

    A summarized version of the Dockerfile looks like this:

    FROM python:3.6.5 as my_base
    ENV CUSTOM_PATH=$CUSTOM_PATH
    RUN mkdir $CUSTOM_PATH
    

    A summarized version of the docker-compose.yml looks like this:

      service:
        environment:
          - CUSTOM_PATH=${CUSTOM_PATH}
        build: ./Docker
    

    The error I'm getting looks like this:

    Step 20/20 : RUN mkdir $CUSTOM_PATH ---> Running in 437aa3f09dbb mkdir: missing operand Try 'mkdir --help' for more information. The command '/bin/sh -c mkdir $CUSTOM_PATH' returned a non-zero code: 1

    Why would this be?

    The ENV variables are successfully in the running container. Using printenv I can see them.

    • Mazel Tov
      Mazel Tov almost 6 years
      Dockerfile doesnt load .env file, so when you build the image the variable is blank... if you need to add some variable when building image you have to do ARG in Dockerfile and pass it there when you do docker build -t dude --build-arg CUSTOM_PATH=cool .
    • user3535074
      user3535074 almost 6 years
      Is there no way I can avoid passing the arg as a parameter in the docker build command? I would like to avoid a síngle parameter being passed like that with the rest all loaded into docker compose from a .env file
    • Mazel Tov
      Mazel Tov almost 6 years
      You could add in dockerfile something like COPY .env /my_env.txt , that way you will have the content of .env in your image, and then you can do some fancy stuff with shell on that
    • user3535074
      user3535074 almost 6 years
      Nice idea - thanks! Though it seems that it isn't the docker way...
    • Mazel Tov
      Mazel Tov almost 6 years
      i would not recommend it either and it depends on your setup, i always put .env in .gitignore and make it unique across machines. I dont know your app but you are in charge of the image (application) and i would suggest not to do this at all, just hardcode the folder and then let user in docker-compose.yml where he mounts it on his machine
  • user3535074
    user3535074 almost 6 years
    Thanks this works for me. Is there any way to avoid passing the arg as a parameter of the build command?
  • Kevin Kopf
    Kevin Kopf almost 6 years
    only to set the default value. It's all described in the docs. Read the docs.