Dockerfile Reference – From Instruction

Hello Everyone !
Hope you are doing well and utilising this important time of your life in learning new things. In one of the previous article we learned how to create a Docker file, now let’s deep dive into each available instructions which are possible in Dockerfile. To make it easy to find, I’ve created a index page, please refer the same for other articles.

Dockerfile Format

Before we understand about FROM instruction, let’s closely observe our Dockerfile and understand how each instruction is written.

FROM node:12
# This is sample Dockerfile
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD ["node", "app.js"]

Format to write Dockerfile is,

# Comment
INSTRUCTION argument

Instructions are not case sensitive, it’s one of the best practice to make Dockerfile readable and separate arguments from instructions.

FROM Instuction

One of the thing which we learned while writing first Dockerfile was, Docker executes all instructions in given sequence. Hence From instruction must be the first instruction of your dockerfile. Comment, Parser directives and global ARGs can be declared before From instruction. From instruction can be written in below formats,

FROM [--platform=<platform>] <image> [AS <name>]
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
  • FROM instruction sets base image for all the following instructions. It should be a valid image which can be pulled from repository. We can also start from scratch.
  • FROM can appear multiple times in a Dockerfile, each FROM instruction starts a clear state. It can be used if you want to create multiple images or one build stage as a dependent for another image.
  • Optionally we can use AS instruction to name image and use it as a reference in subsequent image.
  • –platform flag is also optional, it can be used to specify platform of the image like linux/amd64, linux/arm64 or windows/amd64, by default target platform of build request is used.
  • tag and digest are also optional, if they are not provided, builder assumes latest tag.

Let’s see some quick examples,

ARG VERSION=latest
# Using ARG instruction
FROM baseimage:${VERSION} as base
.
.

# Multiple FROM instructions
FROM image:${VERSION}
COPY --from=base . .
.

To conclude this article, we learned about basics of how to write instructions in Dockerfile and understood FROM instructions and commenting in Dockerfile. For any further articles on the same, please refer index page. Keep learning and expanding your horizon.

Reference: https://docs.docker.com

Sanket Modi

Working in Information Technology since 2012. Started my career as Java Developer and now working in multiple different technologies like nodejs, Python, Docker, Kubernetes, Azure etc. I like to explore new technologies and read books.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: