docker file

A Dockerfile is a script used to build a Docker image. It contains a set of instructions for assembling a containerized application. Below is a comprehensive reference for various instructions that can be used in a Dockerfile:

Dockerfile Instructions:

1. FROM:

  • Defines the base image for subsequent instructions.

FROM image_name:tag

2. WORKDIR:

  • Sets the working directory for subsequent instructions.

WORKDIR /path/to/directory

3. COPY:

  • Copies files or directories from the build context to the container.

COPY source destination

4. ADD:

  • Similar to COPY but allows for additional features like URL support and automatic extraction of compressed files.

ADD source destination

5. RUN:

  • Executes commands in a new layer on top of the current image.

RUN command

6. CMD:

  • Provides default command for the container when it starts. It can be overridden by command-line arguments.

7. ENTRYPOINT:

  • Configures a container that will run as an executable.

8. EXPOSE:

  • Informs Docker that the container will listen on the specified network ports at runtime.

9. ENV:

  • Sets environment variables in the form key=value.

10. ARG:

  • Defines variables that users can pass at build-time to the builder with the docker build command.

11. VOLUME:

  • Creates a mount point with specified name for externally mounted volumes.

12. USER:

  • Sets the username or UID and optionally the user group or GID to use when running the image.

13. HEALTHCHECK:

  • Tells Docker how to test a container to check that it is still working.

14. LABEL:

  • Adds metadata to an image in the form of key-value pairs.

15. ONBUILD:

  • Adds a trigger instruction to the image that will be executed at a later time.

16. SHELL:

  • Overrides the default shell used for the RUN instruction.

Example Dockerfile:

Remember that Dockerfiles are case-sensitive, and instructions must be written in uppercase. Additionally, Dockerfiles are usually kept in the root of the application code. The docker build command is used to build an image from a Dockerfile, and docker run is used to run a container from that image.