簡體   English   中英

如何從 Amazon 上正在運行的容器創建新的 docker 鏡像?

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

這是我的問題:

我有一個在 Amazon ECS 上運行 Docker 映像的任務,但我想從容器的運行實例創建一個新的 Docker 映像。

我在 Amazon ECS 上看到了實例的 ID; 我已經制作了一個 AMI,但我想制作一個可以從 Amazon 中提取的新 docker 映像。

有什么想法嗎?

問候和感謝。

要從容器創建圖像,請執行以下命令:

docker commit hw_container hw_image

您可以運行docker commit ( docs ) 將容器保存到映像,然后將該映像與新標簽一起推送到注冊表。

這可以通過使用“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

除了@Ben Whaley 提供的答案,我個人建議您使用 Docker API。 要使用 Docker API,您需要配置 docker daemon 端口,這里解釋了配置 docker daemon 端口的過程

讓我們使用基本 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 是通過提交容器構建的新 Image Id。

獲取泊塢窗圖像

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

運行新構建的 Image 以查看在前一個容器中提交的更改。

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

從 Docker 文件構建鏡像, 如何使用 docker API 構建鏡像

有關docker API 的更多信息,請參閱此處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM