Docker: Create Image from Container

来源:互联网 发布:淘宝允许好评返现吗 编辑:程序博客网 时间:2024/05/20 12:46

Docker: Create Image from Container

I’m going to create my own image from a running container for further sharing. So, I’m planning to get a container as base, run it, commit as Image, export image as a file and share with friends, and finally be loaded by them.

Here I list the steps:

Base container

Get a base Image

docker pull centos

Run a base Container

docker run -it centos /bin/bash

Update Container

Modify Container

I will create a file /usr/local/bin/echo, with content:

#!/bin/bashecho "hi, $@"

and make it executable.

# chmod a+x echo

See Container status

docker ps -a

Terminal message:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                                              NAMESc71580983e83        centos              "/bin/bash"              44 seconds ago      Exited (0) 3 seconds ago                                                      echo

Now we get container id: c71580983e83

Create Image

Commit Container as Image

Save the image as echo:v1, which name echo with tag v1.

docker commit -m "echo container" -a "wang xiaoqiang" c71580983e83 echo:v1

See the images

docker images

Terminal text:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEecho                v1                  511b808ecb31        12 seconds ago      196.7 MB

So we get the image locally 511b808ecb31 in name echo:v1.

Use Image

Save Image

docker save echo:v1 >echo-v1.tar

Load Image

docker load < echo-v1.tar

Terminal text:

8db4df206a2d: Loading layer [==================================================>]  5.12 kB/5.12 kB

Then image can be loaded in local repository.

1 0