从已有container中生成新的image&打标签——Creating a Docker Image from an Existing Container
Posted panpanwelcome
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从已有container中生成新的image&打标签——Creating a Docker Image from an Existing Container相关的知识,希望对你有一定的参考价值。
原文:
https://docs.oracle.com/cd/E37670_01/E75728/html/section_c5q_n2z_fp.html
4.3 Creating a Docker Image from an Existing Container
If you modify the contents of a container, you can use the docker commit command to save the current state of the container as an image.
The following example demonstrates how to modify an container based on the oraclelinux:6.6
image so that it can run an Apache HTTP server. After stopping the container, the image mymod/httpd:v1
is created from it.
To create an Apache server image from an oraclelinux:6.6
container:
-
Run the bash shell inside a container named
guest
:[root@host ~]#
docker run -i -t --name guest oraclelinux:6.6 /bin/bash
[root@guest ~]# -
If you use a web proxy, edit the yum configuration on the guest as described in https://docs.oracle.com/cd/E52668_01/E54669/html/ol7-proxy-config.html in the Oracle Linux Administrator‘s Guide for Release 7.
-
Install the
httpd
package:[root@guest ~]#
yum install httpd
-
If required, create the web content to be displayed under the
/var/www/html
directory hierarchy on the guest. -
Exit the guest by using the docker stop command on the host:
[root@host ~]#
docker stop guest
guest -
Create the image
mymod/httpd
with the tagv1
using the ID of the container that you stopped:[root@host ~]#
docker commit -m "ol6 + httpd" -a "A N Other"
`docker ps -l -q` mymod/httpd:v1
8594abec905e6374db51bed1bfb208804cfb60d96b285efb897db581a01676e9Use the -m and -a options to document the image and its author. The command returns the full version of the new image‘s ID.
If you use the docker images command, the new image now appears in the list:
[root@host ~]#
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE mymod/httpd v1 8594abec905e 2 minutes ago 938.5 MB oraclelinux 6 9ac13076d2b5 5 days ago 319.4 MB oraclelinux 6.6 9ac13076d2b5 5 days ago 319.4 MB oraclelinux latest 073ded22ac0f 5 days ago 265.2 MB oraclelinux 7 073ded22ac0f 5 days ago 265.2 MB oraclelinux 7.0 073ded22ac0f 5 days ago 265.2 MB -
Remove the container named
guest
.#
docker rm guest
guest
You can now use the new image to create a container that works as a web server, for example:
# docker run -d --name newguest -p 8080:80 mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND
7afbbefec5191f632e149f85ae10ed0ba88f1c545daad18cb930e575ef6a3e63
The -d option runs the command non-interactively in the background and displays the full version of the unique container ID. The -p 8080:80 option maps port 80 in the guest to port 8080 on the host. You can view the port mapping by running docker ps or docker port, for example:
[root@host ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7afbbefec519 mymod/httpd:v1 ... ... 0.0.0.0:8080->80/tcp newguest [root@host ~]#docker port newguest 80
0.0.0.0:8080
The docker ps command displays the short version of the container ID. You can use the --no-trunc option to display the long version.
The default IP address value of 0.0.0.0 means that the port mapping applies to all network interfaces on the host. You can restrict the IP addresses to which the remapping applies by using multiple -p options, for example:
#docker run -d --name newguest -p 127.0.0.1:8080:80 -p 192.168.1.2:8080:80
mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND
You can view the web content served by the guest by pointing a browser at port 8080 on the host. If you access the content from a different system, you might need to allow incoming connections to the port on the host, for example:
[root@host ~]#iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
[root@host ~]#service iptables save
If you need to remove an image, use the docker rmi command:
[root@host ~]# docker rmi mymod/httpd:v1
Untagged: mymod/httpd:v1
Deleted: 7afbbefec5191f632e149f85ae10ed0ba88f1c545daad18cb930e575ef6a3e63
From version 1.8 of Docker, you cannot remove the image of a running container.
In a production environment, using the docker commit command to create an image does not provide a convenient record of how you created the image so you might find it difficult to recreate an image that has been lost or become corrupted. The preferred method for creating an image is to set up a Dockerfile, in which you define instructions that allow Docker to build the image for you. See Section 4.4, “Creating a Docker Image from a Dockerfile”.
参考:
docker commit :从容器创建一个新的镜像。
语法
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS说明:
-
-a :提交的镜像作者;
-
-c :使用Dockerfile指令来创建镜像;
-
-m :提交时的说明文字;
-
-p :在commit时,将容器暂停。
实例
将容器a404c6c174a2 保存为新的镜像,并添加提交人信息和说明信息。
runoob@runoob:~$ docker commit -a "runoob.com" -m "my apache" a404c6c174a2 mymysql:v1 sha256:37af1236adef1544e8886be23010b66577647a40bc02c0885a6600b33ee28057 runoob@runoob:~$ docker images mymysql:v1 REPOSITORY TAG IMAGE ID CREATED SIZE mymysql v1 37af1236adef 15 seconds ago 329 MB
应用
root@43497210935f:/# exit # 从容器退出 exit PS C:****> docker commit -a "p#***" -m "unminimize & xfs" 43497210935f ubuntu:v1_xfs_20200611 #创建image sha256:236e6d89bcd2d73cc6a64fcc9d59633f6007bfbd9efe824c85ce0584e18343270935f ubuntu:v1_xfs_20200611 PS C:****> docker images #查看image REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu v1_xfs_20200611 236e6d89bcd2 49 minutes ago 2.37GB 192.168.33.21:5000/westwin/msap/msap.service.workflow.webapi v1.2.2.16 2e5a43d3483d 2 weeks ago 1.24GB msap.service.workflow.webapi PS C:****> docker run -ti 236e6d89bcd2 #创建container并进入 root@8bb1a44c1ad6:/# ls backups bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@8bb1a44c1ad6:/# root@8bb1a44c1ad6:/#
以上是关于从已有container中生成新的image&打标签——Creating a Docker Image from an Existing Container的主要内容,如果未能解决你的问题,请参考以下文章
在当前 HTTPContext 中生成新的 ASP.NET 会话