Is it possible to pass arguments in dockerfile?

I am trying to pass arguments to dockerfile. My dockerfile:
FROM ubuntu:latest
MAINTAINER Abhay Kumar Somani
CMD echo $var1

Now I want to pass the value for var1 while starting the docker instead of making it static. For example
docker run -i -t ABHAYKUMARSOMANI should perform echo ABHAYKUMARSOMANI after starting the container.
Is there any way I can achieve this?

you can pass an environment variable with the -e switch, so your command to start will be

docker run -i -t -e var1=ABHAYKUMARSOMANI

Create a shell script like the following, in the same directory as your Dockerfile:

#!/bin/sh
echo "$1"

Then change the Dockerfile to add that script and use it as the entrypoint script:

FROM ubuntu:16.04
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
CMD ["default_cmd"]  # delete this line

When you docker run this, Docker runs the ENTRYPOINT, passing either what you passed on the command line or the CMD as arguments to it.

$ docker build -t test .
$ docker run test
default_cmd
$ docker run test hi
hi
$ docker run test hello world
hello

Note that this setup can interfere with some more normal debugging setups

$ docker run test /bin/bash
/bin/bash
$ docker run --entrypoint /bin/bash test
container$
2 Likes