Docker:镜像操作 补充一
1. 实验
启动一个nginx,并将它的首页改为自己的页面,发布出去,让所有人都能用
1.1 搜索、下载、删除镜像
// 搜索nginx,标注OFFICIAL是官方的
(base) ➜ ~ docker search nginx
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 20440 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 97
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 44
nginx/unit This repository is retired, use the Docker o… 64
nginx/nginx-ingress-operator NGINX Ingress Operator for NGINX and NGINX P… 2
nginx/nginx-quic-qns NGINX QUIC interop 1
nginx/unit-preview Unit preview features 0
nginx/nginxaas-loadbalancer-kubernetes 0
bitnami/nginx Bitnami container image for NGINX 194
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 123
rancher/nginx 2
bitnamicharts/nginx Bitnami Helm chart for NGINX Open Source 0
kasmweb/nginx An Nginx image based off nginx:alpine and in… 8
rapidfort/nginx RapidFort optimized, hardened image for NGINX 15
redash/nginx Pre-configured nginx to proxy linked contain… 3
paketobuildpacks/nginx 0
vmware/nginx 2
chainguard/nginx Build, ship and run secure software with Cha… 4
droidwiki/nginx 0
intel/nginx 0
gluufederation/nginx A customized NGINX image containing a consu… 1
circleci/nginx This image is for internal use 2
jitesoft/nginx Nginx on alpine linux 0
corpusops/nginx https://github.com/corpusops/docker-images/ 1
antrea/nginx Nginx server used for Antrea e2e testing 0
// 拉取镜像
(base) ➜ ~ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bb3f2b52e6af: Pull complete
e4bc5c1a6721: Pull complete
e93f7200eab8: Pull complete
1bd52ec2c0cb: Pull complete
411a98463f95: Pull complete
ad5932596f78: Pull complete
df25b2e5edb3: Pull complete
Digest: sha256:fb197595ebe76b9c0c14ab68159fd3c08bd067ec62300583543f0ebda353b5be
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
What's next:
View a summary of image vulnerabilities and recommendations → docker scout quickview nginx
// 查看本地镜像
(base) ➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bdf62fd3a32f 2 weeks ago 197MB
weygu/nebulagraph-jupyter v0 621540960f3a 4 months ago 1.74GB
weygu/nebulagraph-gephi-exchange v0.1 46adca927168 6 months ago 643MB
weygu/nebulagraph-dd-ext 0.4.21 019e494cfd0f 6 months ago 9.17MB
vesoft/nebula-metad v3.8.0 ed1af2e7d732 7 months ago 382MB
vesoft/nebula-storaged v3.8.0 5dc54c63c7ab 7 months ago 383MB
vesoft/nebula-graphd v3.8.0 7738d42b9445 7 months ago 377MB
vesoft/nebula-graph-studio v3 ef5c7a587b3f 7 months ago 85.2MB
docker/welcome-to-docker latest 648f93a1ba7d 13 months ago 19MB
weygu/nebula-webconsole latest 457e03b2bf04 15 months ago 34.2MB
// 下载指定版本的nginx
(base) ➜ ~ docker pull nginx:1.26.0
1.26.0: Pulling from library/nginx
24c63b8dcb66: Pull complete
e1de6ca4e153: Pull complete
77f9c49c64aa: Pull complete
99f52b91c1b3: Pull complete
66f3b31fa55b: Pull complete
b046e9e37505: Pull complete
8468a6b663f0: Pull complete
Digest: sha256:192e88a0053c178683ca139b9d9a2afb0ad986d171fae491949fe10970dd9da9
Status: Downloaded newer image for nginx:1.26.0
docker.io/library/nginx:1.26.0
What's next:
View a summary of image vulnerabilities and recommendations → docker scout quickview nginx:1.26.0
(base) ➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bdf62fd3a32f 2 weeks ago 197MB
weygu/nebulagraph-jupyter v0 621540960f3a 4 months ago 1.74GB
weygu/nebulagraph-gephi-exchange v0.1 46adca927168 6 months ago 643MB
weygu/nebulagraph-dd-ext 0.4.21 019e494cfd0f 6 months ago 9.17MB
vesoft/nebula-metad v3.8.0 ed1af2e7d732 7 months ago 382MB
vesoft/nebula-storaged v3.8.0 5dc54c63c7ab 7 months ago 383MB
vesoft/nebula-graphd v3.8.0 7738d42b9445 7 months ago 377MB
vesoft/nebula-graph-studio v3 ef5c7a587b3f 7 months ago 85.2MB
nginx 1.26.0 561afcc2621f 7 months ago 193MB
docker/welcome-to-docker latest 648f93a1ba7d 13 months ago 19MB
weygu/nebula-webconsole latest 457e03b2bf04 15 months ago 34.2MB
// 根据IMAGE ID删除镜像
(base) ➜ ~ docker rmi bdf62fd3a32f
Untagged: nginx:latest
Untagged: nginx@sha256:fb197595ebe76b9c0c14ab68159fd3c08bd067ec62300583543f0ebda353b5be
Deleted: sha256:bdf62fd3a32f1209270ede068b6e08450dfe125c79b1a8ba8f5685090023bf7f
Deleted: sha256:bf74a794f54dda4a120c2341b9e3eecafc19ab423649909edf66090bcab8a007
Deleted: sha256:d8ab2f8a77485ff82666c0b49a3f098e35643462313f391abe77c2ef0fdcfcfe
Deleted: sha256:c95a6c3fcab6af748cbf95a166ab17d1d00a87fe03b47181f72996b419da5693
Deleted: sha256:ad440851c8e2f92041d499b40dd0aa17fa33d9c8b2e1eec66581fe275b53bc32
Deleted: sha256:1555b493613f3dc11dac1ea991d968c2e068627ba42b33cf8b86702c15f78ff2
Deleted: sha256:bdec05ec0f48beadbd288522556f5650baea79e24b9ca9df5424d06c5a328a8f
Deleted: sha256:3e620c160447d1acff162610a533282fc64863123cba28ce40eaf98c17dde780
1.2 启动、停止容器、查看应用占用、日志
(base) ➜ ~ docker run nginx:1.26.0
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/12/16 14:58:02 [notice] 1#1: using the "epoll" event method
2024/12/16 14:58:02 [notice] 1#1: nginx/1.26.0
2024/12/16 14:58:02 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/12/16 14:58:02 [notice] 1#1: OS: Linux 6.10.14-linuxkit
2024/12/16 14:58:02 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/12/16 14:58:02 [notice] 1#1: start worker processes
2024/12/16 14:58:02 [notice] 1#1: start worker process 29
2024/12/16 14:58:02 [notice] 1#1: start worker process 30
2024/12/16 14:58:02 [notice] 1#1: start worker process 31
2024/12/16 14:58:02 [notice] 1#1: start worker process 32
2024/12/16 14:58:02 [notice] 1#1: start worker process 33
2024/12/16 14:58:02 [notice] 1#1: start worker process 34
2024/12/16 14:58:02 [notice] 1#1: start worker process 35
2024/12/16 14:58:02 [notice] 1#1: start worker process 36
2024/12/16 14:58:02 [notice] 1#1: start worker process 37
2024/12/16 14:58:02 [notice] 1#1: start worker process 38
2024/12/16 14:58:02 [notice] 1#1: start worker process 39
2024/12/16 14:58:02 [notice] 1#1: start worker process 40
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7b8cbccf64c nginx:1.26.0 "/docker-entrypoint.…" 22 seconds ago Up 21 seconds 80/tcp zealous_euler
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:8088->80/tcp welcome-to-docker
// docker ps 查看的是所有的正在运行容器,如果要是看全部的,包括停止的 docker ps -a
(base) ➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7b8cbccf64c nginx:1.26.0 "/docker-entrypoint.…" 5 minutes ago Exited (0) 2 seconds ago zealous_euler
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:8088->80/tcp welcome-to-docker
服务的停止
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca16b2e7880b nginx:1.26.0 "/docker-entrypoint.…" 28 seconds ago Up 27 seconds 80/tcp nostalgic_montalcini
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:8088->80/tcp welcome-to-docker
// 通过docker的NAMES来停止
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab0201efd1e8 nginx:1.26.0 "/docker-entrypoint.…" 3 seconds ago Up 3 seconds 80/tcp naughty_mayer
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:8088->80/tcp welcome-to-docker
(base) ➜ ~ docker stop naughty_mayer
naughty_mayer
// 通过docker的CONTAINER ID来停止
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca16b2e7880b nginx:1.26.0 "/docker-entrypoint.…" 28 seconds ago Up 27 seconds 80/tcp nostalgic_montalcini
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:8088->80/tcp welcome-to-docker
(base) ➜ ~ docker stop ca16b2e7880b
ca16b2e7880b
查看应用的占用情况
(base) ➜ ~ docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
5425400480ac welcome-to-docker 0.00% 9.594MiB / 7.653GiB 0.12% 5.86kB / 402kB 0B / 12.3kB 13
// 通过CONTAINER ID前两位
(base) ➜ ~ docker stats 542
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
5425400480ac welcome-to-docker 0.00% 9.594MiB / 7.653GiB 0.12% 5.86kB / 402kB 0B / 12.3kB 13
日志
(base) ➜ ~ docker logs 542
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/12/16 11:54:24 [notice] 1#1: using the "epoll" event method
2024/12/16 11:54:24 [notice] 1#1: nginx/1.25.3
2024/12/16 11:54:24 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r10)
2024/12/16 11:54:24 [notice] 1#1: OS: Linux 6.10.14-linuxkit
2024/12/16 11:54:24 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/12/16 11:54:24 [notice] 1#1: start worker processes
2024/12/16 11:54:24 [notice] 1#1: start worker process 30
2024/12/16 11:54:24 [notice] 1#1: start worker process 31
2024/12/16 11:54:24 [notice] 1#1: start worker process 32
2024/12/16 11:54:24 [notice] 1#1: start worker process 33
2024/12/16 11:54:24 [notice] 1#1: start worker process 34
2024/12/16 11:54:24 [notice] 1#1: start worker process 35
2024/12/16 11:54:24 [notice] 1#1: start worker process 36
2024/12/16 11:54:24 [notice] 1#1: start worker process 37
2024/12/16 11:54:24 [notice] 1#1: start worker process 38
2024/12/16 11:54:24 [notice] 1#1: start worker process 39
2024/12/16 11:54:24 [notice] 1#1: start worker process 40
2024/12/16 11:54:24 [notice] 1#1: start worker process 41
172.17.0.1 - - [16/Dec/2024:14:59:01 +0000] "GET / HTTP/1.1" 200 651 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [16/Dec/2024:14:59:01 +0000] "GET /static/js/main.c9e951e4.js HTTP/1.1" 200 382506 "http://0.0.0.0:8088/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [16/Dec/2024:14:59:01 +0000] "GET /static/css/main.27312bf9.css HTTP/1.1" 200 791 "http://0.0.0.0:8088/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [16/Dec/2024:14:59:03 +0000] "GET /favicon.ico HTTP/1.1" 200 15086 "http://0.0.0.0:8088/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [16/Dec/2024:14:59:09 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
1.3 后台启动docker
(base) ➜ ~ docker run -d --name mynginx nginx:1.26.0
其中 -d 表示后台启动
-name 给容器起一个名字
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a076f368108 nginx:1.26.0 "/docker-entrypoint.…" 29 seconds ago Up 28 seconds 80/tcp mynginx
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8088->80/tcp welcome-to-docker
// 重启nginx
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a076f368108 nginx:1.26.0 "/docker-entrypoint.…" 29 seconds ago Up 28 seconds 80/tcp mynginx
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8088->80/tcp welcome-to-docker
(base) ➜ ~ docker restart mynginx
mynginx
(base) ➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a076f368108 nginx:1.26.0 "/docker-entrypoint.…" About a minute ago Up 3 seconds 80/tcp mynginx
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8088->80/tcp welcome-to-docker
删除容器
(base) ➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a076f368108 nginx:1.26.0 "/docker-entrypoint.…" 7 minutes ago Exited (0) About a minute ago mynginx
ab0201efd1e8 nginx:1.26.0 "/docker-entrypoint.…" 25 minutes ago Exited (0) 25 minutes ago naughty_mayer
ca16b2e7880b nginx:1.26.0 "/docker-entrypoint.…" 26 minutes ago Exited (0) 25 minutes ago nostalgic_montalcini
c7b8cbccf64c nginx:1.26.0 "/docker-entrypoint.…" 40 minutes ago Exited (0) 35 minutes ago zealous_euler
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8088->80/tcp welcome-to-docker
(base) ➜ ~ docker rm mynginx
mynginx
(base) ➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab0201efd1e8 nginx:1.26.0 "/docker-entrypoint.…" 27 minutes ago Exited (0) 27 minutes ago naughty_mayer
ca16b2e7880b nginx:1.26.0 "/docker-entrypoint.…" 29 minutes ago Exited (0) 28 minutes ago nostalgic_montalcini
c7b8cbccf64c nginx:1.26.0 "/docker-entrypoint.…" 43 minutes ago Exited (0) 37 minutes ago zealous_euler
5425400480ac docker/welcome-to-docker:latest "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:8088->80/tcp welcome-to-docker
(base) ➜ ~ docker run -d --name nginx001 -p 9797:80 nginx:1.26.0
04eae9666335f3ea103552ff0ee70095fdc6e78ecc5df9be9856a1f139009270
9797是外面的机器的端口
80是容器的端口
1.4 进入docker内部操作
(base) ➜ ~ docker exec -it nginx001 /bin/bash
-it :是使用交互模式
/bin/bash :这是在容器内部执行的命令。/bin/bash 是bash shell的路径,这意味着该命令将在容器内打开一个bash会话,允许用户以交互式方式执行进一步的命令。
root@04eae9666335:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib media mnt opt proc root run sbin srv sys tmp usr var
root@04eae9666335:/# cd /usr/share/nginx/html/
root@04eae9666335:/usr/share/nginx/html# ls
root@04eae9666335:/usr/share/nginx/html# echo "<h1>Hello,Docker</h1>" > index.html
1.5 保存镜像
(base) ➜ ~ docker commit -a "fanzhen" -m "update index.html" nginx001 nginx:fan
sha256:1e93aef8c8b4b2733c63afae6866df48fdda499086c063a75316f95661f5a79a
// 把nginx镜像文件保存为一个jar包
(base) ➜ ~ docker save -o mynginx.jar nginx:fan
(base) ➜ ~ ls
13371746609862949.png Movies arthas-output logs nfturbo-gateway
Applications Music as.sh math-game.jar nfturbo-order
CleanCode_2024_08_05_143650.jfr Pictures dump.rdb mynginx.jar nfturbo-trade
Desktop Public fanzhen nacos node_modules
Documents Templates fanzhen.pub nfturbo-admin nohup.out
Downloads Virtual Machines.localized java_error_in_idea.hprof nfturbo-auth package-lock.json
Library arthas-boot.jar java_error_in_webstorm.hprof nfturbo-business store
命令解释
docker commit:这是用来从容器创建镜像的Docker命令。
-a "fanzhen"
:这个选项用于指定作者的名称,用于在镜像的元数据中记录制作这个镜像的人的信息。这里 -a 是 --author 的缩写,“fanzhen” 是作者名。-m "update index.html"
:这个选项用于添加提交信息,类似于版本控制系统中的提交消息。这里 -m 是 --message 的缩写,“update index.html” 是描述这次镜像创建操作或容器状态变更的信息。nginx001
:这是要提交成新镜像的容器的名称。该容器是当前正在运行的状态,其内部可能已经进行了一些修改(如文件更新、配置变更等)。nginx:fan
:这是新创建的镜像的名称和标签。这里 nginx 是镜像的名称,fan 是这个镜像的标签。标签用于区分同一个镜像的不同版本或变体。
把这个jar包传给别人就可以使用了
docker load -i xxx
1.6 分享到社区
第一步:登录
第二步:文件改名
- 首先通过
docker images
查询到所有的镜像 - 原来的镜像通过
docker tag 原镜像REPOSITORY:TAG 你的用户名/新的REPOSITORY:TAG
然后推送到docker
docker push xxxxx/xxx:xx
最后就能看到了