Passing variable from container start to file

I have the following lines in a Dockerfile where I want to set a value in a config file to a default value before the application starts up at the end and provide optionally setting it using the -e option when starting the container.

I am trying to do this using Docker’s ENV commando

  • Docker Remote API JSON schema definition
  • Where should a dockerized web application store uploaded files?
  • Docker exec rpc error code =13 desc = invalid header field value “oci runtime error: exec failed: cannot exec a container that has run and stopped\n”
  • How to create docker image with .net core that reference to .net 4.6 class library
  • Docker Java Image cannot connect to Docker Mongo
  • Docker container - node application - run issue
  •  ENV CONFIG_VALUE default_value
     RUN sed -i 's/CONFIG_VALUE/'"$CONFIG_VALUE"'/g' CONFIG_FILE
     CMD command_to_start_app
    

    I have the string CONFIG_VALUE explicitly in the file CONFIG_FILE and the default value from the Dockerfile gets correctly substituted. However, when I run the container with the added -e CONFIG_VALUE=100 the substitution is not carried out, the default value set in the Dockerfile is kept.

    When I do

    docker exec -i -t container_name bash
    

    and echo $CONFIG_VALUE inside the container the environment variable does contain the desired value 100.

  • Merge two docker images
  • Xdebug on Docker doesn't work with PhpStorm
  • How to use properly centos dockerfile with ubuntu host?
  • Logging from one docker container to another
  • How to access jupyter notebook from docker on server?
  • Removing a file from a docker container
  • 2 Solutions collect form web for “Passing variable from container start to file”

    That shouldn’t be possible in a Dockerfile: those instructions are static, for making an image.

    If you need runtime instruction when launching a container, you should code them in a script called by the CMD directive.
    In other words, the sed would take place in a script that the CMD called. When doing the docker run, that script would have access to the environment variable set just before said docker run.

    Instructions in the Dockerfile are evaluated line-by-line when you execute docker build and are not re-evaluated at run-time.

    You can still do this however by using an entrypoint script, which will be evaluated at run-time after any environment variables have been set.

    For example, you can define the following entrypoint.sh script:

    #!/bin/bash
    
    sed -i 's/CONFIG_VALUE/'"$CONFIG_VALUE"'/g' CONFIG_FILE
    exec "$@"
    

    The exec "$@" will execute any CMD or command that is set.

    Add it to the Dockerfile e.g:

    COPY entrypoint.sh /
    RUN chmod +x /entrypoint.sh
    ENTRYPOINT ["/entrypoint.sh"]
    

    Note that if you have an existing entrypoint, you will need to merge it with this one – you can only have one entrypoint.

    Now you should find that the environment variable is respected i.e:

    docker run -e CONFIG_VALUE=100 container_name cat CONFIG_FILE
    

    Should work as expected.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.