Dockerfile

Hey everyone, This article is about the Dockerfile that we use in building container images. It is nothing but a simple text file with instructions to build a custom image. Docker file helps us to provide instructions on what needs to be pulled, what arguments need to be run after building the image, and providing some configurations.

  1. FROM: Specifies the base image to use for the build. It is typically the first instruction in a Dockerfile. Example: FROM ubuntu:latest
  2. RUN: Executes commands in the shell of the container. These commands are run during the build process to install packages, configure the environment, etc. Example: RUN apt-get update && apt-get install -y python3
  3. COPY or ADD: Copies files or directories from the host machine into the image. COPY is preferred for simple file copying, while ADD has additional features like extracting tar files and downloading files from URLs. Example: COPY app.py /app/
  4. WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, or ADD instructions that follow it in the Dockerfile. Example: WORKDIR /app
  5. CMD: Specifies the command to run when a container is launched from the image. If the Dockerfile has multiple CMD instructions, only the last one will take effect. Example: CMD ["python", "app.py"]
  6. EXPOSE: Informs Docker that the container listens on the specified network ports at runtime. This does not actually publish the port. Example: EXPOSE 80
  7. ENV: Sets environment variables in the container. Example: ENV DB_HOST=localhost
  8. ENTRYPOINT: Sets the executable to run when the container starts. Unlike CMD, the command and arguments are not overwritten when the container is run with alternative commands. Example: ENTRYPOINT ["python", "app.py"]
  9. VOLUME: Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. Example: VOLUME /var/log
  10. ARG: Defines a build-time argument that can be used in the Dockerfile. Example: ARG VERSION=latest
  11. LABEL: Adds metadata to the image. This can be useful for organizing and documenting images. Example: LABEL maintainer="john.doe@example.com"
  12. USER: Sets the user that the image should run as when the container starts. This is useful for security reasons to run the container with limited privileges. Example: USER appuser
  13. ONBUILD: Adds a trigger instruction to the image that will be executed when the image is used as the base for another image. This is useful for building images that are meant to be extended. Example: ONBUILD COPY . /app
  14. STOPSIGNAL: Sets the system call signal that will be sent to the container to stop it. This can be useful for controlling how the container is shut down. Example: STOPSIGNAL SIGTERM
  15. HEALTHCHECK: Defines a command to periodically check the health of the container. This can be used by orchestration tools like Docker Swarm and Kubernetes to automatically restart unhealthy containers. Example: HEALTHCHECK --interval=5m --timeout=3s CMD curl -f http://localhost/ || exit 1
  16. SHELL: Overrides the default shell used by the RUN instruction. This can be useful for using a different shell syntax or for running commands in a specific shell environment. Example: SHELL ["/bin/bash", "-c"]
  17. RUN --mount: Allows you to mount a volume during the build process. This is useful for accessing files or directories from the host machine or other containers during the build. Example: RUN --mount=type=secret,id=mysecret cat /run/secrets/mysecret

Conclusion
Dockerfiles are used to define the steps needed to create a Docker image. These commands allow developers to specify the base image, execute commands during the build process, copy files into the image, set environment variables, expose ports, define the entry point and default command for the container, and more. Understanding these commands and how to use them effectively is essential for building efficient and functional Docker images.

Comments

Popular posts from this blog

How to pass parameters in webhook?

Access and modify all the resources of our Wiki.js using WikiJS API

Fahrenheit to Celsius