- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Important instructions used in dockerfile
We all know the importance of dockerfile in creating an efficient and flexible Docker Image. A dockerfile contains a set of instructions that are executed step by step when you use the docker build command to build the docker image. It contains certain instructions and commands that decides the structure of your image, the amount of time taken to build the image, contains instructions related to docker build context, contains information related to the packages and libraries to be installed in the container and many more. Hence, it becomes very important to create an efficient, reusable, clean dockerfile as it contains the blueprint of the image that you will build.
In this article, I have created a curated list of all the important commands and instructions that are extensively used in a dockerfile. In a separate article, I have also enlisted best practices to create a dockerfile.
Without any further ado, let’s discuss some of the most important dockerfile commands that you should thoroughly be aware of in order to efficiently create a dockerfile.
You must have noticed that almost all the dockerfile starts with the FROM command. The FROM command is of the form −
FROM <image name>:<tag name>
A FROM command allows you to create a base image such as an operating system, a programming language, etc. All the instructions executed after this command take place on this base image. It contains an image name and an optional tag name. If you already have the base image pulled previously in your local machine, it doesn’t pull a new one. There are several pre published docker base images available in the docker registry. You can also push your own customized base image inside the docker registry.
Examples of FROM instruction along with different base images are −
FROM ubuntu FROM centos FROM python:3
A RUN instruction is used to run specified commands. You can use several RUN instructions to run different commands. But it is an efficient approach to combine all the RUN instructions into a single one.
Each RUN command creates a new cache layer or an intermediate image layer and hence chaining all of them into a single line, becomes efficient. However, chaining multiple RUN instructions could lead to cache bursts as well.
Some example of RUN commands are −
RUN apt−get −y install vim RUN apt−get −y update
You can chain multiple RUN instructions in the following way −
RUN apt−get −y update \ && apt−get −y install firefox \ && apt−get −y install vim
If you want to run a docker container by specifying a default command that gets executed for all the containers of that image by default, you can use a CMD command. In case you specify a command during the docker run command, it overrides the default one. Specifying more than one CMD instructions, will allow only the last one to get executed.
Example of a CMD command −
CMD echo "Welcome to TutorialsPoint"
If you specify the above line in the dockerfile and run the container using the following command without specifying any arguments, the output will be “Welcome to TutorialsPoint”
sudo docker run −it <image_name>
Output − “Welcome to TutorialsPoint”
In case you try to specify any other arguments such as /bin/bash, etc, the default CMD command will be overridden.
The difference between ENTRYPOINT and CMD is that, if you try to specify default arguments in the docker run command, it will not ignore the ENTRYPOINT arguments. The exec form of an ENTRYPOINT command is −
ENTRYPOINT [“<executable-command>”, “<parameter 1>”, “<parameter 2>”, ….]
If you have used the exec form of the ENTRYPOINT instruction, you can also set additional parameters with the help of CMD command. For example −
ENTRYPOINT ["/bin/echo", "Welcome to TutorialsPoint"] CMD ["Hello World!"]
Running docker run command without any argument would output −
Welcome to TutorialsPoint Hello World!
If you specify any other CLI arguments, “Hello World!” will get overridden.
You can specify your working directory inside the container using the WORKDIR instruction. Any other instruction after that in the dockerfile, will be executed on that particular working directory only.
Sets the working directory to /usr/src/app inside the container.
This instruction allows you to copy a directory from your local machine to the docker container.
FROM ubuntu WORKDIR /usr/src/app COPY ∽/Desktop/myapp .
This would copy all the files inside the directory ∽/Desktop/myapp in your local machine to your current working directory inside the docker container.
Similar to COPY instruction, you can use ADD to copy files and folders from your local machine to docker containers. However, ADD also allows you to copy files from a URL as well as a tar file.
ADD ∽/Desktop/myapp/practice.tar.gz /usr/src/app
Would copy all the contents inside the tar file to /usr/src/app inside the container.
ADD <URL such as a github url> <Destination path inside the container>
This command would copy all the files inside the github url to the destination.
The EXPOSE instruction inside the dockerfile informs that the container is listening to the specified port in the network. The default protocol is TCP.
Will map the 8080 port to the container.
You can use the −p flag with the docker run command to make the container listen to another container or the host machine.
You can use a LABEL instruction to add description or meta data for a docker image. Its a key−value pair.
LABEL description="This is a sample image"
To conclude, these were some of the basic as well as important instructions that are most commonly used in a dockerfile and using them in the right way would surely make you docker image to build efficiently, reduce its size, make it flexible and reusable.
- When is the !important rule used in CSS?
- An important liquid fuel, used in home is ………..
- Best practices for writing a Dockerfile
- Combine multiple images using one dockerfile
- What are some of the important Scientific Libraries used in Lua programming?
- Arithmetic instructions in 8086 microprocessor
- Logical instructions in 8086 microprocessor
- What are important HTTP headers to be frequently used in Python CGI Programming?
- What is the difference between CMD and ENTRYPOINT in a Dockerfile?
- Number of instructions in 8085 Microprocessor
- Conditional JUMP instructions in 8085 Microprocessor
- Conditional call instructions in 8085 Microprocessor
- Conditional return instructions in 8085 Microprocessor
- Restart instructions (RSTn) in 8085 Microprocessor
- EI and DI instructions in 8085