To create a docker image, the following steps need to be followed -
i) Create a Dockerfile.
ii) Add instructions to this Dockerfile -
A sample Dockerfile is added here for your reference -
Code Block |
---|
FROM node
WORKDIR /app
COPY package.json
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node","app.js"] |
iii) Run the following command to build an image based on the Dockerfile -
Code Block |
---|
docker build . |
iv) To run the built image -
Code Block |
---|
docker run <image_id> |
v) To list all running instances of the image i.e. containers -
...
If we rebuild images without any changes, it is observed that docker builds images superfast this time around. This is because Docker determines that you do not really need to run certain commands again and hence will just use the cache in such cases.
Whenever an image is being built, docker caches every single instruction and the result of that instruction. Upon rebuilding this image, docker uses cached instruction without having to re-execute the command. So, every instruction in the Dockerfile represents a LAYER in the image.