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 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
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
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
- Create a directory for the container image
mkdir ~/environment/container-image

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

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

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

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

- 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

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

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

- 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

- 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

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

- Use docker ps -a

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

- Use docker rm nginx to remove the container

- 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

- 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

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

- Try curl http://localhost:8081 again
curl http://localhost:8081

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