Dockerfile - What does it include

0 votes
What are the basic instructions that constitute a Dockerfile? What different instructions does it have and how are they different from each other?
Aug 26, 2019 in Docker by Shawn
932 views

1 answer to this question.

0 votes

Basic instructions in a dockerfile

FROM instruction :

Usage :

FROM <image> [AS <name>]

Or

FROM <image>[:<tag>] [AS <name>]

Or

FROM <image>[@<digest>] [AS <name>]

FROM helps to set the base image for the following instructions in the Dockerfile. The base image could be one pulled from the remote public repository or a local image built by the user. 

FROM can appear multiple times in Dockerfile and each time it appears it clears any stage created from previous instructions. 

Hence usually an AS <NAME> is added to the FROM instruction to identify the last image created just before the FROM instruction.  

RUN instruction:

Usage:

  • RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
  • RUN ["executable", "param1", "param2"] (exec form)

RUN instruction creates a new layer on the current image which is used for the next step in Dockerfile. RUN cache is valid for the next build provided “docker build” command is not run with “--no-cache” flag.

RUN in the executable form doesn’t do shell processing like variable substitution.

CMD instruction:

Usage:

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

CMD provides defaults for executing container. There could only be one CMD instruction but if there are many, only the last one has the effect. 

If CMD is providing default arguments for the ENTRYPOINT instruction, then the CMD and ENTRYPOINT instructions should be specified with the JSON array format.  

ADD instruction:

Usage:

  • ADD [--chown=<user>:<group>] <src>... <dest>
  • ADD [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)

The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.

COPY instruction:

Usage:

  • COPY [--chown=<user>:<group>] <src>... <dest>
  • COPY [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)

The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>. Main difference between COPY and ADD is that ADD supports the source to be a URL or tar file.

ENTRYPOINT:

Usage:

  • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
  • ENTRYPOINT command param1 param2 (shell form)

An ENTRYPOINT allows you to configure a container that will run as an executable. 

As a best practice ENTRYPOINT should be defined when using the container as an executable and CMD should be used as a way of defining default arguments for an ENTRYPOINT command.CMD could be overridden while running a container with arguments.

WORKDIR:

Usage:

WORKDIR /path/to/workdir

The WORKDIR instruction sets the working directory for RUN, CMD, ENTRYPOINT, COPY and ADD instructions in the Dockerfile.

answered Aug 26, 2019 by Sirajul
• 59,230 points

Related Questions In Docker

0 votes
1 answer

Brew install docker does not include docker engine?

Please try running brew install docker This will install ...READ MORE

answered Jul 30, 2018 in Docker by Kalgi
• 52,350 points
834 views
0 votes
1 answer

Dockerfile to install apache server and start httpd service on it.

Hey @Kali, That's pretty simple. You could have ...READ MORE

answered Jan 22, 2019 in Docker by Yesha
23,387 views
0 votes
1 answer
0 votes
1 answer

What all platforms does docker run on?

Hey @Albert, docker runs on the following ...READ MORE

answered Feb 13, 2019 in Docker by Imran
851 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,738 views
+1 vote
1 answer

What is a docker file? How do I create a docker image with dockerfile?

 A Dockerfile is a script/text configuration file that contains ...READ MORE

answered Jun 7, 2019 in Docker by Sirajul
• 59,230 points
4,550 views
0 votes
1 answer

what does docker import command do?

docker import command Imports the contents from ...READ MORE

answered Jul 2, 2019 in Docker by Sirajul
• 59,230 points
6,640 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP