码迷,mamicode.com
首页 > 其他好文 > 详细

从已有container中生成新的image&打标签——Creating a Docker Image from an Existing Container

时间:2020-06-11 20:04:51      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:href   red   rom   apach   The   diff   说明文   install   virtual   

原文:

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:

  1. Run the bash shell inside a container named guest:

    [root@host ~]# docker run -i -t --name guest oraclelinux:6.6 /bin/bash
    [root@guest ~]#
  2. 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.

  3. Install the httpd package:

    [root@guest ~]# yum install httpd
  4. If required, create the web content to be displayed under the /var/www/html directory hierarchy on the guest.

  5. Exit the guest by using the docker stop command on the host:

    [root@host ~]# docker stop guest
    guest
  6. Create the image mymod/httpd with the tag v1 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
    8594abec905e6374db51bed1bfb208804cfb60d96b285efb897db581a01676e9

    Use 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
  7. 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
Note

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
Note

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 :从容器创建一个新的镜像。

语法

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

标签:href   red   rom   apach   The   diff   说明文   install   virtual   

原文地址:https://www.cnblogs.com/panpanwelcome/p/13095502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!