Docker provides us cool features to build applications in the complete isolated environment. we could manage and deploy our application with very minimal resources in Docker container.Docker_docs
So if we have a container that hosting a useful application and we like to setup same application on another machine, then we need to save the same state of the container into an image and move it to another Host machine to launch container from the same image.

So, In Docker, we have features to save container changes in another image to launch container from same. This container would have a same deployed application that was working on the first Host machine.

In this post, we would understand how we could save co

Setup

Container changes in an images.

For this post we have one Ubuntu 17.04 host with Docker 17.06

root@srvU:~# lsb_release -d
Description:    Ubuntu 17.04
root@srvU:~# docker -v
Docker version 17.06.0-ce, build 02c1d87

Now we have one Ubuntu image which we run and perform some changes to commit into a new image.

root@srvU:~# docker images ubuntu
REPOSITORY  TAG     IMAGE ID      CREATED     SIZE
ubuntu      latest  14f60031763d  3 days ago  120MB

Run Container over official Image

Let’s run it with bash to connect like below

root@srvU:~# docker run -it --name ubuntu-latest ubuntu /bin/bash
root@0731a89a026e:/# cat /etc/issue
Ubuntu 16.04.2 LTS \n \l

root@srvU:~# docker ps -a
CONTAINER ID  IMAGE    COMMAND      CREATED         STATUS          PORTS    NAMES
0731a89a026e  ubuntu   "/bin/bash"  2 minutes ago   Up 2 minutes             ubuntu-latest

Perform some changes

Now we would perform some changes on Ubuntu and commit it into a new image.

root@af1b3db2b163:/# apt-get update > /dev/null 2>&1 ; echo $?
0
root@af1b3db2b163:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.1  18244  3288 pts/0    Ss   19:46   0:00 /bin/bash
root       195  0.0  0.1  34428  2832 pts/0    R+   19:47   0:00 ps aux
root@af1b3db2b163:/# apt-get install openssh-server -y > /dev/null 2>&1 ; echo $?
0
root@af1b3db2b163:~# /etc/init.d/ssh start
 * Starting OpenBSD Secure Shell server sshd        [ OK ]
root@af1b3db2b163:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  18248  3308 pts/0    Ss   19:46   0:00 /bin/bash
root      3632  0.0  0.1  65524  3124 ?        Ss   19:50   0:00 /usr/sbin/sshd
root      3634  0.0  0.1  34428  2848 pts/0    R+   19:50   0:00 ps aux

Now we SSH server installed on ubuntu Container, which is running and should connect from outside. Let’s check. for same we have to perform some changes in SSH configuration file and set root password.

root@af1b3db2b163:~# grep Permit /etc/ssh/sshd_config
PermitRootLogin prohibit-password
PermitEmptyPasswords no
# the setting of "PermitRootLogin without-password".
root@af1b3db2b163:~# sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config


root@af1b3db2b163:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Now we should connect Ubuntu container from outside (from Host Machine)

root@srvU:~# ssh "172.17.0.2"
root@172.17.0.2's password:
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.10.0-19-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@af1b3db2b163:~# logout
Connection to 172.17.0.2 closed.

Commit Container changes

I think, we have done plenty of changes on ubuntu container, we should commit changes and build new image.

root@srvU:~# docker commit -a Sang.Sirohi -m "This ubuntu has OpenSSH installd"  ubuntu-latest ubuntu-with-ssh
sha256:1d66b84132843f9d0db5d4d87693841db98d3dc82bf381e159e0cabf81069098


root@srvU:~# docker inspect ubuntu-with-ssh | egrep "Author|Comment"
        "Comment": "This Ubuntu has OpenSSH Installed",
        "Author": "Sang.Sirohi",

In above command, I tried to cover few more points -a, Author and -m, Message. Same could inspect with Container properties as well.

Let’s launch another Container and see those changes persistent or not.

root@srvU:~# docker images ubuntu-with-ssh
REPOSITORY          TAG      IMAGE ID      CREATED         SIZE
ubuntu-with-ssh     latest   1d66b8413284  5 minutes ago  213MB



root@srvU:~# docker run  -it ubuntu-with-ssh
root@975cdcd9a93c:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.4  0.1  18244  3348 pts/0    Ss   20:17   0:00 /bin/bash
root         9  0.0  0.1  34428  2872 pts/0    R+   20:17   0:00 ps aux
root@975cdcd9a93c:/# /etc/init.d/ssh status
 * sshd is not running
root@975cdcd9a93c:/# /etc/init.d/ssh start
 * Starting OpenBSD Secure Shell server sshd        [ OK ]

Let’s check SSH service from outside container

root@srvU:~# docker ps -a
CONTAINER ID    IMAGE               COMMAND        CREATED             STATUS          PORTS   NAMES
a201ec37daec    ubuntu-with-ssh     "/bin/bash"    3 minutes ago       Up 3 minutes            friendly_montalcini
0731a89a026e    ubuntu              "/bin/bash"    15 minutes ago      Up 15 minutes           ubuntu-latest

# docker inspect a201ec37daec | grep \"IPAdd
            "IPAddress": "172.17.0.3",

root@srvU:~# ssh "172.17.0.3"
The authenticity of host '172.17.0.3 (172.17.0.3)' can't be established.
ECDSA key fingerprint is SHA256:+yEhwbi7iANhsJLPAQ//Px7JMTvi+vEA/7q7lk1qmQs.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.3' (ECDSA) to the list of known hosts.
root@172.17.0.3's password:
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.10.0-19-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
Last login: Sun Jul 23 20:57:14 2017 from 172.17.0.1
root@a201ec37daec:~# logout
Connection to 172.17.0.3 closed.

You should always see command-line (docker commit –help) help and docker website help for the same topic