简体   繁体   中英

Override Dockerfile Entrypoint with Python and parameters/

in my Dockerfile:

ENTRYPOINT ["python3", "start1.py"]

When I run the docker image i want to override it with start2.py with a parameter year=2020 . So I run:

docker run -it --entrypoint python3 start2.py year 2020 b43ssssss

It still runs start1.py , what am i doing wrong?

For a couple of reasons, I tend to recommend using CMD over ENTRYPOINT as a default. This question is one of them: if you need to override the command at run time, it's much easier to do if you specify CMD .

# Change ENTRYPOINT to CMD
CMD ["python3", "start1.py"]
# Run an alternate script
docker run -it myimage \
  python3 start2.py year 2020 b43ssssss

# Run a debugging shell
docker run --rm -it myimage \
  bash

# Quickly double-check file contents
docker run --rm -it myimage \
  ls -l /app

# This is what you're trying to avoid
docker run --rm -it \
  --entrypoint /bin/ls \
  myimage \
  -l app

There is also a useful pattern of using ENTRYPOINT to run a secondary script that does some initial setup (waits for a database, rewrites config files, bootstraps a data store, ...) and then does exec "$@" to launch the CMD . I tend to reserve ENTRYPOINT for this pattern and default to CMD even if I don't specifically need it.

I do not recommend splitting the command with ENTRYPOINT ["python3"] . In the very specific case of wanting to run an alternate Python script it saves one word in the docker run command, but you still need to repeat the script name (unlike the "entrypoint-as-command" pattern) and you still need the --entrypoint option if you want to run something non-Python.

It still runs start1.py, what am i doing wrong?

Because anything pass as CMD with the entrypoint ENTRYPOINT ["python3", "start1.py"] will be pass as an argument to python file start1.py . You can verify this by doing the following

import argparse, sys
print  ("All ARGs",sys.argv[1:])

So the output will be

All ARGs ['start2.py', 'year', '2020', 'b43ssssss']

So Convert entrypoint to python3 only with some default CMD (start1.py) so you will have control which files to run.

ENTRYPOINT ["python3"]
# Default file to run
CMD ["start1.py"]

and then override at run time

docker run -it --rm my_image start2 year 2020 b43ssssss

Now the args should be

All ARGs ['year', '2020', 'b43ssssss']

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