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

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

![](https://1309293527-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lt87Z2Vgpk1kICGRgqP%2F-MB7OyaZpweHVv7opD9n%2F-MB7Pik8VWAQKXVJZDo9%2Fpythonapp.png?alt=media\&token=4a34fa9e-8914-42b9-a088-0b34da7c9ea5)

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

```
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
```

![](https://1309293527-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lt87Z2Vgpk1kICGRgqP%2F-MB7RIxPngOCRD98Vzag%2F-MB7yDNGz-fNa-Dpf8T-%2Fpythonapp2.png?alt=media\&token=e06e2b3a-1f2e-4645-92ad-13e68c58b292)

