Quick News Bit

How to build a Docker image from a Dockerfile

0

If you’re starting your Docker developer journey, learn how to build an image using nothing more than a Dockerfile.

A developer writing software.
Image: Seventyfour/Adobe Stock

When developing with Docker, you’ll find numerous pre-made images on Docker Hub, though you might not find the exact image you want. Alternatively, perhaps you want to create custom images for a specific purpose. If you don’t want to hand over the deployment of your containerized apps and services to an image built by a third party, I’ll show you how easy it is to build your own Docker image.

SEE: Hacking and securing Docker containers (TechRepublic Academy)

Jump to:

What you need to build a Docker image

You’ll need Docker installed on your operating system of choice. I’ll demonstrate this tutorial with Ubuntu Server 22.04; if you use an OS other than Ubuntu Linux, you’ll need to modify the Docker installation steps. You also need a user with sudo privileges.

How to write a Dockerfile

We’re going to create a Dockerfile to build an image based on the latest version of Ubuntu, which includes NGINX.

Create a new directory to house the Dockerfile with:

mkdir docker_images

Change into that new directory with the following:

cd docker_images

Create the new Dockerfile with the command:

nano Dockerfile

In that new file, paste the following contents:

#

# Base the image on the latest version of UbuntuFROM ubuntu:latest

#

# Identify yourself as the image maintainer (where EMAIL is your email address)LABEL maintainer="EMAIL"

#

# Update apt and update UbuntuRUN apt-get update && apt-get upgrade -y

#

# Install NGINXlRUN apt-get install nginx -y

#

# Expose port 80 (or whatever port you need)EXPOSE 80

#

# Start NGINX within the ContainerCMD ["nginx", "-g", "daemon off;"]

Here’s a description of the different directives:

  • FROM: Defines the base image that will be used.
  • MAINTAINER: The author of the image.
  • RUN: Instructions for executing commands while building the image.
  • CMD: Provides defaults for executing a command within the image; there can be only one CMD directive in your Dockerfile.

With your Dockerfile created, save and close it with the CTRL+X keyboard shortcut.

How to build a Docker image

Be sure to give your Docker image a specific name so you always know which image to use. We’ll call our image tr_test_image and build it with the command:

docker build -t tr_test_image .

The reason we have the . at the end of the command is to inform the docker command that we are building within the current working directory. Docker will pull the latest Ubuntu image and build tr_test_image with NGINX pre-installed. You can verify the image was created with the command:

docker images

You should see something like this in the output:

tr_test_image latest 663ea67dc848   15 minutes ago   174MB

You can then deploy a container based on your image with a command like this:

docker run -d -p 8001:80 tr_test_image

If you point a web browser to http://SERVER:8001, where SERVER is the IP address of the hosting server, you should see the NGINX welcome page.

Easy image building

That’s all there is to creating your custom images with Dockerfiles. Although this image is simple, it makes it clear how easy it is to build images and then deploy containers based on those images. As you get more advanced with your image building, only create images with the smallest footprint possible, use multi-stage build and add only what is necessary.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

For all the latest Technology News Click Here 

 For the latest news and updates, follow us on Google News

Read original article here

Denial of responsibility! NewsBit.us is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment