一、Docker基本命令
1、查看docker版本
docker version && docker info
[root@localhost ~]
Client: Docker Engine - Community
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:45:41 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:44:05 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@localhost ~]
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
scan: Docker Scan (Docker Inc., v0.12.0)
Server:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 2
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.686GiB
Name: localhost.localdomain
ID: BXSB:URMW:4X4Q:YWTU:XCLJ:P6OG:CCIM:S4Q2:ZSB4:FWHB:QV64:J2TS
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://y9hsd18p.mirror.aliyuncs.com/
Live Restore Enabled: false
2、搜索镜像和dockerhub
docker search 镜像名称
[root@localhost ~]
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16136 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2108 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 820 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 312
linuxserver/nginx An Nginx container, brought to you by LinuxS… 160
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 151 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 150 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 114 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 82 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 61 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 59
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 57
nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 34
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 25 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 23
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 16
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 13 [OK]
bitwarden/nginx The Bitwarden nginx web server acting as a r… 12
mailu/nginx Mailu nginx frontend 10 [OK]
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 3 [OK]
wodby/nginx Generic nginx 1 [OK]
arnau/nginx-gate Docker image with Nginx with Lua enabled on … 1 [OK]
3、下载镜像
docker pull 镜像名称
默认是从docker hub上
[root@localhost ~]
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
4、docker run
docker run 镜像名称
run —运行
①检测本地有没有该镜像(没有的话直接到docker hub上下载)
②create(将镜像创建为容器)+ start 将创建好的容器运行起来
[root@localhost ~]
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
工作流程
①docker client客户端连接到了服务端(服务端是以一个守护进程的形式跑在操作系统里面的) restful api典型的c/s架构 ②由docker服务端的守护进程从docker hub 上下载了镜像(Ps:服务端会先检查本地系统是否有这个镜像) ③服务端创建了一个新的容器,然后从拉去的这个镜像启动了一个容器,容器执行了脚本/可执行程序让我们可以查看/使用(client) ④docker 服务端把这些信息流(传递)返回到客户端并展示出来,(展示在终端上) docker client可以是多种形式,比如"docker"命令工具所在的终端
5、查看所有状态容器
docker ps -a
-a:显示所有容器
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d611f425588 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago beautiful_sanderson
1c165bc5beaa b0f53fa23af6 "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago zen_wright
b66ea884146f b66755a4ef1c "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago gracious_keldysh
c08cd97f28fe f7720be4ebf1 "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago gallant_shamir
43f5d77173c5 f7720be4ebf1 "/bin/sh -c 'cmake -…" 2 days ago Exited (1) 2 days ago wonderful_banzai
5f38c8aec4aa f7720be4ebf1 "/bin/sh -c './cmake…" 2 days ago Exited (126) 2 days ago gifted_lewin
注:
字段说明
CONTAINER ID:容器的ID号
IMAGE:加载的镜像
COMMAND :运行的程序
CREATED :创建时间
STATUS:当前的状态
PORTS:端口映射
NAMES:名称
二、docker镜像操作
1、查看镜像列表
docker images—》查看当前docker下的下载镜像信息
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b0f53fa23af6 2 days ago 784MB
<none> <none> b66755a4ef1c 2 days ago 784MB
<none> <none> f7720be4ebf1 2 days ago 784MB
nginx latest 605c77e624dd 2 weeks ago 141MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[root@localhost ~]
2、获取镜像信息
docker inspect 镜像ID
[root@localhost ~]
3、添加镜像标签
docker tag 旧标签 新标签
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b0f53fa23af6 2 days ago 784MB
<none> <none> b66755a4ef1c 2 days ago 784MB
<none> <none> f7720be4ebf1 2 days ago 784MB
nginx latest 605c77e624dd 2 weeks ago 141MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[root@localhost ~]
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b0f53fa23af6 2 days ago 784MB
<none> <none> b66755a4ef1c 2 days ago 784MB
<none> <none> f7720be4ebf1 2 days ago 784MB
nginx latest 605c77e624dd 2 weeks ago 141MB
hello-world ding feb5d9fea6a5 3 months ago 13.3kB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos 7 eeb6ee3f44bd 4 months ago 204MB
4、删除镜像
docker rmi 镜像名称 :当一个镜像有多个标签时,只是删除其中指定的标签
docker rmi 镜像ID:会彻底删除该镜像
批量删除:docker rmi ``docker images -q
注:
rmi 是 rm image 的简写
不能删除运行中的容器,想要删除运行中的容器的加:-f
-f:强制删除
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
[root@localhost ~]
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
[root@localhost ~]
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
5、镜像导出与镜像导入
导出:docker save -o 文件名 镜像名
导入:docker load < 导出的文件
[root@localhost ~]
[root@localhost ~]
anaconda-ks.cfg nginx_1.21 模板 图片 下载 桌面
initial-setup-ks.cfg 公共 视频 文档 音乐
[root@localhost ~]
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[root@localhost ~]
anaconda-ks.cfg nginx_1.21 模板 图片 下载 桌面
initial-setup-ks.cfg 公共 视频 文档 音乐
[root@localhost ~]
2edcec3590a4: Loading layer 83.86MB/83.86MB
e379e8aedd4d: Loading layer 62MB/62MB
b8d6e692a25e: Loading layer 3.072kB/3.072kB
f1db227348d0: Loading layer 4.096kB/4.096kB
32ce5f6a5106: Loading layer 3.584kB/3.584kB
d874fd2bc83b: Loading layer 7.168kB/7.168kB
Loaded image: nginx:latest
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos 7 eeb6ee3f44bd 4 months ago 204MB
三、docker容器操作
1、查询容器
docker ps -a—查询容器的所有状态
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
461b9ca23242 nginx:latest "/docker-entrypoint.…" 27 seconds ago Exited (0) 2 seconds ago nostalgic_sinoussi
2、创建容器
docker create 选项 镜像名称 /bin/bash
注:/bin/bash–给予一个终端环境支持
选项:-i–让容器的输入保持打开 -t–分配一个伪终端 -d–后台守护进程的方式运行
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[root@localhost ~]
f47cceadb2c027f86af1387399f070aa33761507ec2cef6b9292cfb7f2d1bbf5
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 7 seconds ago Created inspiring_sammet
3、启动容器
docker start 容器ID
[root@localhost ~]
f47cceadb2c0
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 seconds 80/tcp inspiring_sammet
CONTAINER ID:容器ID
IMAGE :容器使用的镜像
COMMAND :命令
CREATED :创建的时间/已经创建的时间
STATUS:目前的专状态
PORTS :容器内部暴露的端口
NAMES:容器本身的的名称
创建并启动容器:docker run -d 镜像名称 /usr/bin/bash -c ls / (一次性执行)
**注:**run包含了create与start
[root@localhost ~]
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60b518e6723c centos:7 "/usr/bin/bash -c ls…" 18 seconds ago Exited (0) 17 seconds ago wonderful_banzai
f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 15 minutes ago Up 10 minutes 80/tcp inspiring_sammet
4、停止容器
docker stop 容器ID
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 18 minutes ago Up 13 minutes 80/tcp inspiring_sammet
[root@localhost ~]
f47cceadb2c0
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f47cceadb2c0 nginx:latest "/docker-entrypoint.…" 18 minutes ago Exited (137) 3 seconds ago inspiring_sammet
5、持续后台运行
执行后不退出,以守护进程方式执行持续性任务
docker run -it 镜像名字 /bin/bash -c “while true;do echo hello;done”
“while true;do echo hello;done” --赋予一个字循环
[root@localhost ~]
ae3edb4f9876dfa3fb84a78c86236f2b132a3b68e8804276e1a260a3ae98cfda
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae3edb4f9876 centos:7 "/bin/bash -c 'while…" 9 seconds ago Up 8 seconds loving_tereshkova
669a0bfb5290 centos:7 "/usr/bin/bash -c ls…" 2 minutes ago Exited (0) 2 minutes ago reverent_borg
d79bdefa9ad5 nginx:latest "/docker-entrypoint.…" 3 minutes ago Exited (137) 2 minutes ago loving_lamarr
6、进入容器
6.1run
run–一次性进入
docker run -it 镜像名称 /bin/bash
6.2exec
exec–永久性进入
docker exec -it 容器ID /bin/bash
**注:**容器必须为开启状态
Ps:
docker run -it会创建前台进程,但是会在输入exit后终止进程,
docker attach 会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止容器进程.
docker exec -it 会连接到容器,可以像sSH一样进入容器内部,进行操作,可以通过exit退出容器,不影响容器运行。
7、容器导出与容器导入
7.1容器导出
docker export 容器ID > 文件名
[root@localhost ~]
[root@localhost ~]
anaconda-ks.cfg centos_7 initial-setup-ks.cfg 公共 模板 视频 图片 文档
7.2容器导入
docker inport 容器ID > 导出的文件名(容器)指定镜像名称
[root@localhost ~]
anaconda-ks.cfg centos_7 initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]
sha256:5b6c2cdb58d692112fb592d80e976816bb407fe6c9a059cb6a9b749c64cfa884
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
centos lnmp 5b6c2cdb58d6 12 seconds ago 204MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
8、批量删除容器
docker rm 选项
1.
docker rm 3224eb044879
2.
docker rm 1270a6791069
3.
docker rm -f distracted_panini
4.
docker rm -f `docker ps -q`
5.
docker rm -f `docker ps -aq`
6.
docker ps -a l awk ' {print "docker rm "$1}'l bash
7.
for i in `dockef ps -a l grep -i exit / awk '{print $1}' '; do docker rm -f $i;done
5b6c2cdb58d6 12 seconds ago 204MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
### 8、批量删除容器
docker rm 选项
```bash
1.#不能删除运行状态的容器,只能-f强制删除,或者先停止再删除
docker rm 3224eb044879
2.#已经退出的容器,可以直接删除
docker rm 1270a6791069
3.#基于名称匹配的方式删除
docker rm -f distracted_panini
4.#删除所有运行状态的容器
docker rm -f `docker ps -q`
5.#删除所有容器
docker rm -f `docker ps -aq`
6.#有选择性的批量删除 (正则匹配)
docker ps -a l awk ' {print "docker rm "$1}'l bash
7.#删除退出状态的容器
for i in `dockef ps -a l grep -i exit / awk '{print $1}' '; do docker rm -f $i;done
|