Operation and Maintenance
Docker
How to override the default entrypoint in Docker? (Command line)
How to override the default entrypoint in Docker? (Command line)
--entrypoint can directly overwrite the image ENTRYPOINT, only accepts a single executable path, and the parameters must be placed after the image name; the shell form must be separated by --, such as --entrypoint sh -- -c "cmd"; use "" or [] to clear the entry.

Use --entrypoint when docker run to override the default entry of the image
It takes effect directly without changing the Dockerfile or rebuilding the image. It will completely replace ENTRYPOINT defined in the image, including shell form (such as ["sh", "-c"] ) and exec form.
-
--entrypointis followed only by an executable file path (such as/bin/bash) and cannot contain parameters; parameters must be written after the image name - If the original image defines
ENTRYPOINTin shell form (for example,ENTRYPOINT node app.js), after overwriting with--entrypoint, the original command will not be automatically appended - you have to fill in the originalCMDyourself, otherwise nothing will be executed. - Common misoperations:
docker run --entrypoint /bin/sh myimage -c "echo hello"- here-cwill be used as the parameter after the image name, but/bin/shdoes not accept-cby default, and must be explicitly written as/bin/sh -c
Want to keep the original CMD and only change the entrance? Clear ENTRYPOINT with empty string ""
Some images put all the startup logic into ENTRYPOINT , and CMD is just a parameter. At this time, if you don’t want to change the entry, but just want to temporarily skip it and run the command directly, you must first clear the entry and then run the original CMD as a normal command.
- Execute an interactive shell:
docker run --entrypoint "" -it nginx /bin/sh- This bypasses the default startup script of nginx and connects directly to the shell - Note: If the image does not define
CMDand there is no command after--entrypoint "", the container will exit immediately (because there is nothing to execute) - This technique is particularly fast when debugging init containers or troubleshooting startup failures. It is more trouble-free than entering the container and then
psto check the process link.
The command is not executed after overwriting? Check exec vs shell mode differences
Docker's processing of --entrypoint strictly follows the exec mode: it only recognizes executable paths in the form of arrays and does not parse shell syntax. If you write --entrypoint "sh -c" in the habit, Docker will try to execute a file named "sh -c" (which obviously doesn't exist).
- The correct way to write it is:
--entrypoint sh -- -c "echo hello"-- the firstshis the entry, and everything after the--separator is passed in as the parameter ofsh - Another equivalent way of writing:
--entrypoint /bin/sh -- -c "echo hello", a clearer path - Error example:
--entrypoint "sh -c 'echo hello'"→ errorexecutable file not found in $PATH - This is inconsistent with the behavior of shell form
ENTRYPOINTindocker buildand is easy to confuse.
How to configure it in docker-compose.yml? The corresponding field is entrypoint
Not command , not cmd . It is completely equivalent to the CLI's --entrypoint , and the value can be a string or an array.
- Array writing method (recommended):
entrypoint: ["/bin/sh", "-c"], clear and unambiguous - String writing method:
entrypoint: "/bin/sh -c", Docker will split it into an array internally, but it is prone to errors when encountering spaces or quotation marks, so it is not recommended. - If you want to clear the entry, write
entrypoint: [](empty array), notnullor empty string - Note:
entrypointandcommandare two independent fields in compose.commandis equivalent toCMDof the image and will be passed toentrypointas a parameter.
In actual use, the most common thing that gets stuck is shell string parsing - you think you wrote a command, but in fact Docker looks for it as a file name. Pay attention to executable file not found in the error message. This is basically the problem.
The above is the detailed content of How to override the default entrypoint in Docker? (Command line). For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20572
7
13671
4




