DEV Community

Randika Madhushan Perera
Randika Madhushan Perera

Posted on

Docker Hands-On Part 05

05. Building Container Images Using Dockerfiles

Introduction

The evolution of container technology seeks to address and simplify the deployment process, making it more efficient and less prone to errors. Manual image creation, while possible, contradicts this objective by introducing unrepeatable processes fraught with potential mistakes. Dockerfiles represent Docker's solution to this challenge, enabling the automation of image creation through a process that is both reproducible and version-controllable.

The Dockerfile Advantage

A Dockerfile is essentially a script composed of various commands, each instructing Docker how to build a specific image. This method allows developers to define their images as code, significantly reducing the manual labor involved in image creation and ensuring that the process can be easily repeated and integrated into continuous integration/continuous deployment (CI/CD) pipelines.

Building an Image for a Web Server

In this tutorial, we'll demonstrate how to utilize a Dockerfile to build a container image designed to serve a company's website. The process will be divided into three key versions, each improving upon the last:

Version 1: Updating the Base Image

Start by creating a Dockerfile that updates the base image using the package manager. While updating the base image is generally not required for every Dockerfile, it serves as an illustrative first step in this tutorial.

Version 2: Removing the Default Apache Welcome Page

The second version of the Dockerfile will modify the image to remove the default Apache welcome page. This step is crucial for preparing the image to host custom web content.

Version 3: Adding Website Data to the Image

The final version of the Dockerfile will incorporate the actual website data into the image. This ensures that the website is served directly from the container upon startup, providing a seamless deployment experience.

Deploying the Final Image

Once the Dockerfile for version 3 is complete, the next step involves building the image and running a container from it. This process will demonstrate the web server in action, serving the company's website from the containerized environment.

Getting Started

To begin, create a Dockerfile in a directory dedicated to this project. The Dockerfile will evolve through each version, culminating in a container image that is ready for deployment. This method not only streamlines the development and deployment process but also aligns with best practices for managing application infrastructure as code.

Step 01: Create a Docker file

$ cd content-widget-factory-inc

$ vim Dokcerfile
Enter fullscreen mode Exit fullscreen mode

add the below codes to it.

#base image httpd
FROM httpd:latest   
# Run the app update
RUN apt update -y && apt upgrade -y && apt autoremove -y && apt clean && rm -rf /var/lib/apt/lists*
Enter fullscreen mode Exit fullscreen mode

Step 02: Build the image

$ docker build -t widgetfactory:0.1 .
Enter fullscreen mode Exit fullscreen mode

Now you can see

$ docker images

REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
widgetfactory           0.1       b1f9112e68d5   22 seconds ago   180MB
Enter fullscreen mode Exit fullscreen mode

Step 03: Set variables to examine the image's size and layers

$ export showLayers='{{ range .RootFS.Layers }}{{ println . }}{{end}}'
$ export showSize='{{ .Size }}'
Enter fullscreen mode Exit fullscreen mode

Step 04: Show the widgetfactory image's size

$ docker inspect -f "$showSize" widgetfactory:0.1
Enter fullscreen mode Exit fullscreen mode

Step 05: We are going to change Dockerfile

Add the below two lines

WORKDIR /usr/local/apache2/htdocs
COPY ./web .

#base image httpd
FROM httpd:latest   
# Run the app update
RUN apt update -y && apt upgrade -y && apt autoremove -y && apt clean && rm -rf /var/lib/apt/lists*
WORKDIR /usr/local/apache2/htdocs
COPY ./web . 
Enter fullscreen mode Exit fullscreen mode
$ docker build -t widgetfactory:0.2 .
Enter fullscreen mode Exit fullscreen mode

Step 06: Get the widgetfactory:0.2 container

$ docker run --rm -it widgetfactory:0.3 bash

$ ls -l
Enter fullscreen mode Exit fullscreen mode

Step 07: Run the container.

$ docker run -d --name web1 -p 80:80 widgetfactory:0.3
Enter fullscreen mode Exit fullscreen mode

Step 08: Let's test

$ wget localhost

--2024-02-14 10:31:37--  http://localhost/
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3276 (3.2K) [text/html]
Saving to: ‘index.html’

index.html                                          100%[=================================================================================================================>]   3.20K  --.-KB/s    in 0s

2024-02-14 10:31:37 (376 MB/s) - ‘index.html’ saved [3276/3276]

Enter fullscreen mode Exit fullscreen mode

Top comments (0)