docker学习笔记详记
常见docker命令列表
attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像
build Build an image from a Dockerfile # 通过 Dockerfile 定制镜像
commit Create a new image from a container changes # 提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path # 从容器中拷贝指定文件或者目录到宿主机中
create Create a new container # 创建一个新的容器,同 run,但不启动容器
diff Inspect changes on a container's filesystem # 查看 docker 容器变化
events Get real time events from the server # 从 docker 服务获取容器实时事件
exec Run a command in an existing container # 在已存在的容器上运行命令
export Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件 [对应 import]
history Show the history of an image # 展示一个镜像形成历史
images List images # 列出系统当前镜像
import Create a new filesystem image from the contents of a tarball # 从 tar 包中的内容创建一个新的文件系统映像 [对应 export]
info Display system-wide information # 显示系统相关信息
inspect Return low-level information on a container # 查看容器详细信息
kill Kill a running container # kill 一个指定 docker 容器
load Load an image from a tar archive # 从一个 tar 包中加载一个镜像 [对应 save]
login Register or Login to the docker registry server # 注册或者登陆一个 docker 资源服务器
logout Log out from a docker registry server # 从当前 Docker registry 退出
logs Fetch the logs of a container # 输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口
pause Pause all processes within a container # 暂停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 从 docker 镜像源服务器拉取指定镜像或者仓库镜像
push Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至 docker 源服务器
restart Restart a running container # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或多个镜像 [无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container # 创建一个新的容器并运行一个命令
save Save an image to a tar archive # 保存一个镜像为一个 tar 包 [对应 load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索镜像
start Start a stopped containers # 启动容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 给源中镜像打标签
top Lookup the running processes of a container # 查看容器中运行的进程信息
unpause Unpause a paused container # 取消暂停容器
version Show the docker version information # 查看 docker 版本号
wait Block until a container stops, then print its exit code # 截取容器停止时的退出状态值
docker volume ls #查看所有卷的情况
容器与镜像
1. 定义
- 镜像(Image)
-
- 就像一个模板,里面包含了操作系统环境、应用程序、依赖库等。
- 镜像是只读的。
- 类似于 操作系统 ISO 文件,你不能直接在 ISO 上操作,只能拿它来安装/运行系统。
- 容器(Container)
-
- 是基于镜像启动的一个运行实例。
- 容器在镜像的基础上,加了一层 可写层,你的修改、运行数据就会写在这一层。
- 类似于你装好系统后真正运行的那台虚拟机。
2. 关系
镜像类似手机APP的安装包,容器类似安装过的APP
- 镜像 👉 就像 程序的类(class)
- 容器 👉 就像 类创建出来的对象(instance)
-
- 一个镜像可以创建多个容器。
- 删除容器不会影响镜像,删除镜像会影响容器。
查看 Docker 服务状态:
sudo systemctl status docker
确保 Docker 服务处于 active (running)
状态。
若 Docker 服务运行正常,但仍不确定镜像源配置,可以尝试重新加载 Docker 守护进程配置并重启 Docker 服务,然后再通过上述 docker info
或查看配置文件的方式检查,命令如下:
sudo systemctl daemon-reload
sudo systemctl restart docker
查看配置文件信息
cat /etc/docker/daemon.json
验证镜源是
docker info | grep Registry
确认镜像源地址已正确显示后,再尝试拉取 hello-world
镜像测试:
docker run hello-world
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!
DockerServer接受到Docker-Client的指令,就会执行这个命令。
Docker为什么比VM快?
- Docker有着比VM更少的抽象层,
- Docker用的是宿主机的 内核。VM用的Guest OS
所以说,新建容器时候,Docker不需要像虚拟机那样,重新加载一个虚拟机的内核,避免引导,虚拟机的加载Guest OS,分钟级别,而Docker是利用宿主机的操作系统,省略这个复杂的过程,秒级。
Docker常用命令
帮助命令
docker --version #Docker的版本信息
docker info #详细信息 Docker的系统信息,包括镜像和容器的数量
docker 命令 --help
镜像命令
查看镜像
docker images #查看所有本地的主机上的镜像
root@Ubuntu:/etc/docker# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 1b44b5a3e06a 7 weeks ago 10.1kB
#解释
REPOSITORY 镜像仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像大小
#可选项
-a 列出所有镜像
-q 只显示镜像id
-f -a, --all Show all images (default hides intermediate images)
-q, --quiet Only show image IDs
docker search 搜索镜像
docker search [名称] #可搜索可下载版本
docker pull 下载镜像
docker pull 镜像名[:tag]root@Ubuntu:~# docker pull mysql
Using default tag: latest #默认下载最新版本
latest: Pulling from library/mysql
806f49275cbf: Pull complete #分层下载,docker iamge 的核心 联合文件系统
872c71b4bef6: Pull complete
930d9dafee77: Pull complete
d9301ae294fe: Pull complete
1620875f220d: Pull complete
6457b1401253: Pull complete
4c8e7ce47d40: Pull complete
e522a8b4b86f: Pull complete
e81ddb8dbdb4: Pull complete
619a14803e82: Pull complete
Digest: sha256:91447968e66961302339ec4dc4d385f5e1a957d98e63c7d52ecf8b1de0907346 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实系统两命令等价
docker pull mysql
docker.io/library/mysql:latest#指定版本下载
root@Ubuntu:~# docker pull mysql:5.7
5.7: Pulling from library/mysql
20e4dcae4c69: Pull complete
1c56c3d4ce74: Pull complete
e9f03a1c24ce: Pull complete
68c3898c2015: Pull complete
6b95a940e7b6: Pull complete
90986bb8de6e: Pull complete
ae71319cb779: Pull complete
ffc89e9dfd88: Pull complete
43d05e938198: Pull complete
064b2d298fba: Pull complete
df9a4d85569b: Pull complete
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7#在联合文件系统中,如果系统中已经有要下载文件,会显示Already exists,不用下载。
删除镜像
docker rmi -f #删除全部镜像
一般根据id删除镜像
root@Ubuntu:~# docker rmi -f 5107333e08a8
Untagged: mysql:5.7
Untagged: mysql@sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Deleted: sha256:5107333e08a87b836d48ff7528b1e84b9c86781cc9f1748bbc1b8c42a870d933
Deleted: sha256:37fd5f1492d4e9cb540c52c26655f220568050438f804275e886200c8807ffb4
Deleted: sha256:1105a50d3483cb9f970e70cf5163e3352f0b2fe2ff07c6abcca6f34228e76dc5
Deleted: sha256:94187496c18bb11b78e71017f2774ad3c0a734da9749a46e917c4239504e9322
Deleted: sha256:ae59716eae3be604a4fd43e86fd2ad504cb06c89cc064c73c78eee651e675805
Deleted: sha256:97d26ca29ec287ff4bd09a49602c44cbcabcf3303ddc726b3b94cbe26dfe1c94
Deleted: sha256:27303974d12144264b32b8936ca7c90d72bdba939a9e791010201b3b1717c4c4
Deleted: sha256:4d4483f06dbe01282c10cb9e429a0be826c18c61048f7860dad49ae7f6bac927
Deleted: sha256:3b73a6f6b3298c568dcfb8fa5e96c581a1b5c0ad395b0c38f9addd0c79703124
Deleted: sha256:46446bf265a411a4a13a4adc86f60c9e0479a2e03273c98cafab7bc4151dd2bc
Deleted: sha256:1d5264146b09a27a8fc6801dc239a4962582ed27dd2fbd8ee708463a1857b06b
Deleted: sha256:cff044e186247f93aa52554c96d77143cc92f99b2b55914038d0941fddeb6623docker rmi -f $(docker images -aq) #递归删除所有镜像
容器命令
说明:有了镜像才可以创建容器
下载镜像,新建容器,并启动测试
root@Ubuntu:~# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latestdocker run [可选参数] image
#参数说明
--name="Name" 容器名字
-d 后台方式运行
-it 使用交互方式运行
-p 指定容器端口 -p 8080:8080-p ip 主机端口:容器端口-p 主机端口:容器端口 (主机端口映射到容器端口)-p 容器端口容器端口
-P(大写) 随机指定端口#测试,启动并进入容器
root@Ubuntu:~# docker run -it centos /bin/bash
[root@b157ddb080d0 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr#从容器退出
[root@b157ddb080d0 /]# exit
exit
root@Ubuntu:~# ls
aa packge snap
查看运行中的容器
docker ps 选项-a 显示当前运行的容器+历史运行过的容器-n=? 显示最近创建的容器-p 只显示容器编号docker ps #查看当前运行中的容器
docker pa -a #查看曾经运行过的容器root@Ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@Ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b157ddb080d0 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago naughty_dijkstra
bbfd11c04c36 1b44b5a3e06a "/hello" 42 hours ago Exited (0) 42 hours ago festive_jang
d4ea29e52edc 1b44b5a3e06a "/hello" 43 hours ago Exited (0) 43 hours ago exciting_lamarr
退出容器
exit #直接容器停止并退出
Ctrl q + p #容器不停止退出
删除容器
docker rm 容器id #指定删除容器 不能删除正在运行的容器
docker rm -f $(docker ps -aq) #递归删除所有容器docker ps -a -q | xargs docker rm #删除所有容器
xargs 的作用:把前面输出的多个 ID 变成参数传给 docker rm
启动,重启,停止容器
docker start 容器id
docker restart 容器id
docker stop 容器id #停止当前正在运行容器
stop是优雅退出,如保存数据,释放资源docker kill 容器id #强制停止当前容器
kill一般用于stop不能成功停止容器时使用
常用其他命令
后台启动容器
root@Ubuntu:~# docker run -d centos:latest
76ef8cae27d9f1557f286713d2301e49fad9b39858f905b75bdf1302b0694c11
root@Ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES问题:执行docker ps后发现,Centos停止
常见的坑:docker容器使用后台运行,必须要有一个前台进程,docker发现没有应用,自动停止
容器启动后,发现自己没有提供服务,就会立刻停止,没有程序了
查看日志
docker logs -tf 容器id
#显示日志-tf #显示日志--tail number #显示日志条数
查看容器中进程信息
docker top 容器idroot@Ubuntu:~# docker top 7153c2a9a256
UID PID PPID C STIME TTY TIME CMD
root 12179 12155 0 15:02 ? 00:00:00 /bin/bash
查看镜像元数据
docker inspect 容器id
root@Ubuntu:~# docker inspect 7153c2a9a256
[{"Id": "7153c2a9a2564f8a89bdbf9f2563850505026de38931177130f263b7cd6cce09","Created": "2025-10-03T07:02:52.008836519Z","Path": "/bin/bash","Args": [],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 12179,"ExitCode": 0,"Error": "","StartedAt": "2025-10-03T07:02:52.083476054Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6","ResolvConfPath": "/var/lib/docker/containers/7153c2a9a2564f8a89bdbf9f2563850505026de38931177130f263b7cd6cce09/resolv.conf","HostnamePath": "/var/lib/docker/containers/7153c2a9a2564f8a89bdbf9f2563850505026de38931177130f263b7cd6cce09/hostname","HostsPath": "/var/lib/docker/containers/7153c2a9a2564f8a89bdbf9f2563850505026de38931177130f263b7cd6cce09/hosts","LogPath": "/var/lib/docker/containers/7153c2a9a2564f8a89bdbf9f2563850505026de38931177130f263b7cd6cce09/7153c2a9a2564f8a89bdbf9f2563850505026de38931177130f263b7cd6cce09-json.log","Name": "/elated_neumann","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "docker-default","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "jso