> For the complete documentation index, see [llms.txt](https://cutejaneii.gitbook.io/docker/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cutejaneii.gitbook.io/docker/docker-container/li-yong-rong-qi-jian-li-ying-xiang-dang.md).

# 利用互動後的容器建立映像檔

假設我建立了一個容器之後，在容器內進行了一些指令（新增檔案或是安裝套件，任何指令都算），希望能夠依此容器來建立新映像檔，可利用`docker commit`。

建容器前，用`docker history`看一下映像檔`cutejaneii/pythonapp`的內容

```
root@mv:/home/jennifer# docker history cutejaneii/pythonapp
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
de1bcecfcad9        3 hours ago         /bin/sh -c #(nop)  CMD ["python" "main.py"]     0B
993212ec1639        3 hours ago         /bin/sh -c #(nop) COPY dir:5f74b7454f45cc72f…   260B
fb8ad4738161        3 hours ago         /bin/sh -c pip install flask                    11.2MB
d15e7a9f900e        2 weeks ago         /bin/sh -c #(nop)  CMD ["python3"]              0B
<missing>           2 weeks ago         /bin/sh -c set -ex;   wget -O get-pip.py "$P…   6.51MB
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_SHA256…   0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_URL=ht…   0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=20…   0B
<missing>           2 weeks ago         /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32B
<missing>           2 weeks ago         /bin/sh -c set -ex  && apk add --no-cache --…   55.5MB
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.5.9     0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV GPG_KEY=97FC712E4C024…   0B
<missing>           2 weeks ago         /bin/sh -c apk add --no-cache ca-certificates   512kB
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0B
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:c92c248239f8c7b9b…   5.57MB
```

接著建立容器，再瀏覽器看一下網站`http://host-ip/`

```
root@vm:/home/jennifer# docker run -d --net=host cutejaneii/pythonapp
927c93e53633b1860656608713316f48930fda7cfd90fb0779706ad93afe7fef
```

![](/files/-MB7Pik8VWAQKXVJZDo9)

接著我們進入容器內，修改內容後，再建立一個新的映像檔

```
root@vm:/home/jennifer# docker exec -it 927c sh
/ # vi main.py
/ # exit
```

修改main.py內容

```
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World! (Python+Flask+Docker)"

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True, port=80)
```

`docker commit`成一個新的映像檔`cutejaneii/pythonapp2`

```
root@vm:/home/jennifer# docker commit b495 cutejaneii/pythonapp2
sha256:011402b3da74aa9923a158f2d3803b9e354dce404ccd0ad6156b2df2707fde31
```

再來用這個新的映像檔`cutejaneii/pythonapp2`建立容器，就打開瀏覽器，就可以發現內容已改變

```
root@vm:/home/jennifer# docker run -d --net=host cutejaneii/pythonapp2
```

![](/files/-MB7yDNGz-fNa-Dpf8T-)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cutejaneii.gitbook.io/docker/docker-container/li-yong-rong-qi-jian-li-ying-xiang-dang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
