How can I use a variable inside a Dockerfile CMD?

Inside my Dockerfile:

ENV PROJECTNAME mytestwebsite
CMD ["django-admin", "startproject", "$PROJECTNAME"]

Error:

  • “Unable to access jarfile” when trying to run a jar with docker
  • Get docker container id from container name
  • How to connect with JMX from host to Docker container in Docker machine?
  • docker run fails to start command specified as CMD option (path, library no found)
  • how to diagnose 404 not found error nginx on docker?
  • How to correctly configure docker daemon listen on port, in Ubuntu16.04?
  • CommandError: '$PROJECTNAME' is not a valid project name
    

    What is the quickest workaround here? Does Docker have any plan to “fix” or introduce this functionality in later versions of Docker?

    NOTE: If I remove the CMD line from the Docker file and then run the Docker container, I am able to manually run Django-admin startproject $PROJECTNAME from inside the container and it will create the project…

  • Downloading file from S3 using boto3 inside Docker fails
  • How to migrate an existing service docker?
  • Query to dataframe very slow on Zeppelin
  • DNS failover with Docker Swarm 1.12
  • Docker-compose not attaching redis: redis does not start
  • Edit container project files from host
  • One Solution collect form web for “How can I use a variable inside a Dockerfile CMD?”

    When you use an execution list, as in…

    CMD ["django-admin", "startproject", "$PROJECTNAME"]
    

    …then Docker will execute the given command directly, without involving a shell. Since there is no shell involved, that means:

    • No variable expansion
    • No wildcard expansion
    • No i/o redirection with >, <, |, etc
    • No multiple commands via command1; command2
    • And so forth.

    If you want your CMD to expand variables, you need to arrange for a shell. You can do that like this:

    CMD ["sh", "-c" "django-admin startproject $PROJECTNAME"]
    

    Or you can use a simple string instead of an execution list, which gets you a result largely identical to the previous example:

    CMD django-admin startproject $PROJECTNAME
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.