Abstract
- Set of steps for Docker to build Docker Container
Important
We can use a
.dockerignore
file to exclude files from being copied into a Docker image. It’s best practice to copy over the dependency files and install dependencies before copying the application files to take advantage of layer caching.
Keyword at Runtime
- Keywords that have an effect when Docker Container is running
ENV
- Set the Environment Variable in the Docker Container
CMD
- Purpose: Provides default commands or arguments that can be overridden at runtime. If you specify a command when starting the container (
docker run <image> <command> <args>
), it overrides theCMD
- Typically used for setting default arguments when a container starts (e.g., parameters for the main application specified with ENTRYPOINT)
- Exec form:
CMD ["executable", "param1", "param2"]
(preferred as it doesn’t run in a shell). ExampleCMD ["python", "app.py"]
ENTRYPOINT
- Purpose: Defines the main command that will always run in the container, and you can optionally provide default arguments with CMD. It is not overridden by arguments passed to
docker run <image> <args>
(unlikeCMD
). The container will always execute the command defined in ENTRYPOINT, and any arguments passed at runtime will be appended to it - Exec form:
ENTRYPOINT ["executable", "param1"]
(recommended)
CMD + ENTRYPOINT
ENTRYPOINT
specifies the command that will always runCMD
provides default arguments to theENTRYPOINT
- In the above example, if you run
docker run <image> script.py
, it will executepython script.py
becauseCMD
can be overridden CMD
does not execute as a separate process whenENTRYPOINT
specifies a complete command. Instead, it is effectively ignored in that context.- Refer to HashiCorp Vault Dockerfile for an example