search

Home  >  Q&A  >  body text

docker - 容器commit为镜像,再run成container, 端口映射丢失 该如何理解.

问个问题啊, 我run 了一个container hello,通过-p参数 映射主机端口 和 容器端口.

sudo docker run -d --name hello -p 8080:8080 -m 268435456 ubuntu:14.04 bin/bash -c "while true;do echo hello $(date); sleep 2; done"

然后通过 commit 命令生成新的镜像 sudo docker commit hello hello-image .
在然后, 基于该镜像,使用docker run -d hello-image 重新运行一个新的容器(这个时候并没有指定-p参数).
新生成的容器 是没有 端口映射的,而且也没有之前容器设置的-m参数信息.

我的问题是: 如何理解新生成的容器没有映射端口和 内存限制这两项信息? 什么参数是属于容器的,什么参数是属于镜像的呢?

ringa_leeringa_lee2760 days ago852

reply all(3)I'll reply

  • 阿神

    阿神2017-04-24 09:11:34

    When the container is committed into a mirror, only the static files will be retained, because the container itself is also stopped at this time

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-24 09:11:34

    You need to reserve the port, or use the dockerfile to open the port, and use the -P command when creating the container.
    Or still run similar to the way you specified the port above. Otherwise, the port will not be opened, and everything related to the host must be dynamically specified when the container needs to be created.
    sudo docker run -d --name hello -p 8080:8080 -m 268435456 ubuntu:14.04 bin/bash -c "while true;do echo hello $(date); sleep 2; done"

    reply
    0
  • ringa_lee

    ringa_lee2017-04-24 09:11:34

    Port mapping belongs to the container and will not exist in the image. It must be specified every time it runs.

    reply
    0
  • Cancelreply