container-commit
通过 commit 创建镜像
docker container commit [container-name/id] [dockerhub-id]/[docker-image]:[tag]
docker container commit
docker images查看本地镜像docker container run -d -p 80:80 --name hello-nginx nginx运行 hello-nginx 容器docker container exec -it hello-nginx sh进入 hello-nginx 容器内部curl "http://127.0.0.1"发送 GET 请求echo "<h1>hello nginx, hello jaime</h1>" >/usr/share/nginx/html/index.html更改网页内容docker container stop hello-nginx停止 hello-nginx 容器docker container start hello-nginx启动 hello-nginx 容器docker container commit hello-nginx jaimezeng/hello-nginx:0.0.1使用 docker container commit 创建镜像docker image push jaimezeng/hello-nginx:0.0.1推送镜像到 dockerhubdocker container stop hello-nginx && docker container rm hello-nginx && docker image rm jaimezeng/hello-nginx:0.0.1停止容器后删除容器,然后删除本地镜像docker container run -d -p 80:80 --name hello-nginx jaimezeng/hello-nginx:0.0.1拉去镜像并后台运行docker container exec -it hello-nginx curl "http://127.0.0.1"在容器内发送 GET 请求

练习 - 使用 container commit 构建镜像执行 Python 程序
例子:在 [[dockerfile-intro#Dockerfile 基本结构]] 中 执行 Python 程序
docker image pull ubuntu:22.04拉取 ubuntu:22.04 镜像docker container run --name hello-jaime -it ubuntu:22.04 sh进入容器内部apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip安装依赖echo 'print("hello docker, hello jaime")' >hello.py添加hello.py文件exit/ Ctrl + d退出容器终端docker container commit hello-jaime jaimezeng/hello-jaime:0.0.1打包镜像docker image push jaimezeng/hello-jaime:0.0.1推送镜像到 dockerhubdocker container run -it --name hello-jaime-demo jaimezeng/hello-jaime:0.0.1 python /hello.py运行容器并执行 python 程序后退出

Backlinks
Docker Notes
- 通过 commit 创建镜像 [[container-commit]]