Container Image

How to build a container image

In this section, we build a container image and work with a Dockerfile.

A Dockerfile is a text file with no file extension that contains specifications for a software execution environment, the structure for a Docker Image. From those commands, Docker will build a Docker image (usually ranging from a few MB to a few GB in size).

Overview of Dockerfile

The general syntax of a Dockerfile

INSTRUCTION arguments
  • INSTRUCTION is the name of the directives in the Dockerfile, each directive performs a specific task defined by Docker.
  • When declaring these directives, they must be written in UPPERCASE.
  • A Dockerfile must begin with the FROM directive to declare which image will be used as the base to build your image.
  • arguments are the contents of the directives, determining what the directive will do.

Some directives in Dockerfile

FROM

FROM <image> [AS <name>]
FROM <image>[:<tag>] [AS <name>]
FROM <image>[@<digest>] [AS <name>]

LABEL

LABEL <key>=<value> <key>=<value> <key>=<value> ... <key>=<value> 

MAINTAINER

MAINTAINER <name> [<email>]

RUN

RUN <command>

ADD

ADD [--chown=<user>:<group>] <src>... <dest>
ADD [--chown=<user>:<group>] ["<src>",... "<dest>"]

COPY

COPY [--chown=<user>:<group>] <src>... <dest>
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]

ENV

ENV <key>=<value> ...

CMD Used to provide the default command that will be run when the Docker Container starts from the built Image, there can only be 1 CMD directive.

Practice

  1. Create a directory for the container image
mkdir ~/environment/container-image

Building a container image

  1. Enter cd ~/environment/container-image to change to that directory.
cd ~/environment/container-image

Building a container image

  1. Run touch Dockerfile to create a Dockerfile. This file will contain a set of steps needed to build the container image.
touch Dockerfile

Building a container image

  1. Run the command below to update the Dockerfile content
cat <<EOF > Dockerfile
FROM nginx\:latest
COPY index.html /usr/share/nginx/html
EOF

Building a container image

  1. Run touch index.html to create an empty html file that will contain a simple message.
touch index.html

Building a container image

  1. Use echo to pass a simple message into the index.html file
echo "We've added our own custom content into the container" >> index.html

Building a container image

  1. Use docker build -t nginx:1.0 . to build the nginx container image from the Dockerfile.
docker build -t nginx:1.0 .
  • Reference command to build an image from a Dockerfile
docker build [OPTIONS] PATH | URL | -

Building a container image

  1. Use docker history nginx:1.0 to view all the steps and the base container.
docker history nginx:1.0
  • Reference command to view the history of an image:
docker history [OPTIONS] IMAGE

Building a container image

  1. Use the command docker run -p 8081:80 –name nginx nginx:1.0 to run the container (not using detached mode to make debugging easier)
docker run -p 8081:80 --name nginx nginx:1.0

Building a container image

  1. Open another Terminal tab (Window -> New Terminal). Run curl http://localhost:8081 in the tab a few times and see the new content.
curl http://localhost:8081

Building a container image

  1. Go back to the first tab and see the log lines sent directly to STDOUT. Type Ctrl-C to exit the log output. Note that the container has stopped but is still there when you run docker ps -a.

Building a container image

  1. Use docker ps -a
docker ps -a

Building a container image

  1. Use sudo docker inspect nginx to see detailed information about the stopped container.
sudo docker inspect nginx

Building a container image

  1. Use docker rm nginx to remove the container
docker rm nginx

Building a container image

  1. Mount some files from the host into the container instead of embedding them in the image.
docker run -d -p 8081:80 -v /home/ec2-user/environment/container-image/index.html:/usr/share/nginx/html/index.html\:ro --name nginx nginx\:latest

Building a container image

  1. Execute curl http://localhost:8081. Note that even though this is an upstream nginx image from Docker Hub, the content is there.
curl http://localhost:8081

Building a container image

  1. Edit the index.html file
echo "This is another line I've added to my container" >> index.html

Building a container image

  1. Try curl http://localhost:8081 again
curl http://localhost:8081

Building a container image

  1. Finally, run docker stop nginx and docker rm nginx to stop and remove the container
docker stop nginx && docker rm nginx

Building a container image