DEV Community

Subham Nandi
Subham Nandi

Posted on

Docker: Day 4

Dockerfiles and Docker Images

A Dockerfile is a text document containing instructions to build Docker images. These images consist of read-only layers, each layer corresponding to a Dockerfile instruction. Docker can automatically create images by interpreting these instructions. The following command is used to build an image from a Dockerfile:

docker build -f <dockerfile_path>
Enter fullscreen mode Exit fullscreen mode

Dockerfile Instructions

1. FROM

The FROM instruction initializes a new build stage and sets the base image for subsequent instructions. A valid Dockerfile must begin with a FROM instruction. The base image can be any valid image.

Syntax:

FROM <Image_name>:<Image_tag>
Enter fullscreen mode Exit fullscreen mode

2. LABEL

The LABEL instruction adds metadata to an image, helping to organize images by project or record licensing information. Each label is defined as a key-value pair.

Examples:

LABEL com.example.version="0.0.1-beta"
LABEL vendor1="ACME Incorporated"
Enter fullscreen mode Exit fullscreen mode

3. RUN

The RUN instruction executes commands in a new layer on top of the current image and commits the results. The resulting image serves as the base for the next step.

Example:

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y curl
Enter fullscreen mode Exit fullscreen mode

4. CMD

The CMD instruction specifies the default command to run in the container. Only one CMD instruction is allowed per Dockerfile; the last CMD overrides any previous ones.

Syntax:

CMD ["executable", "param1", "param2"]
Enter fullscreen mode Exit fullscreen mode

5. EXPOSE

The EXPOSE instruction specifies the ports on which the container listens for connections.

Syntax:

EXPOSE <port>
Enter fullscreen mode Exit fullscreen mode

6. ENV

The ENV instruction sets environment variables for the container. It can also update the PATH variable to make software easier to run.

Example:

ENV PATH /usr/local/nginx/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

7. ADD

The ADD instruction copies files, directories, or remote URLs from the source to the specified destination within the image.

Example:

ADD hom* /mydir/  # Adds all files starting with “hom”
Enter fullscreen mode Exit fullscreen mode

8. VOLUME

The VOLUME instruction designates storage areas, such as database files or configuration storage, that should be persisted outside the container.

9. WORKDIR

The WORKDIR instruction sets the working directory for subsequent RUN, CMD, and ADD instructions.


Top comments (0)