基础镜像与当前镜像中ENTRYPOINT和CMD的工作机制及相关问题
Let's break down both questions clearly based on how Docker handles ENTRYPOINT and CMD inheritance and execution:
1. Will the base image's ENTRYPOINT ["entrypoint.sh"] execute before the current image's ENTRYPOINT ["echo"]?
Short answer: No
Docker doesn't stack or chain ENTRYPOINT instructions from parent and child images. When you define an ENTRYPOINT in your child Dockerfile (the ENTRYPOINT ["echo"] here), it completely replaces the ENTRYPOINT inherited from the base image (somedockerhub/backend-base).
When your app container starts, only the echo entrypoint will run. The base image's entrypoint.sh won't execute at all—there's no "before" or "after" execution here because the child image's ENTRYPOINT overrides the parent's entirely.
2. If the current image had no ENTRYPOINT defined, would the base image's ENTRYPOINT run first, then launch uwsgi as PID 1 via CMD?
Short answer: Yes, and here's exactly how it works:
- If your child Dockerfile doesn't set an ENTRYPOINT, it inherits the base image's
ENTRYPOINT ["entrypoint.sh"]by default. - The
commandspecified in your docker-compose file acts as the CMD that gets passed as arguments to the inherited ENTRYPOINT. - Looking at the base image's
entrypoint.shscript:
The#!/bin/bash . /appenv/bin/activate exec $@exec $@line is key here. When the script runs:- It first activates the Python virtual environment with
. /appenv/bin/activate. - Then
exec $@replaces the current bash process (which is PID 1 at startup) with the command specified in$@—this is your full uwsgi command from the docker-compose file.
- It first activates the Python virtual environment with
The end result is:
- The base image's
entrypoint.shruns first (as the initial PID 1). - After activating the virtual environment, the bash process is replaced by uwsgi, which becomes the new PID 1 process.
Without the exec call, the bash shell would stay as PID 1 and uwsgi would run as a child process—this can cause issues with signal handling (like not properly shutting down uwsgi when the container stops), so the exec here is intentional to make uwsgi the main container process.
内容的提问来源于stack exchange,提问作者overexchange

