Haraball.no

Passing arguments to docker run

2016-09-06

A quick example on how to pass arguments to a python script running in a Dockerfile.

The hello.py Python script:

#!/usr/bin/python
import sys
print("Hello %s" % sys.argv[1])

Dockerfile:

FROM python:2.7-slim
COPY . /src
CMD ["python", "/src/hello.py"]

Put those two files in the same folder:

$ ls -1
Dockerfile
hello.py

Build the docker image:

docker build -t pytest .

Run the script with an argument

$ docker run pytest python /src/hello.py world
Hello world

Note that the argument index in the script is 1, not 0.