How to create a new docker image from a running co

2019-02-25 03:02发布

问题:

Here is my problem :

I have a task running a docker image on amazon ECS but i would like to make a new docker image from the running instance of the container.

I see the id of the instance on Amazon Ecs , i have made an AMI but i would like to make a new docker image that i can pull from amazon.

Any ideas ?

Regards and thanks

回答1:

Apart from the answer provided by @Ben Whaley, I personally suggest you to make use of Docker APIs. To use Docker APIs you need to configure the docker daemon port and the procedure is explained here configuring docker daemon port

Lets run a container using an base Ubuntu Image and create a folder inside the container:

#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

Status of the exited container:

# 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 file which is an input for committing a container:

#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 to commit a container

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

The Id that is generated is the new Image Id which is build from committing a container.

Get docker Images

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

Run the newly build Image to see the changes commited in the previous container.

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

To build an image from Docker file, how to build an image using docker API

For more information on docker APIs, refer here.



回答2:

You can run docker commit (docs) to save the container to an image, then push that image with a new tag to the registry.



回答3:

To create a image from container execute the command below:

docker commit hw_container hw_image