Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 如何从Amazon上运行的容器创建新的docker映像?_Amazon Web Services_Docker_Amazon Ecs - Fatal编程技术网

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

Amazon web services 如何从Amazon上运行的容器创建新的docker映像?,amazon-web-services,docker,amazon-ecs,Amazon Web Services,Docker,Amazon Ecs,我的问题是: 我有一个在Amazon ECS上运行Docker映像的任务,但我想从容器的运行实例创建一个新的Docker映像 # docker run -it ubuntu:15.0 /bin/bash root@3a48af5eaec9:/# cd /root/ root@3a48af5eaec9:~# ls TEST_DIR root@3a48af5eaec9:~# exit 我在Amazon ECS上看到了实例的id;我已经做了一个AMI,但我想做一个新的docker形象,我可以从亚马逊

我的问题是:

我有一个在Amazon ECS上运行Docker映像的任务,但我想从容器的运行实例创建一个新的Docker映像

# docker run -it ubuntu:15.0 /bin/bash
root@3a48af5eaec9:/# cd /root/
root@3a48af5eaec9:~# ls
TEST_DIR
root@3a48af5eaec9:~# exit
我在Amazon ECS上看到了实例的id;我已经做了一个AMI,但我想做一个新的docker形象,我可以从亚马逊拉

有什么想法吗


问候并感谢。

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

除了@Ben Whaley提供的答案之外,我个人建议您使用Docker API。要使用Docker API,您需要配置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
#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"
  }
}
sudo docker pull nginx
sudo docker run  -it --name nginx-template-base -p 8080:80 nginx
apt-get install nano
​apt-get install php5
JSON文件,作为提交容器的输入:

# 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
#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"
  }
}
sudo docker pull nginx
sudo docker run  -it --name nginx-template-base -p 8080:80 nginx
apt-get install nano
​apt-get install php5
提交容器的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 run -it ubuntu:15.0 /bin/bash
root@3a48af5eaec9:/# cd /root/
root@3a48af5eaec9:~# ls
TEST_DIR
root@3a48af5eaec9:~# exit
获取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文件生成映像


有关docker API的更多信息,请参阅从容器创建映像执行以下命令:

docker提交硬件容器硬件图像


这可以通过使用“docker提交”轻松完成

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

拉动映像并运行容器:

# 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
#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"
  }
}
sudo docker pull nginx
sudo docker run  -it --name nginx-template-base -p 8080:80 nginx
apt-get install nano
​apt-get install php5
修改容器:

# 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
#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"
  }
}
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

请您解释一下,与从命令行(假设您可以访问命令行)创建映像相比,通过Docker API创建映像有哪些优势?当您的容器部署在多台服务器上获取所需信息(容器统计、资源使用和进程运行)时,我觉得有几个优势1)使用API将很容易2)如果不使用容器orchestrator,则可以在多台服务器上自动化和管理docker部署。3) 最后,您不需要访问服务器来获取容器信息或部署它们。