Geekdosage
  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
  • About Us

Geekdosage

  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
  • About Us
CentOSDockerLinuxUbuntu

How to Build a Docker Image from Dockerfile

by GeekDosage December 14, 2020
written by GeekDosage December 14, 2020
create a docker image from dockerfile

Dockerfile is a file that uses as the script with a defined set of instructions to build a docker image. Docker official docs describe Dockerfile as a “text document that contains all the commands a user could call on the command line to assemble an image”.

You should have a properly installed and configured docker installation for this tutorial. Refer to this article to install docker in Ubuntu.

.dockerignore file

Yes, you are right. As the name suggests this file used to exclude files or directories from adding to the creating image. There are some hidden files such as .git, .vscode, .vimrc which are used in developing an application, But should not be included in the final deployment environment. Therefore you can create a .dockeringore file and include excluding from the docker image build.

Dockerfile Instructions

Let’s understand Dockerfile instructions before writing a Dockerfile.

FROM

This instruction used to set the base image for the new docker image. As defined in the official documentation, a valid Dockerfile must start with a FROM instruction when building a docker image from Dockerfile. It is recommended always to try to choose a lightweight base image such as alpine since they have included only necessary binaries.

FROM nginx

You can find various kind of docker images according to your requirement in docker hub. It is a service provided by Docker to share docker images.

Docker hub

Docker hub

MAINTAINER

You can use this instruction to provide the owner/author of the docker image. However, this is deprecated instruction now. Use LABEL instruction to provide metadata instead of this.

LABEL

This instruction adds metadata about your image as key-value pairs. You can use docker inspect command to view these metadata when running as a docker container.

LABEL "Release"="version 1.0"

RUN

The base image only has a file system with a limited set of binaries and libraries. Therefore You have to modify the underlying file system as your application requires. RUN instruction is used for this purpose, such as installing packages, execute shell commands, etc. You can use this instruction in 2 different methods.

  1. RUN mkdir /var/www/html/site
  2. RUN ["/usr/bin/mkidir","/va/www/html/site"]

Every RUN Instruction creates a new layer on top of the existing image. Therefore as a best practice, always try to combine commands into one RUN instruction to reduce the Docker image size.

CMD

You can set the default running command when the container starting from the image. There should be only one CMD instruction in a Dockerfile. But remember this can be overwritten at the docker run command.

CMD ["nginx", "-g", "daemon off;"]

ENTRYPOINT

This instruction defines the initial running command when the container gets started. Same as CMD instruction, right? Yes but there are several use cases where you can use both instructions together. CMD can append values to the ENTRYPOINT instruction.

ENTRYPOINT ["echo", "HELLO WORLD"]

WORKDIR

This Instruction sets the working directory when creating an image. And also, it will be the default working directory when you access the running docker containers shell.

WORKDIR /app

COPY

You can use this instruction to copy files and directories from a source to the creating docker image file system.

COPY /opt/app1 /app/source

EXPOSE

Instruct service listening ports when the image runs as a container. It can be a single or several ports. You can define it as a TCP or UDP.

EXPOSE 80 

or 

EXPOSE 80 8080

Now you know the most commonly used instructions in Dockerfile. Let’s create a simple web server using Nginx base image.

$ cat Dockerfile

FROM nginx

LABEL maintainer="GeekDosage"

COPY index.html /usr/share/nginx/html

EXPOSE 80

Create an index.html file in the same path where Dockerfile is located.

$ cat index.html

Hello GEEKS!

Build docker image from Dockerfile

You can create the docker image from the created Dockerfile by running the following command. you can use -t to tag your Docker image accordingly.

$ docker build -t first-docker-image .

Use -f if you wish to specify a different path to the Dockerfile

$ docker build  -f /path-to-Dockerfile/ .
OUTPUT


Sending build context to Docker daemon 3.072kB

Step 1/4 : FROM nginx

---> 0901fa9da894

Step 2/4 : LABEL maintainer="GeekDosage"

---> Using cache

---> 342dd6c7ac45

Step 3/4 : COPY index.html /usr/share/nginx/html

---> a8fac61cb37b

Step 4/4 : EXPOSE 80

---> Running in dd68a449e8e0

Removing intermediate container dd68a449e8e0

---> fe5d4dcb32b1

Successfully built fe5d4dcb32b1

Successfully tagged first-docker-image:latest

We can list all the available docker images in our computer by runniing follwing command.

$ docker images

Docker images

You can refer to this tutorial to learn more about docker basic commands.

Let’s run a docker container from the created docker image. Use -d to run the container in the background and -p to bind host 80 port with container 80 port.

$ docker run -d --name first-nginx -p 80:80 first-docker-image:latest

Check the Nginx server using a browser. http://127.0.0.1. You will see it is working fine.

Output

Conclusion

In this tutorial, we have explained how to build a docker image using a Dockerfile. It is better to understand the usability of each Dockerfile instructions. Refer docker official documentation here.

GeekDosage
build a docker image from dockerfilebuild docker imagedocker imagehow to build a docker image
0 comment
0
FacebookTwitterPinterest
previous post
How to Install Docker in Ubuntu 20.04
next post
How to Create a Private Docker Registry in Ubuntu 20.04

Related Posts

2 Simplest Methods to Take/Capture Desktop Screenshot in...

January 17, 2021

How to remove/delete user in Linux

January 11, 2021

How to rename a Directory in Linux?

January 5, 2021

How to Install VirtualBox in Ubuntu 20.04

January 3, 2021

How to Create a Private Docker Registry in...

December 23, 2020

How to Install Docker in Ubuntu 20.04

December 12, 2020

How to Install Docker on CentOS 7

December 10, 2020

How to change the forgotten root password in...

November 25, 2020

What is a Linux OS? Why Linux is...

August 12, 2020

What is an operating system ? ~ Why...

August 12, 2020

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

In This Article

  • 1 .dockerignore file
  • 2 Dockerfile Instructions
    • 2.1 FROM
    • 2.2 MAINTAINER
    • 2.3 LABEL
    • 2.4 RUN
    • 2.5 CMD
    • 2.6 ENTRYPOINT
    • 2.7 WORKDIR
    • 2.8 COPY
    • 2.9 EXPOSE
  • 3 Build docker image from Dockerfile
  • 4 Conclusion

Keep in touch

Facebook Twitter Google + Instagram Pinterest Email

Popular Posts

  • How to change the forgotten root password in CentOS 8

  • How to Install Docker on CentOS 7

  • How to Build a Docker Image from Dockerfile

  • Best GPU stress test software to test your GPU

About Geekdosage

Geekdosage is a technology based website which mainly focuses on network engineering, computer science, automation, computer hardware and software and internet technologies. Our purpose is to create a platform where anyone can access the subject areas without barriers.

Recent Posts

  • 2 Simplest Methods to Take/Capture Desktop Screenshot in Ubuntu

    January 17, 2021
  • How to remove/delete user in Linux

    January 11, 2021

Find us

Facebook Twitter Instagram Pinterest Email
  • About Us
  • Contact Us
  • Disclaimer
  • Terms and Conditions
  • Cookie Privacy policy
  • Privacy Policy
  • DCMA

@2021 - All Right Reserved | Designed and Developed by GeekDosage