27

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.

Alex Watt
  • 927
  • 5
  • 14
Bussiere
  • 500
  • 13
  • 60
  • 119

4 Answers4

49

To create a image from container execute the command below:

docker commit container_id imagename

T.Todua
  • 53,146
  • 19
  • 236
  • 237
Renato Coutinho
  • 1,151
  • 10
  • 7
8

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

Ben Whaley
  • 32,811
  • 7
  • 87
  • 85
5

This can be easily done by using "docker commit".

Let's say you need an image, based on the latest from NGINX, with PHP, build-essential, and nano installed. I'll walk you through the process of pulling the image, running the container, accessing the container, adding the software, and committing the changes to a new image that can then be easily used as a base for your dev containers.

Pulling the image and running the container:

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

Modifying the container:

apt-get install nano
​apt-get install php5

Commit the changes:

sudo docker commit CONTAINER_ID nginx-template

The newly created template is ready and you can run using:

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

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.

Here_2_learn
  • 5,013
  • 15
  • 50
  • 68
  • Would you please explain what advantages it offers to create an image via Docker API, as opposed from the command line (assuming you have access to the command line)? – His Jul 02 '18 at 00:45
  • 1
    There are couples of advantages I feel 1) When your containers are deployed on multiple servers getting required information(container stats, resource usage and process running) will be easy with APIs 2) Automating & managing the docker deployments on multiple servers is possible if container orchestrator is not used. 3) Finally, you don't need access to the server to get the containers info or to deploy them. – Here_2_learn Jul 02 '18 at 05:09