Monday, August 13, 2018

How to make an image/a container

What the goal is

We will use Docker and install Apache in container of CentOS 7. We will save the container as image for later use.

Install Docker toolbox in Windows

See this post to install Docker toolbox in Windows.

Pull CentOS 7 image

Double click on "Docker quick start" icon and after its boot, run the following command on the console:
$ docker pull centos
This command is to get the latest CentOS image. To get Cent OS 7 explicitly, use
$ docker pull centos:centos7
instead. For this time, we will pull the latest CentOS. After the pull command, we should have the latest CentOS image. Run the following to check what images were downloaded:
$ docker images

Seems like the "centos" image is there. Now run the following command to run the centos image's container and start its bash. (Container is a kind of a virtual server made by the image.)
$ docker run -i -t centos /bin/bash

[root@ffdb5501112c /]# (the container's bash started)
You can run "exit" to stop the bash (but you don't need to stop it yet). Or you can use docker's attach command to get the container's shell. See here.
OK, we have the bash to run commands on the CentOS 7 container now. Run the following commands to see information of the container:
[root@ffdb5501112c /]# uname -a
[root@ffdb5501112c /]# cat /etc/redhat-release

Install Apache

We started the container and its bash by this command:
$ docker run -i -t centos /bin/bash
On the bash, run the following command to install Apache to the container's virtual machine:
[root@ffdb5501112c /]# yum -y install httpd
And the virtual machine will install Apache. 


Save the state as image for later use

We have installed Apache in CentOS 7. We will save this container as another docker image for later use, which means we make "CentOS 7 + Apache" docker image. At first, exit the bash mode:
[root@ffdb5501112c /]# exit
Run the following command to see the container ID.
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED
STATUS                        PORTS               NAMES
5b49d3ec21fb        centos              "/bin/bash"         13 minutes ago
Exited (127) 14 seconds ago                       nervous_allen

The top of the list is the container ID, so seems like it is "5b49d3ec21fb". Run the following command to make the image from the container:
$ docker commit 5b49d3ec21fb centos7/apache
Now the image should have been saved! Check if it was really saved by this command:
$ docker images

The image was successfully saved. Now you can use the image anytime like this:
$ docker run -i -t centos7/apache /bin/bash

Delete Docker images

$ docker rmi [image name]

Docker images made by other people

Docker images are shared in Docker hub.

Build image from Dockerfile

You can also build image from Dockerfile. If you want to share an same environment in a team, share Dockerfile and build image from the Dockerfile.

Use multiple containers for Docker

If you use multiple containers for your environment, you can use Docker compose.