What is Dockerfile? What are Dockerfile Commands?

dockerfile

Dockerfile is the configuration file used for the creation of Docker containers. The Dockerfile specifies how to run the application and which dependencies to install. Docker creates a container using this Dockerfile and converts this container into a file called an image. This image can then be run on any system.

Dockerfile is a script file that defines the file system. Dockerfile consists of line-by-line configuration commands, and each command performs an operation, such as creating a file or directory, modifying the contents of a file or directory. The Dockerfile specifies how the application will be run and which dependencies to install, so that everything needed for the application to run is taken into account when creating the Docker container. The Dockerfile also specifies the processes running inside the container and how to manage them.

Dockerfile consists of many different configuration commands. For example, the FROM command can be used to specify the base image on which a container will run. The RUN command can be used to specify the commands to run in the container. The COPY command can be used to copy files or directories into a container. The EXPOSE command can be used to specify the ports from which the container can be accessed.

Find out about the Docker key commands: https://devopstipstricks.com/mastering-docker-essential-key-commands-with-real-world-examples/

How to Create a Dockerfile?

You can follow the steps below to create a simple Dockerfile:

Create a file and name it Dockerfile.

Type the FROM command at the beginning of the file and specify the base image on which the container will run. For example, with the FROM ubuntu:latest command, you specify that it will use the latest version of Ubuntu.

Specify the commands to run in the container with RUN commands. For example, with the RUN apt-get update command, you can update the apt-get package manager in the container.

You can copy files or directories into the container with COPY commands. For example, with the COPY index.html /var/www/html command, the index.html file can be copied to the /var/www/html directory in the container.

Specify from which ports the container can be accessed with the EXPOSE command. For example, with the EXPOSE 80 command you specify that the container can be accessed from port 80.

With the CMD command, specify the commands to run when the container is running. For example, we run the nginx command when the container is running and use the parameters “-g” and “daemon off;”.

FROM ubuntu:latest

RUN apt-get update

COPY index.html /var/www/html/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Once these steps are complete, the Dockerfile will be created and a container can be created using this Dockerfile with the docker build command. For example, docker build -t my-app. will create a container in the directory where the Dockerfile is located. The name of this container will be my-app and it can be run with the docker run command.

Dockerfile Commands and Explanations

Here are some important commands you can use in Dockerfile:

FROM: This command specifies the base image you will use. For example, FROM ubuntu:latest.

MAINTAINER: This command specifies the name and email address of the creator of the Dockerfile. For example, MAINTAINER DevOps Tips and Tricks.

RUN: This command is used to run commands on the container. For example, RUN apt-get update && apt-get install -y nginx.

CMD: This command specifies the commands to be executed when the container is run. For example, CMD [“nginx”, “-g”, “daemon off;”].

EXPOSE: This command specifies the open ports on the container. For example, EXPOSE 80.

COPY: This command copies files or directories from the host system to specific directories in the container. For example, COPY index.html /var/www/html/.

ENV: This command defines the environment variables to be used in the container. For example, ENV MYSQL_ROOT_PASSWORD=secretpassword.

ADD: This command copies files or directories on the host system to specific directories in the container and also downloads URLs. For example, ADD https://example.com/myfile.txt /path/in/container.

ENTRYPOINT: This command specifies commands to be executed when the container is run. This command is used in conjunction with the CMD command. The ENTRYPOINT command can take the arguments of the CMD command. For example, ENTRYPOINT [“nginx”], CMD [“-g”, “daemon off;”].

LABEL: This command is used to add additional metadata to the container. For example, LABEL version=”1.0”.

USER: This command specifies which user will execute the commands to be run on the container. For example, USER root.

WORKDIR: This command specifies the default directory to run commands on the container. For example, WORKDIR /var/www.

ARG: This command defines the variables to be used during build. For example, ARG version=latest.

ONBUILD: This command, when building on a container, specifies the commands for the container to be built on top of that container.

STOPSIGNAL: This command specifies how to stop the container.

HEALTHCHECK: This command is used to check if the container is healthy.

SHELL: This command specifies the shell to be used on the container. For example SHELL [“/bin/bash”, “-c”].

These commands are just some of them, there are many other commands you can use in Dockerfile. You can find more detailed information about the purpose and usage of each command in Docker’s official documentation.

Dockerfile Stage Usage

Dockerfiles can also be divided into several different stages. Each stage can be run independently of each other, so that it is not necessary to run part of the Dockerfile repeatedly. For example, in the first stage of a Dockerfile, an application can be compiled, and in the second stage, a container can be created to run the compiled application. This configuration allows the Dockerfile to run more efficiently.

Below, I provide a Dockerfile for an example multi-stage build:

# Build stage
FROM golang:latest as builder

COPY . /app
WORKDIR /app

RUN go build -o main .

# Run stage
FROM alpine:latest

COPY --from=builder /app/main /app/main

CMD ["/app/main"]

In this Dockerfile, we first create a compiler using the golang image and build our application. The lines that do this start with FROM golang:latest as builder and end with RUN go build -o main . Next, we create a container for execution using the alpine image. These lines start with FROM alpine:latest and end with CMD ["/app/main"]. For this container to work, we move the “main” file created during the build process to the alpine container with the COPY command.

In this example, two different images are used for compilation and execution, and different commands are executed in both images. Therefore we have a multi-stage build and we reduce the size of the container.

References:
https://docs.docker.com/reference/dockerfile/

Leave a Reply

Your email address will not be published. Required fields are marked *