Table of contents
Creating a docker image makes use of docker file. It contains specific command or syntax to tell docker what we want.
Commands
FROM:
Base image for new image
WORKDIR
Instruction is used to set the working directory for any
RUN
,CMD
,ENTRYPOINT
,COPY
, andADD
instructions that follow it in the Dockerfile. It doesn't create a new directory but rather sets the working directory for subsequent instructions.COPY
instruction is used to copy files or directories from the build context (the directory containing the Dockerfile) into the filesystem of the container.
RUN
Instruction is used to execute commands during the image build process.
It allows you to install packages, update configurations, or perform any other tasks necessary for setting up your application or environment within the Docker image.
EXPOSE
Instruction informs Docker that the container listens on specified network ports at runtime. It does not actually publish the specified ports; it serves as documentation for the user about the ports to which the service inside the container will respond.
ENV
instruction is used to set environment variables. Environment variables can be used to customize the behavior of software running in a Docker container.
ARG
Instruction is used to define build-time variables. These variables are set during the build and can be used as variables in the Dockerfile. They provide a way to pass information into the Docker build process.
VOLUME
instruction in a Dockerfile is used to create a mount point and associate it with a directory in the container that can be used for persisting data. It is often used to handle data that should persist outside the lifecycle of the container, such as database files, configuration files, or user uploads.
CMD
instruction provides the default command or parameters to execute when the container starts. It is not executed during the image build but specifies the primary command that should run when the container is launched.
ENTRYPOINT
instruction in a Dockerfile is used to set the primary command that will be executed when a container is run. It allows you to configure a container to run a specific command as the default command, and it can also be used to pass default arguments to the command.
ENTRYPOINT VS CMD
Both are instruction for defining the default command to run when a container starts
CMD | ENTRYPOINT |
FLEXIBLE, can be overridden | Cannot be overridden |
Default executable that can be changed | Fixed starting point that can't be overridden |
When both CMD
and ENTRYPOINT
instructions are used in a Dockerfile, the CMD
instruction provides default arguments for the ENTRYPOINT
instruction. This means that if a user runs a container without specifying a command, the command defined in CMD
will be used as arguments to the command defined in ENTRYPOINT
.
The basic syntax looks like this:
ENTRYPOINT ["executable", "param1", "param2"]
CMD ["param3", "param4"]
Here's an example to illustrate this:
FROM ubuntu:20.04
# Set the entry point to echo
ENTRYPOINT ["echo", "Hello"]
# Set default command arguments
CMD ["world"]
In this example:
The
ENTRYPOINT
is set to theecho
command with the initial parameters "Hello".The
CMD
sets default command arguments to "world".
When you run a container without specifying a command, the default command from CMD
is used as arguments to the command specified in ENTRYPOINT
:
docker run myimage
This will effectively run:
echo Hello world
If you run the container with a specific command, it overrides both CMD
and ENTRYPOINT
:
docker run myimage "Goodbye"
This will run:
echo Goodbye
In summary, when both CMD
and ENTRYPOINT
are used, CMD
provides default arguments to the command specified in ENTRYPOINT
. Users can still override both the CMD
and ENTRYPOINT
by providing a command when running the container.
EXAMPLE
REFERENCE
EXTRA COMMANDS: