Docker Hands-On Part 05
Tags: tutorial, aws, docker
Published: 2024-02-14 12:51:28

## 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
```

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*
```

Step 02: Build the image

```
$ docker build -t widgetfactory:0.1 .
```

Now you can see

```
$ docker images

REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
widgetfactory           0.1       b1f9112e68d5   22 seconds ago   180MB
```

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

```
$ export showLayers='{{ range .RootFS.Layers }}{{ println . }}{{end}}'
$ export showSize='{{ .Size }}'
```

Step 04: Show the widgetfactory image's size

```
$ docker inspect -f "$showSize" widgetfactory:0.1
```

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 . 
```

```
$ docker build -t widgetfactory:0.2 .
```

Step 06: Get the widgetfactory:0.2 container

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

$ ls -l
```

Step 07: Run the container. 

```
$ docker run -d --name web1 -p 80:80 widgetfactory:0.3
```

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]

```