简体   繁体   中英

Docker pass command-line arguments

I really just want to pass an argument via docker run My Dockerfile:

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# tell the port number the container should expose
EXPOSE 5000

# run the command
CMD ["python", "./app.py"]

My python file:

import sys
print(sys.argv)

I tried:

docker run myimage foo

I got an error:

  flask-app git:(master) ✗ docker run myimage foo
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"foo\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled

When you write foo at the end of your docker run command then you overwrite whole command. Therefore instead of

python app.py

you call

foo

Proper way of calling your script with arguments is:

docker run myimage python app.py foo

Alternatively you may use ENTRYPOINT instead of CMD and then your docker run command may contain just foo after image name

Dockerfile:

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY app.py .

# run the command
ENTRYPOINT ["python", "./app.py"]

calling it:

docker run myimage foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM