Essential Dockerfile Commands

Dockerfiles are the blueprint to define Docker images, providing step-by-step instructions for Docker to build containerized applications. This guide explores essential Dockerfile commands with practical examples to help you understand their usage and importance in Docker containerization.
Certainly! Let’s go through each Dockerfile command with examples to illustrate their usage and importance in Docker containerization.
1. FROM
Specifies the base image from which you are building.
Example: Using an official Python image as the base.
FROM python:3.11
2. RUN
Executes commands in a new layer on top of the current image and commits the results.
Example: Installing system packages and dependencies.
RUN apt-get update && apt-get install -y \
package1 \
package2
3. COPY
Copies files or directories from the host into the container’s filesystem.
Example: Copying application code into the container.
COPY ./app /app
4. ADD
Similar to COPY but with additional features such as auto-extraction of compressed files.
Example: Adding a compressed archive and extracting it.
ADD my-archive.tar.gz /extracted-folder/
5. WORKDIR
Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile.
Example: Setting the working directory to `/app`.
WORKDIR /app
6. CMD
Provides defaults for an executing container. It can be overridden at the command line when starting a container.
Example: Running a Python script as the default command.
CMD ["python", "app.py"]
7. ENTRYPOINT
Configures a container that will run as an executable.
Example: Using a custom entrypoint script.
ENTRYPOINT ["/entrypoint.sh"]
CMD ["python", "app.py"]
8. EXPOSE
Informs Docker that the container listens on specific network ports at runtime. It does not actually publish the port.
Example: Exposing port 8000 for a web server.
EXPOSE 8000
9. ENV
Sets environment variables in the container.
Example: Setting environment variables.
ENV DB_HOST=localhost \
DB_PORT=5432
10. ARG
Defines variables that users can pass at build-time to the builder with the `docker build` command using the ` — build-arg <varname>=<value>` flag.
Example: Using build-time arguments.
ARG VERSION=latest
RUN echo "Building version: $VERSION"
11. LABEL
Adds metadata to an image.
Example: Adding labels to the image.
LABEL maintainer="John Doe <john.doe@example.com>"
LABEL version="1.0"
12. VOLUME
Creates a mount point and marks it as holding externally mounted volumes from native host or other containers.
Example: Defining a volume for persistent data.
VOLUME /data
13. USER
Sets the username or UID to use when running the image.
Example: Running the container as a non-root user.
USER appuser
14. HEALTHCHECK
Specifies a command to run to check the health of a container.
Example: Checking the health of a web server.
HEALTHCHECK CMD curl - fail http://localhost/ || exit 1
15. ONBUILD
Adds a trigger instruction to be executed at a later time, when the image is used as the base for another build.
Example: Defining actions to be triggered in child images.
ONBUILD COPY ./config /app/config
ONBUILD RUN pip install -r /app/config/requirements.txt
16. STOPSIGNAL
Sets the system call signal that will be sent to the container to exit.
Example: Setting the stop signal to SIGTERM.
STOPSIGNAL SIGTERM
Conclusion
These Dockerfile commands provide powerful capabilities for defining and customizing Docker images tailored to your application’s needs. By mastering these commands and using them effectively, you can streamline the containerization process, ensure consistency across different environments, and optimize the deployment of your applications in Docker containers. Experiment with these commands in your Dockerfiles to build robust and efficient Docker images for various use cases.
Rohit Kumar is a passionate software evangelist. Who loves implementing, breaking and engineering software products. He actively engages on platforms such as LinkedIn, GitHub, & Medium through email.