Home > Net >  Overwrite entrypoint in Dockerfile without using the command "docker run"
Overwrite entrypoint in Dockerfile without using the command "docker run"

Time:09-21

I have a problem with overwriting the entrypoint in my Dockerfile. For this (as I know) docker run with --entrypoint option is used, e.g.:

docker run --entrypoint "python3 main_script.py DEV"

However, in my case I can't use the docker run command because I have to use Helm charts for the deployments.

Is there an alternative to docker run using Helm charts, or other alternatives/solutions that I may not have thought of?

CodePudding user response:

use Helm charts for the deployments

This is easy: Kubernetes command: overrides Docker ENTRYPOINT (and Kubernetes args: matches Docker CMD). So your Deployment spec can just say

command:
  - python3
  - main_script.py
  - {{ .Values.environment }}

Usually, though, you shouldn't need to override ENTRYPOINT, especially in a docker run context. An ENTRYPOINT isn't required, and it's very easy to override CMD at the end of the docker run line. So if you change your Dockerfile to say

CMD python3 some_other_script.py
# with no ENTRYPOINT

then you can

docker run my-image \
  python3 main_script.py DEV

If that's not an option, then you need to be aware of the limitation that docker run --entrypoint only takes a single shell word. That means the first word of your command goes with --entrypoint and is a Docker option (before the image name), and the rest is interpreted as a command (after the image name).

docker run \
  --entrypoint python3 \
  my-image \
  main_script.py DEV

CodePudding user response:

First you may set the command as suggested in https://stackoverflow.com/a/69242677/15087442. This is desribed in detail in the kubernetes documentation: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/

But there's also another option: You can change the entrypoint of the docker image. If it is your own docker image that's a no-brainer. But let's consider you are using someone elses image. Then you can modify it by building another image out of it. Dockerfile:

FROM old_image:latest

ENTRYPOINT python3 main_script.py DEV

And then you build the image with docker build ., push it to your preferred docker repository with docker push and use that one instead of the original one.

This is mostly interesting if you also want to modify other things in the image.

  • Related