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.

CMD ["executable", "arg1", "arg2"]

7. ENTRYPOINT:

  • Configures a container that will run as an executable.

ENTRYPOINT ["executable", "arg1", "arg2"]

8. EXPOSE:

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

EXPOSE port

9. ENV:

  • Sets environment variables in the form key=value.

ENV key=value

10. ARG:

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

ARG variable_name=default_value

11. VOLUME:

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

VOLUME /path/to/mountpoint

12. USER:

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

USER user[:group]

13. HEALTHCHECK:

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

HEALTHCHECK [options] CMD command

14. LABEL:

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

LABEL key="value" key2="value2"

15. ONBUILD:

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

ONBUILD instruction

16. SHELL:

  • Overrides the default shell used for the RUN instruction.

SHELL ["executable", "parameters"]

Example Dockerfile:

# Use an official base image
FROM ubuntu:latest

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install dependencies
RUN apt-get update && apt-get install -y python3

# Set environment variable
ENV NAME World

# Expose port
EXPOSE 80

# Run the command when the container starts
CMD ["python3", "app.py"]

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.