What are Dockerfile Directives

What are Dockerfile Directives

Dockerfile Directives

Dockerfile directives are the set of commands that you can use to define the steps needed to create a Docker image. Each directive in a Dockerfile performs a specific function, such as specifying a base image, installing dependencies, or configuring how the container should behave when it runs. These directives are written in the Dockerfile in a specific order, and each one has a purpose in building the image.

Here are the main Dockerfile directives you’ll encounter:

1. FROM

The the FROM directive sets the base image for the Docker image you are building. Every Dockerfile must start with a FROM directive.

Syntax:

FROM <image>:<tag>

Example:

FROM ubuntu:20.04

This uses the official ubuntu image, version 20.04, as the base for the Docker image.

2. LABEL

The the LABEL directive adds metadata to the image. Labels are key-value pairs that can describe information such as the image's maintainer, version, and more.

Syntax:

LABEL <key>=<value>

Example:

LABEL maintainer="youremail@example.com" LABEL version="1.0"

This adds metadata specifying the maintainer and version of the image.

3. RUN

The The RUN directive executes commands in a new layer on top of the current image and commits the results. It's typically used to install dependencies or make modifications to the image (e.g., running package managers like apt-get, npm, etc.).

Syntax:

RUN <command>

Example:

RUN apt-get update && apt-get install -y python3

This updates the package lists for Ubuntu and installs Python 3.

4. COPY

The The COPY directive copies files and directories from the host machine into the Docker image. The syntax allows you to specify the source and destination paths.

Syntax:

COPY <src> <dest>

Example:

COPY ./app /usr/src/app

This copies everything from the ./app directory on the host machine into /usr/src/app inside the image.

5. ADD

Similar to COPY, the ADD directive copies files and directories into the image, but it also has additional features, such as automatically extracting compressed files (e.g., .tar files) and supporting URLs as sources.

Syntax:

ADD <src> <dest>

Example:

ADD https://example.com/file.tar.gz /tmp

This downloads a tarball file from the URL and adds it to /tmp.

Note: Use COPY for simple copying of files as it's more predictable. ADD is useful when you need to handle compressed files or URLs.

6. WORKDIR

The the WORKDIR directive sets the working directory for subsequent commands in the Dockerfile. If the directory doesn’t exist, it will be created.

Syntax:

WORKDIR <dir>

Example:

WORKDIR /usr/src/app

This sets /usr/src/app as the working directory for all subsequent commands.

7. CMD

The the CMD directive specifies the default command to run when a container is started from the image. You can only have one CMD in a Dockerfile. If multiple CMD directives are provided, only the last one is used.

Syntax:

CMD ["executable", "param1", "param2"]

Or

CMD ["param1", "param2"]

Example:

CMD ["node", "server.js"]

This tells Docker to run the node server.js command when the container starts.

8. ENTRYPOINT

The A ENTRYPOINT directive defines the command that will always run when the container starts, and it’s often used together with CMD for additional flexibility. The ENTRYPOINT is used to set a fixed command, while CMD can be used to provide default arguments.

Syntax:

ENTRYPOINT ["executable", "param1", "param2"]

Example:

ENTRYPOINT ["python3", "app.py"]

This ensures that when the container starts, it always runs python3 app.py.

If you use both ENTRYPOINT and CMD, CMD will be passed as arguments to ENTRYPOINT.

9. EXPOSE

The The EXPOSE directive informs Docker that the container will listen on the specified network ports at runtime. It does not actually expose the ports; it serves as documentation and helps Docker with networking.

Syntax:

EXPOSE <port>

Example:

EXPOSE 8080

This indicates that the container will listen on port 8080.

10. ENV

The the ENV directive sets an environment variable inside the container. These variables can be accessed by the application running inside the container.

Syntax:

ENV <key>=<value>

Example:

ENV NODE_ENV=production

This sets the environment variable NODE_ENV to production.

11. VOLUME

The the VOLUME directive creates a mount point inside the container that can be used for persistent data storage. You can use volumes to persist data even if the container is removed.

Syntax:

VOLUME ["/path/to/dir"]

Example:

VOLUME ["/data"]

This creates a mount point at /data Inside the container.

12. USER

The the USER directive sets the user name or UID (and optionally the group) to use when running the container. This is important for running the application as a non-root user.

Syntax:

USER <username or UID>

Example:

USER node

This ensures the container runs as the node user.

13. ARG

The A ARG directive defines build-time variables that can be passed during the docker build process. They are not available after the image is built (unlike environment variables set with ENV).

Syntax:

ARG <variable_name>[=<default_value>]

Example:

ARG VERSION=1.0

This sets the VERSION argument, which can be used in the Dockerfile during the build.

14. STOPSIGNAL

The the STOPSIGNAL directive specifies the signal to send to the container to gracefully stop it. The default signal is SIGTERM.

Syntax:

STOPSIGNAL <signal>

Example:

STOPSIGNAL SIGINT

This sets the stop signal to SIGINT, which is commonly used for graceful shutdowns.

15. HEALTHCHECK

The A HEALTHCHECK directive defines a command that Docker can run to check if the container is still healthy. If the container fails the health check, it can be marked as unhealthy.

Syntax:

HEALTHCHECK [OPTIONS] CMD <command>

Example:

HEALTHCHECK CMD curl --fail http://localhost:8080/ || exit 1

This checks the health of the container by running curl to see if the application inside is responding.

16. SHELL

The SHELL directive allows you to specify which shell to use for RUN commands. The default shell is /bin/sh -c, but you can change it to something else like bash.

Syntax:

SHELL ["executable", "parameters"]

Example:

SHELL ["/bin/bash", "-c"]

Summary of Key Dockerfile Directives

  1. FROM: Specifies the base image.

  2. LABEL: Adds metadata.

  3. RUN: Executes commands inside the image.

  4. COPY: Copies files from host to image.

  5. ADD: Similar to COPY, but also supports URLs and auto-extraction.

  6. WORKDIR: Sets the working directory.

  7. CMD: Specifies the default command to run.

  8. ENTRYPOINT: Defines the fixed command to run.

  9. EXPOSE: Exposes a port.

  10. ENV: Sets environment variables.

  11. VOLUME: Defines a mount point for persistent data.

  12. USER: Sets the user to run commands.

  13. ARG: Defines build-time variables.

  14. STOPSIGNAL: Sets the stop signal for the container.

  15. HEALTHCHECK: Defines a health check command.

  16. SHELL: Specifies the shell to use for RUN commands.

Each of these directives serves a specific purpose in building and configuring Docker images. They can be combined to automate the building process of images with necessary dependencies, environment variables, and configurations.

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close