How to build a Docker image from a Dockerfile

How to Build a Docker Image from a Dockerfile

How to build a Docker image from a Dockerfile

Home » News » How to build a Docker image from a Dockerfile
Table of Contents
a Developer Writing Software.
picture seventyfouradobe inventory

When growing with Docker, you’ll discover quite a few pre-made pictures on Docker Hub, although you won’t discover the precise picture you need. Alternatively, maybe you need to create customized pictures for a selected objective. If you happen to don’t need to hand over the deployment of your containerized apps and companies to a picture constructed by a 3rd social gathering, I’ll present you the way simple it’s to construct your personal Docker picture.

SEE: Hacking and securing Docker containers (roosho Academy)

Bounce to:

What it is advisable construct a Docker picture

You’ll want Docker put in in your working system of selection. I’ll reveal this tutorial with Ubuntu Server 22.04; for those who use an OS aside from Ubuntu Linux, you’ll want to change the Docker set up steps. You additionally want a consumer with sudo privileges.

Methods to write a Dockerfile

We’re going to create a Dockerfile to construct a picture primarily based on the most recent model of Ubuntu, which incorporates NGINX.

Create a brand new listing to accommodate the Dockerfile with:

mkdir docker_images

Become that new listing with the next:

cd docker_images

Create the brand new Dockerfile with the command:

nano Dockerfile

In that new file, paste the next contents:

#
# Base the picture on the most recent model of Ubuntu
FROM ubuntu:newest
#
# Determine your self because the picture maintainer (the place EMAIL is your electronic mail deal with)
LABEL maintainer="EMAIL"
#
# Replace apt and replace Ubuntu
RUN apt-get replace && apt-get improve -y
#
# Set up NGINXl
RUN apt-get set up nginx -y
#
# Expose port 80 (or no matter port you want)
EXPOSE 80
#
# Begin NGINX inside the Container
CMD ["nginx", "-g", "daemon off;"]

Right here’s an outline of the completely different directives:

  • FROM: Defines the bottom picture that shall be used.
  • MAINTAINER: The writer of the picture.
  • RUN: Directions for executing instructions whereas constructing the picture.
  • CMD: Offers defaults for executing a command inside the picture; there might be just one CMD directive in your Dockerfile.

Along with your Dockerfile created, save and shut it with the CTRL+X keyboard shortcut.

Methods to construct a Docker picture

Be sure you give your Docker picture a selected identify so that you at all times know which picture to make use of. We’ll name our picture tr_test_image and construct it with the command:

docker construct -t tr_test_image .

The rationale we’ve the . on the finish of the command is to tell the docker command that we’re constructing inside the present working listing. Docker will pull the most recent Ubuntu picture and construct tr_test_image with NGINX pre-installed. You possibly can confirm the picture was created with the command:

docker pictures

You must see one thing like this within the output:

tr_test_image newest 663ea67dc848   quarter-hour in the past   174MB

You possibly can then deploy a container primarily based in your picture with a command like this:

docker run -d -p 8001:80 tr_test_image

If you happen to level an internet browser to http://SERVER:8001, the place SERVER is the IP deal with of the internet hosting server, it’s best to see the NGINX welcome web page.

Simple picture constructing

That’s all there may be to creating your customized pictures with Dockerfiles. Though this picture is easy, it makes it clear how simple it’s to construct pictures after which deploy containers primarily based on these pictures. As you get extra superior along with your picture constructing, solely create pictures with the smallest footprint attainable, use multi-stage construct and add solely what is important.

Subscribe to roosho’s How To Make Tech Work on YouTube for all the most recent tech recommendation for enterprise execs from Jack Wallen. If you’re involved in studying extra about Docker, you’ll be able to try the next sources in roosho Academy:

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
share this article.

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name