WHAT IS DOCKERFILE: A Dockerfile is a script or configuration file used in Docker, a platform for developing, shipping, and running a...
WHAT IS DOCKERFILE:
A Dockerfile is a script or configuration file used in Docker, a platform for developing, shipping, and running applications in containers. Containers allow developers to package an application and its dependencies together, providing consistency and reproducibility across different environments.
The Dockerfile contains a set of instructions that Docker follows to automatically build a Docker image. An image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
The syntax for writing a Dockerfile and Format
1. FROM
A FROM statement defines which image to download and start from. It must be the first command in your Dockerfile. A Dockerfile can have multiple FROM statements which means the Dockerfile produces more than one image.
Example:
Syntax: FROM <baseimagename>[:<releasetag>]
FROM java:17
FROM nginx:latest
2. MAINTAINER
This statement is a kind of documentation, that defines the author who is creating this Dockerfile or who should you contact if it has bugs.
SYNTAX: MAINTAINER <authour’s details>
MAINTAINER Firstname Lastname <example@gmail.com>
3. RUN
The RUN statement defines running a command through the shell, waiting for it to finish, and saving the result. It tells what process will be running inside the container at the run time.
5. ENV
ENV statement sets the environment variables both during the build and when running the result. It can be used in the Dockerfile and any scripts it calls. It can be used in the Dockerfile as well as any scripts that the Dockerfile calls. These are also persistent with the container and can be referred to at any moment.
6. ENTRYPOINT
7. CMD
CMD specifies the whole command to run. We can say CMD is the default argument passed into the ENTRYPOINT. The main purpose of the CMD command is to launch the software required in a container.
Example:
CMD [ "npm" "run" "start"]
Note: If you have both ENVIRONMENT and CMD, they are combined together.
8. EXPOSE
9. VOLUME
The VOLUME statement defines shared volumes or ephemeral volumes depending upon whether you have one or two arguments.
VOLUME ["/host/path" "/container/path/"]
10. WORKDIR
WORKDIR sets the directory that the container starts in. Its main purpose is to set the working directory for all future Dockerfile commands.
11. USER
It sets which user’s container will run as. This can be useful if you have shared network directories involved that assume a fixed username or a fixed user number.
12. ARG
A variable that can be provided at build time is defined by an ARG Instruction. Once it has been specified in the Dockerfile, you can specify it using the –build-arg switch when creating the image. The Dockerfile supports multiple ARG instructions. The only instruction in the Dockerfile that can come before the FROM instruction is ARG.
After the image is created, ARG values are not accessible. An ARG variable value won’t be accessible to a running container.
Here's a basic structure of a Dockerfile:
Once you have a Dockerfile, you can use the docker build command to build a Docker image based on that file. The resulting image can then be run in a container using the docker run command.