Docker Containerization

Containerization is the process of encapsulating an application, its dependencies, and configurations into a self-contained unit called a container, which runs consistently and predictably across various computing environments, thereby enhancing portability, manageability, and scalability.

Dockerfile

A Dockerfile is a text file that contains all the commands you could call on the command line to assemble an image. These instructions include identifying the base image, installing any necessary applications, copying files from the host to the container, exposing network ports, setting environment variables, and specifying the execution command for the container.

This is an example Dockerfile.

# Use an official Nginx image as a parent image
FROM nginx:latest

# A LABEL is a key-value pair. Here, it defines the maintainer of the image, which is an email address in this case.
LABEL maintainer="grinntec@gmail.com"

# Set the working directory in the container to /usr/share/nginx/html
WORKDIR /usr/share/nginx/html

# Copy the specified directory contents into the container at /usr/share/nginx/html
COPY ./src .

# Make port 80 available to the world outside this container
EXPOSE 80

# Run Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]

FROM nginx:latest: indicates the base image. In this case, it’s using the latest version of the official Nginx image from Docker Hub.

LABEL maintainer="grinntec@gmail.com": The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. Here, it defines the maintainer of the image, which is an email address in this case.

WORKDIR /usr/share/nginx/html: sets the working directory for any instructions that follow in the Dockerfile. It’s the directory where Nginx expects to find the files to serve on the web.

COPY ./src .: copies new files or directories from <src> and adds them to the filesystem of the image at the path <dest>. Here, it’s copying everything from the src directory (relative to the Dockerfile on your host machine) to the current directory in the Docker image (which is /usr/share/nginx/html due to the WORKDIR command).

EXPOSE 80: informs Docker that the container listens on the specified network port at runtime. Here, it’s port 80 and we’ll use 8080 as the listener on the Docker host network interface (port mapping 8080 to 80).

CMD ["nginx", "-g", "daemon off;"]: The CMD instruction provides defaults for an executing container. Here, it’s starting the Nginx server. The -g option sets global directives, and "daemon off;" means running Nginx in the foreground, which is necessary for Docker.

The Dockerfile must beging with a capital D and not have an extension.

Create a simple image

This process will walk through creating a single page web site running on Nginx as a Docker container.

Working directory

Setup a working directory so you can create files and generate Docker data. I use GitHub for these projects as I can save them for later.

My working directory is called simple-nginx.

Create the html

This is a simple test so there will be a single index.html file served by the web server. To keep things organized this file will be in ./src.

<!DOCTYPE html>
<html>
<head>
<title>nginx.web.server</title>
<meta charset="UTF-8">
</head>
<body>
<h1>This website is a Docker container</h1>
</body>
</html>

Define how to create the Docker image

In the root of the working directory create a new file named Dockerfile. It’s impprant to capitalize the D and do not add a extension. This file is the set of instruction Docker will use to create the Docker image. Paste in the example at the top of this page.

Build the Docker image The following command will create a Docker image based on what is defined in the Dockerfile file. In effect it will create a temporary container, execute the commands, and save the failes back to a template file we call an image. The -t paramter allow us to tag the image which in this example is tagged as latest. The trailing period is important as it tells Docker to use the current directory.

docker build -t simple-nginx:latest .

Run the image as a container This command will now run the image as a container. The -d tells Docker to run the container as a daemon so you get your shell back otherwise the shell will be locked whilst the container is running. The -p defines the port mapping so we can access a port on the Docker host and access the container port which in this example is 8081 mapping to 80, so we go to http://{dockerhost}:8080. Lastly we run the Docker image and tag combination we created earlier.

docker run -d -p 8080:80 simple-nginx:latest
Last modified July 21, 2024: update (e2ae86c)