Docker is an open platform management tool for Linux Containers. It provides a means for developers and system administrators to build and package applications into lightweight containers. Docker consists of the following components:
- Docker Engine – A portable, lightweight runtime and packaging tool
- Docker Hub – A cloud service for sharing applications and automating workflows
Docker is used to create image-based application containers. Image-based containers package an application with the individual runtime stack into a single container. This makes the container independent from the host operating system and kernel version. As a result, you can run the same application, unchanged, on laptops, data center virtual machines, and any cloud. You can transfer this container to another machine that runs Docker and run the application without any compatibility issues.
Creating an Image from a Container
You can save the current state of a container as a new image by using the “docker commit” command. This is useful if you have modified a container and want to commit the changes to a new image for later use.
The example in the slide creates a new container named “geeklab” from the centos:7 image and runs the bash shell command in the container.
# docker run -i -t --name geeklab centos:7 /bin/bash [root@873abc18f59d /]#
From within the container, the yum command is used to install the httpd package.
[root@873abc18f59d /]# yum install httpd
Use the exit command to stop a running container.
[root@873abc18f59d /]# exit exit
The “docker commit” command saves the changes to a new image. Use the -m option to provide a message describing the changes. Use the -a option to provide author information. Provide the container ID or container name, the image name, and a tag. Example:
# docker commit -m="CentOS 7 With httpd installed" -a "Geek Lab" geeklab centos7/httpd:v1 sha256:b3c42dd36e247cabcfaf134afb4765e9853ebfbe856c8d785f65cfafc9fd53cd
The output of the docker images command now includes the new image.
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7/httpd v1 b3c42dd36e24 20 seconds ago 334 MB
Creating an Image from a Dockerfile
Use the docker build command to create a new image from the instructions contained in a file named “Dockerfile“. The format of the Dockerfile is:
# Comment INSTRUCTION arguments
The instruction is not case-sensitive but convention is to capitalize the instruction to distinguish it from the arguments. Docker runs the instructions in a Dockerfile in order. You build a new image from a base image. The first instruction is FROM and specifies the base image to use. Example:
Use the RUN instruction to specify the commands to run in a new layer on top of the current image and commit the results. Example:
RUN yum –y install httpd
The ENTRYPOINT instruction specifies the command that the container created from the image runs. Example:
ENTRYPOINT /usr/sbin/httpd –D FOREGROUND
Refer to the dockerfile man page for a description of all the instructions. The following URL also provides descriptions, usage, and examples of all the available Dockerfile instructions: https://docs.docker.com/reference/builder/.
Save and Load an Image or a Container
You can create tar files of images and containers to use on systems that do not have access to Docker Hub. Use the “docker save” command to save images to a tar file. You can either save all images in a repository to a tar file, or save a specific image to a tar file. Create the tar file either by redirecting STDOUT to a tar file or use the -o option to specify an output tar file name.
The following example redirects STDOUT to save all images in the centos repository to centos-all.tar:
# docker save centos > centos-all.tar
The following example saves the centos:latest image to the centos-lates.tar file.
# docker save -o=centos-latest.tar centos:latest
Use the docker load command to load an image from a tar file to a local Docker repository. The following example loads the images from the centos-all.tar file:
# docker load --input centos-all.tar