Docker Notes
T

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 推送镜像到 dockerhub
  • docker 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 请求

docker comtainer commit

练习 - 使用 container commit 构建镜像执行 Python 程序

例子:在 [[dockerfile-intro#Dockerfile 基本结构]] 中 执行 Python 程序

  1. docker image pull ubuntu:22.04 拉取 ubuntu:22.04 镜像
  2. docker container run --name hello-jaime -it ubuntu:22.04 sh 进入容器内部
  3. apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip 安装依赖
  4. echo 'print("hello docker, hello jaime")' >hello.py 添加 hello.py 文件
  5. exit/ Ctrl + d 退出容器终端
  6. docker container commit hello-jaime jaimezeng/hello-jaime:0.0.1 打包镜像
  7. docker image push jaimezeng/hello-jaime:0.0.1 推送镜像到 dockerhub
  8. docker container run -it --name hello-jaime-demo jaimezeng/hello-jaime:0.0.1 python /hello.py 运行容器并执行 python 程序后退出

hello jaime demo

Show Graph Visualisation