PURPOSE: This chapter explains how to create a Docker image.
As explained in [Docker_Basics], Docker image is the build component of Docker and a read-only template of application operating system.
Docker build images by reading instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. docker build command uses this file and executes all the commands in succession to create an image.
build command is also passed a context that is used during image creation. This context can be a path on your local filesystem or a URL to a Git repository.
Dockerfile is usually called Dockerfile. The complete list of commands that can be specified in this file are explained at https://docs.docker.com/reference/builder/. The common commands are listed below:
| Command | Purpose | Example |
|---|---|---|
FROM |
First non-comment instruction in Dockerfile |
|
COPY |
Copies mulitple source files from the context to the file system of the container at the specified path |
|
ENV |
Sets the environment variable |
|
RUN |
Executes a command |
|
CMD |
Defaults for an executing container |
|
EXPOSE |
Informs the network ports that the container will listen on |
|
-
Create a new directory 'helloworld' and "cd" into it.
-
Create a new text file, name it Dockerfile, and use the following contents:
FROM fedora
CMD ["/bin/echo", "hello world"]This image uses fedora as the base image. CMD command defines the command that needs to run. It provides a different entry point of /bin/echo and gives the argument “hello world”.
-
Build this image:
$ docker build -t helloworld .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM fedora
latest: Pulling from library/fedora
369aca82a5c0: Pull complete
a887c7ad7f3f: Pull complete
Digest: sha256:a220110e096cd1d6034699a5e30a012de6c55c83149d01658120918d01f1587a
Status: Downloaded newer image for fedora:latest
---> a887c7ad7f3f
Step 2 : CMD /bin/echo hello world
---> Running in 47e65a18749c
---> 405849ed68f1
Removing intermediate container 47e65a18749c
Successfully built 405849ed68f1. in this command is the context for docker build.
-
List the images available:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
helloworld latest 405849ed68f1 18 seconds ago 204.3 MB
fedora latest a887c7ad7f3f 6 days ago 204.3 MB-
Run the container:
$ docker run -it helloworld
to see the output:
hello world
-
Change the base image from
fedoratobusyboxinDockerfile. Build the image again:
$ docker build -t helloworld2 .
and view the images as:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
helloworld2 latest 3dece940fa4a 6 seconds ago 1.109 MB
helloworld latest 405849ed68f1 About a minute ago 204.3 MB
fedora latest a887c7ad7f3f 6 days ago 204.3 MB
busybox latest c51f86c28340 8 days ago 1.109 MB-
Create a new directory.
-
Create a new text file, name it Dockerfile, and use the following contents:
FROM jboss/wildfly-
Build the image:
$ docker build -t mywildfly .
-
Run the container:
$ docker run -it mywildfly