Docker常见操作指令
本文最后更新于 2024-11-13,文章内容可能已经过时。
docker是一种开源的应用容器引擎,它通过操作系统层级的虚拟化技术,将软件及其依赖项打包为容器,从而简化应用程序的部署和管理。 Docker容器具有轻量级、高效的特点,能够在不同的主机之间无缝运行,并且具备一致的行为,因为它们包含了应用运行所需的所有依赖库、配置文件和服务。
指令 | 描述 |
---|---|
docker build | 根据Dockerfile构建镜像 |
docker run | 创建并启动容器 |
docker pull | 从仓库拉取或者更新指定的镜像 |
docker push | 将本地的镜像上传到仓库 |
docker images | 列出本地所有的镜像 |
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
2d429b9e73a6: Pull complete
9b1039c85176: Pull complete
9ad567d3b8a2: Pull complete
773c63cd62e4: Pull complete
1d2712910bdf: Pull complete
4b0adc47c460: Pull complete
171eebbdf235: Pull complete
Digest: sha256:bc5eac5eafc581aeda3008b4b1f07ebba230de2f27d47767129a6a905c84f470
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run -d -p 8080:80 --name TestContainer nginx
3722328f456ca98987864d75b41e727fddc359417e22cca7d3aa2930ef315890
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3722328f456c nginx "/docker-entrypoint.…" 6 seconds ago Up 6 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp TestContainer
[root@localhost ~]# docker exec -it 3722328f456c /bin/bash
root@3722328f456c:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@3722328f456c:/#
root@3722328f456c:/# cd /etc/nginx/
root@3722328f456c:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@3722328f456c:/etc/nginx# cd conf.d/
root@3722328f456c:/etc/nginx/conf.d# ls
default.conf
root@3722328f456c:/etc/nginx/conf.d#
root@3722328f456c:/etc/nginx/conf.d# exit
exit
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3722328f456c nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp TestContainer
[root@localhost ~]# docker stop TestContainer
TestContainer
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3722328f456c nginx "/docker-entrypoint.…" 6 minutes ago Exited (0) 10 seconds ago TestContainer
[root@localhost ~]# docker start TestContainer
TestContainer
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3722328f456c nginx "/docker-entrypoint.…" 7 minutes ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp TestContainer
[root@localhost ~]# docker logs TestContainer
/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/11/13 00:49:00 [notice] 1#1: using the "epoll" event method
2024/11/13 00:49:00 [notice] 1#1: nginx/1.27.2
2024/11/13 00:49:00 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/11/13 00:49:00 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_64
2024/11/13 00:49:00 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/11/13 00:49:00 [notice] 1#1: start worker processes
2024/11/13 00:49:00 [notice] 1#1: start worker process 29
2024/11/13 00:49:00 [notice] 1#1: start worker process 30
2024/11/13 00:49:00 [notice] 1#1: start worker process 31
2024/11/13 00:49:00 [notice] 1#1: start worker process 32
192.168.239.1 - - [13/Nov/2024:00:49:30 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36" "-"
192.168.239.1 - - [13/Nov/2024:00:49:31 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.239.149:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36" "-"
2024/11/13 00:49:31 [error] 32#32: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.239.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.239.149:8080", referrer: "http://192.168.239.149:8080/"
2024/11/13 00:55:45 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2024/11/13 00:55:45 [notice] 30#30: gracefully shutting down
2024/11/13 00:55:45 [notice] 31#31: gracefully shutting down
2024/11/13 00:55:45 [notice] 32#32: gracefully shutting down
2024/11/13 00:55:45 [notice] 30#30: exiting
2024/11/13 00:55:45 [notice] 31#31: exiting
2024/11/13 00:55:45 [notice] 32#32: exiting
2024/11/13 00:55:45 [notice] 30#30: exit
2024/11/13 00:55:45 [notice] 32#32: exit
2024/11/13 00:55:45 [notice] 31#31: exit
2024/11/13 00:55:45 [notice] 29#29: gracefully shutting down
2024/11/13 00:55:45 [notice] 29#29: exiting
2024/11/13 00:55:45 [notice] 29#29: exit
2024/11/13 00:55:45 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2024/11/13 00:55:45 [notice] 1#1: worker process 29 exited with code 0
2024/11/13 00:55:45 [notice] 1#1: worker process 30 exited with code 0
2024/11/13 00:55:45 [notice] 1#1: signal 29 (SIGIO) received
2024/11/13 00:55:45 [notice] 1#1: signal 17 (SIGCHLD) received from 32
2024/11/13 00:55:45 [notice] 1#1: worker process 32 exited with code 0
2024/11/13 00:55:45 [notice] 1#1: signal 29 (SIGIO) received
2024/11/13 00:55:45 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2024/11/13 00:55:45 [notice] 1#1: worker process 31 exited with code 0
2024/11/13 00:55:45 [notice] 1#1: exit
/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: IPv6 listen already enabled
/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/11/13 00:56:18 [notice] 1#1: using the "epoll" event method
2024/11/13 00:56:18 [notice] 1#1: nginx/1.27.2
2024/11/13 00:56:18 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/11/13 00:56:18 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_64
2024/11/13 00:56:18 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/11/13 00:56:18 [notice] 1#1: start worker processes
2024/11/13 00:56:18 [notice] 1#1: start worker process 22
2024/11/13 00:56:18 [notice] 1#1: start worker process 23
2024/11/13 00:56:18 [notice] 1#1: start worker process 24
2024/11/13 00:56:18 [notice] 1#1: start worker process 25
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3722328f456c nginx "/docker-entrypoint.…" 10 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp TestContainer
[root@localhost ~]# docker rm -f $(docker ps -aq)
3722328f456c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 60c8a892f36f 5 weeks ago 192MB
[root@localhost ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
2d429b9e73a6: Already exists
92ef1eccbb9f: Pull complete
5e00ad97561c: Pull complete
8f865c3d417c: Pull complete
74c736b00471: Pull complete
928f5dbb5007: Pull complete
4f4fb700ef54: Pull complete
6fd0c1bf3b91: Pull complete
Digest: sha256:af0be38eb8e43191bae9b03fe5c928803930b6f93e2dde3a7ad1165c04b1ce22
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 6c199afc1dae 5 weeks ago 117MB
nginx latest 60c8a892f36f 5 weeks ago 192MB
[root@localhost ~]# docker image prune -a --force
Deleted Images:
untagged: redis:latest
untagged: redis@sha256:af0be38eb8e43191bae9b03fe5c928803930b6f93e2dde3a7ad1165c04b1ce22
deleted: sha256:6c199afc1dae9c01ae5347c390d379912bb9d56ab37516ec8ad3fb6c4aebbbdd
deleted: sha256:904585ae297e947f9cdcb7100c00fa7327c377687312a1f05cd0e374cc00cc17
deleted: sha256:9365e8db6ea6fd1da5cf06ba6ad47cfcc3fd0287a23a15e5dc3f3b5e40d84d5c
deleted: sha256:16a580d05ae1cb098fbf1d902a7e1c21337d8da49769d04ee6b2c7abaefe5a1d
deleted: sha256:8ecd43a538010bc300ef5267cdb0e41599ff64a08473283e796744a5eec45798
deleted: sha256:90aeb89244941f32cc144d68d121e90ca408d7ba0ce5ba24442b3b05efb5053c
deleted: sha256:d26c67d379ce1fd1574d70740fb7f11f2da7ec133f9196fc79f0d522dc35b862
deleted: sha256:03f1b1a8eade9de3773ba90bf4b321ea0da6ae11e70b91ee2315dc696b92bcca
untagged: nginx:latest
untagged: nginx@sha256:bc5eac5eafc581aeda3008b4b1f07ebba230de2f27d47767129a6a905c84f470
deleted: sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876
deleted: sha256:47984982982b32672d3b0cc6ebc1016e70916a8347c79765dc2ba09ed9afc97c
deleted: sha256:f8fffef24ebb396c3e1721168923665f594d6b0ec1270700f642155fb51179cb
deleted: sha256:ceff183e9da02c76af52712096cbe7e26e01909f827f18141058afbf4f7e32db
deleted: sha256:01c22c5216c94ae4a6285e21b0ccb6bb786d437aa7eb7d3e2de8a454115d17a8
deleted: sha256:9a980991ece0116dad7650d5af48faa2f693f9277bfd99f4fb3c8c2ce0b4e27d
deleted: sha256:d775439dbfb804d168b7ab8501c32013896d40d66b14944d2429778d995c7fe4
deleted: sha256:c3548211b8264f8bfa47a6727043a64f1791b82ac965a284a7ea187e971a95e2
Total reclaimed space: 233.9MB
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]#
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 软件从业者Hort
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果