CompTIA Linux+ (XK0-006)Services and User ManagementHard
A system administrator wants to configure a container image to automatically run a specific command, `/usr/local/bin/start-app.sh`, every time a new container is launched from it, without requiring the user to specify the command manually during `docker run`. Which Dockerfile instruction should be used for this purpose?
- ARUN /usr/local/bin/start-app.sh
- BCMD ["/usr/local/bin/start-app.sh"]
- CENTRYPOINT ["/usr/local/bin/start-app.sh"]
- DEXEC /usr/local/bin/start-app.sh
Show answer & explanationAnswer & explanation
Correct answer: B. CMD ["/usr/local/bin/start-app.sh"]
The `CMD` instruction in a Dockerfile provides defaults for an executing container. These defaults can include an executable, or they can be arguments to an `ENTRYPOINT` instruction. If a `CMD` instruction is defined with an executable, it will be the default command executed when a container starts, unless overridden by the `docker run` command.
Why the other options are wrong
- A. The `RUN` instruction executes commands during the *image build process*, creating a new layer in the image. It does not define what runs when the container starts.
- C. The `ENTRYPOINT` instruction also defines the command to execute, but it is typically used for defining the primary executable of the container, which `CMD` then provides arguments to. `ENTRYPOINT` is harder to override.
- D. `EXEC` is not a valid Dockerfile instruction.
Dockerfile CMD Instruction
The `CMD` instruction in a Dockerfile provides the default command or parameters that are executed when a container is launched from the image. It can be easily overridden by specifying a command in the `docker run` command.
- Defines default command for container execution.
- Only one `CMD` per Dockerfile (last one wins).
- Can be overridden by `docker run <image> <command>`.
Memory trick: CMD gives the default COMMAND for the container.