Building a container image is very simple. It is actually a one-line command: docker build -t <image-name>:version <build_context>. While we will discuss building container images in detail in the Building and managing container images section, let’s first build the Dockerfile:

$ docker build -t <your_dockerhub_user>/nginx-hello-world .

[+] Building 50.0s (7/7) FINISHED

=> [internal] load .dockerignore 0.0s

=> => transferring context: 2B 0.0s

=> [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 171B 0.0s

=> [internal] load metadata for docker.io/library/ubuntu:bionic 2.4s => [1/3] FROM docker.io/library/ubuntu:bionic@sha256:152dc042… 2.8s => => resolve docker.io/library/ubuntu:bionic@sha256:152dc04… 0.0s => => sha256:152dc042… 1.33kB / 1.33kB 0.0s => => sha256:dca176c9… 424B / 424B 0.0s

=> => sha256:f9a80a55… 2.30kB / 2.30kB 0.0s

=> => sha256:7c457f21… 25.69MB / 25.69MB 1.0s

=> => extracting sha256:7c457f21… 1.6s

=> [2/3] RUN apt update && apt install -y curl 22.4s

=> [3/3] RUN apt update && apt install -y nginx 21.6s

=> exporting to image 0.6s

=> => exporting layers 0.6s

=> => writing image sha256:9d34cdda… 0.0s

=> => naming to docker.io/<your_dockerhub_user>/nginx-hello-world

You might have noticed that the name of the container had a prefix in front of it. That is your Docker Hub account name. The name of the image has a structure of <registry-url>/<account-name>/<container-image-name>:<version>.

Here, we have the following:

  • registry-url: The URL to the Docker registry – defaults to docker.io
  • account-name: The user or account that owns the image
  • container-image-name: The container image’s name
  • version: The image version

Now, let’s create a container out of the image using the following command:

$ docker run -d -p 80:80 <your_dockerhub_user>/nginx-hello-world 092374c4501560e96a13444ce47cb978b961cf8701af311884bfe…

$ docker ps      
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
092374c45015<your_dockerhub“nginx -g28 secondsUp 270.0.0.0:80->80/loving_
 _user>/nginx-‘daemon of…”agosecondstcp, :::80->80/tcpnoether
 hello-world     

Here, we can see that the container is up and running.

If we run curl localhost, we get the default nginx html response:

$ curl localhost

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

</body>

</html>

That’s great! We have built our first image using a Dockerfile.

What if we wanted to customize the image according to our requirements? Practically speaking, no one would want an NGINX container just responding with the default Welcome to nginx! message, so let’s create an index page and use that instead:

$ vim index.html

Hello World! This is my first docker image!

This one outputs acustom message instead of the default NGINX HTML page.

We all know that the default NGINX directory containing the index.html file is /var/www/ html. If we can copy the index.html file into this directory, it should sort out our problem.

So, modify the Dockerfile so that it includes the following:

$ vim Dockerfile

FROM ubuntu:bionic

RUN apt update && apt install -y curl

RUN apt update && apt install -y nginx

WORKDIR /var/www/html/

ADD index.html ./

CMD [“nginx”, “-g”, “daemon off;”]

Here, we’ve added two directives to the file: WORKDIR and ADD. Let’s understand what each one does:

  • WORKDIR: This defines the current working directory, which is/var/www/html in this case. The last WORKDIR in the Dockerfile also specifies the working directory when the container is executed. So, if you exec into a running container, you will land in the last defined WORKDIR.

WORKDIR can be absolute as well as relative to the current working directory.

  • ADD: This adds a local file to the container filesystem – the working directory, in this case. You can also use a COPY directive here instead of ADD, though ADD offers some more features, such as downloading files from a URL and using an archive such as a TAR or ZIP package.

Leave a Reply

Your email address will not be published. Required fields are marked *