Docker – Images and containers

An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.

A Docker image is made up of a collection of files that bundle together all the essentials, such as installations, application code and dependencies, required to configure a fully operational container environment.

You can create a Docker image in one of two ways:

  • Interactive Method: By running a container from an existing Docker image, manually changing that container environment through a series of live steps and saving the resulting state as a new image.
  • Dockerfile Method: By constructing a plain-text file, known as a Dockerfile, which provides the specifications for creating a Docker image.

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

Image Layers

Each of the files that make up a Docker image is known as a layer. These layers form a series of intermediate images, built one on top of the other in stages, where each layer is dependent on the layer immediately below it.

This is because, when you make changes to a layer in your image, Docker not only rebuilds that particular layer but all layers built from it. Therefore a change to a layer at the top of the stack involves the least amount of computational work to rebuild the entire image.

Container Layer

Each time Docker launches a container from an image, it adds a thin writable layer, known as the container layer, which stores all changes to the container throughout its runtime.

Below Commands used for Docker Image Creation in Dockerfile:

CommandPurpose
FROMTo specify the parent image.
WORKDIRTo set the working directory for any commands that follow in the Dockerfile.
RUNTo install any applications and packages required for your container.
COPYTo copy over files or directories from a specific location.
ADDAs COPY, but also able to handle remote URLs and unpack compressed files.
ENTRYPOINTCommand that will always be executed when the container starts. If not specified, the default is /bin/sh –c
CMDArguments passed to the entrypoint. If ENTRYPOINT is not set (defaults to /bin/sh -c), the CMD will be the commands the container executes.
EXPOSETo define which port through which to access your container application.

Example Dockerfile

FROM ubuntu:16.04

RUN apt-get update &&
apt-get install -y nginx curl   # Install nginx and curl


Below are some command examples:

  • Run below command to create image

# docker build –t image_name:tag .

e.g. docker build –t testimage:latest .

  • List available images:

# docker images

REPOSITORY     TAG  IMAGE ID        CREATED        SIZE

nginx       0.4  f45ae2g1355b    1 seconds ago 148MB

ubuntu         16.04 ddd6e57d412v  12 days ago    62.2MB

  • To remove docker images:

# docker rmi image_name/image_ID

  • To get Help on docker build Run below command

# docker build –help

  • To check image layers

# docker history image_ID/name

  • To get more details about Image

# docker image inspect Image_ID/name

  • To run Container using docker image

# docker run –name container_name Image_name:tag

# docker run –d –p 8080:8085 –name test test:latest

Above command run container named test in detach mode from test:latest image with publishing port 8085 from container to bind 8080 host port.

  • To check all running container status:

# docker ps

  • To check all container status:

# docker ps –a

  • To login in container:

# docker exec –it container_id shell(eg sh/bash)

  • To run command in specified container:

# docker exec container_id command

Options used in “docker run” command:

–cpusNumber of CPUs
–env , -eSet environment variables
–exposeExpose a port or a range of ports
–helpPrint usage
–interactive , -iKeep STDIN open even if not attached
–memory , -mMemory limit
–mount Attach a filesystem mount to the container
–name Assign a name to the container
–publish , -p Publish a container’s port(s) to the host
–tty , -t Allocate a pseudo-TTY
–volume , -v Bind mount a volume
–workdir , -w Working directory inside the container
–volumes-from Mount volumes from the specified container(s)

References:

  1. https://docs.docker.com
  2. from Miscellaneous technical websites.