Docker - Dockerfile
What is Dockerfile?
A Dockerfile is a text document that contains commands that are used to assemble an
image
. We can use any command that call on thecommand line
. Docker buildsimages
automatically by reading theinstructions
from theDockerfile
.The docker build command is used to build an
image
from theDockerfile
. You can use the-f flag
withdocker build
to point to aDockerfile
anywhere in your file system.
$ docker build -f /path/to/a/Dockerfile .
Dockerfile Instructions
The
instructions
are not case-sensitive but you must follow conventions which recommend to use uppercase.Docker runs
instructions
ofDockerfile
in top to bottom order. The firstinstruction
must beFROM
in order to specify theBase Image
.A statement begin with
#
treated as acomment
. You can useRUN
,CMD
,FROM
,EXPOSE
,ENV
etc instructions in yourDockerfile
.
FROM
- This instruction is used to set the
Base Image
for the subsequent instructions. A validDockerfile
must have FROM as its firstinstruction
.
FROM node:latest
LABEL
- We can add labels to an
image
to organize images of our project. We need to useLABEL
instruction to set label for theimage
.
LABEL maintainer = "CUBETIQ"
WORKDIR
- The
WORKDIR
is used to set the working directory for anyRUN
,CMD
andCOPY
instruction that follows it in theDockerfile
. If work directory does not exist, it will be created by default. We can useWORKDIR
multiple times in aDockerfile
.
WORKDIR /app
COPY
- This instruction is used to copy new files or directories from
source
to the filesystem of the container at thedestination
.
COPY package*.json ./
RUN
- This instruction is used to execute any command of the
current image
.
RUN npm install
CMD
- This is used to execute
application
by theimage
. There can be only oneCMD
in aDockerfile
. If we use more than oneCMD
, only last one will execute.
CMD ["node", "app.js"]