Skip to content

Latest commit

 

History

History
123 lines (83 loc) · 4.05 KB

File metadata and controls

123 lines (83 loc) · 4.05 KB

Build an Image

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.

Dockerfile

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:

Table 1. Common commands for Dockerfile
Command Purpose Example

FROM

First non-comment instruction in Dockerfile

FROM ubuntu

COPY

Copies mulitple source files from the context to the file system of the container at the specified path

COPY .bash_profile /home

ENV

Sets the environment variable

ENV HOSTNAME=test

RUN

Executes a command

RUN apt-get update

CMD

Defaults for an executing container

CMD ["/bin/echo", "hello world"]

EXPOSE

Informs the network ports that the container will listen on

EXPOSE 8093

Create your first Docker image

  1. Create a new directory 'helloworld' and "cd" into it.

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

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

  1. 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
  1. Run the container:

$ docker run -it helloworld

to see the output:

hello world
  1. Change the base image from fedora to busybox in Dockerfile. 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

WildFly Image

  1. Create a new directory.

  2. Create a new text file, name it Dockerfile, and use the following contents:

FROM jboss/wildfly
  1. Build the image:

    $  docker build -t mywildfly .
  2. Run the container:

    $  docker run -it mywildfly

Import and export images

Docker images can be saved using save command to a .tar file:

$  docker save helloworld > helloworld.tar

These tar files can then be imported using load command:

$  docker load -i helloworld.tar