如何从 Amazon 上的 运行 容器创建新的 docker 图像?

How to create a new docker image from a running container on Amazon?

这是我的问题:

我在 Amazon ECS 上有一个任务 运行 Docker 图像,但我想从容器的 运行 实例制作一个新的 Docker 图像。

我在 Amazon ECS 上看到实例的 ID;我制作了一个 AMI,但我想制作一个新的 docker 图像,我可以从 Amazon 中提取它。

有什么想法吗?

问候和感谢。

您可以运行 docker commit (docs) 将容器保存到镜像中,然后将带有新标签的镜像推送到注册表。

除了@Ben Whaley 提供的答案外,我个人建议您使用Docker APIs。 使用Docker APIs 您需要 配置 docker 守护程序端口,此处说明了该过程

让运行容器使用基础Ubuntu图像并在容器内创建文件夹:

#docker run -it ubuntu:14.04 /bin/bash
root@58246867493d:/# 
root@58246867493d:/# cd /root
root@58246867493d:~# ls
root@58246867493d:~# mkdir TEST_DIR
root@58246867493d:~# exit

退出容器的状态:

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
58246867493d        ubuntu:14.04        "/bin/bash"         2 minutes ago       Exited (127) 57 seconds ago                       hungry_turing

JSON 提交容器的输入文件:

#cat container_create.json 
{
  "AttachStdin": true,
  "AttachStdout": true,
  "AttachStderr": true,
  "ExposedPorts": {
    "property1": {},
    "property2": {}
  },
  "Tty": true,
  "OpenStdin": true,
  "StdinOnce": true,
  "Cmd": null,
  "Image": "ubuntu:14.04",
  "Volumes": {
    "additionalProperties": {}
  },
  "Labels": {
    "property1": "string",
    "property2": "string"
  }
}

API 提交容器

# curl -X POST http://127.0.0.1:6000/commit?container=58246867493d\&repo=ubuntu\&tag=15.0 -d @container_create.json --header "Content-Type: application/json" | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   593  100    81  100   512    175   1106 --:--:-- --:--:-- --:--:--  1108
{
  "Id": "sha256:acac1f3733b2240b01e335642d2867585e5933b18de2264315f9b07814de113a"
}

生成的 Id 是通过提交容器构建的新图像 Id。

获取 docker 图片

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
**ubuntu              15.0                acac1f3733b2        10 seconds ago      188MB**
ubuntu              14.04               132b7427a3b4        10 hours ago        188MB

运行 新建镜像以查看在先前容器中提交的更改。

# docker run -it ubuntu:15.0 /bin/bash
root@3a48af5eaec9:/# cd /root/
root@3a48af5eaec9:~# ls
TEST_DIR
root@3a48af5eaec9:~# exit

要从 Docker 文件构建图像,

有关有关dockerAPIs的更多信息,请参阅here.

要从容器创建映像,请执行以下命令:

docker commit container_id imagename

这可以通过使用 "docker commit" 轻松完成。

假设您需要一个映像,它基于 NGINX 的最新版本,安装了 PHP、build-essential 和 nano。我将引导您完成拉取映像、运行 连接容器、访问容器、添加软件以及将更改提交到新映像的过程,该映像随后可以轻松用作开发的基础容器。

拉取镜像并运行安装容器:

sudo docker pull nginx
sudo docker run  -it --name nginx-template-base -p 8080:80 nginx

修改容器:

apt-get install nano
​apt-get install php5

提交更改:

sudo docker commit CONTAINER_ID nginx-template

新创建的模板已准备就绪,您可以运行使用:

sudo docker run -it --name nginx-dev -p 8080:80 nginx-template