在我的博客阅读本文会有更好的阅读体验哦!博客: www.lxiaocode.com
更多内容可以到我博客浏览: www.lxiaocode.com
B站算法动画: https://space.bilibili.com/22392939
1. Docker 基本概念
Docker 是一个让开发人员和运维人员利用容器来构建、运行和共享应用程序的平台。可以将 Docker 看作一台速度飞快地虚拟机,使用容器运行应用程序并将与其他运行的容器隔离起来。
将应用程序部署在容器中被称为容器化。容器的概念越来越流行,而 Docker 容器的优点是:
- 灵活性:即使最复杂的应用程序也可以被封装在容器。
- 轻量化:容器利用和共享主机内核,使得在系统资源方面比虚拟机更加有效。
- 可移植性:你可以在本地构建,部署到云端并在任何地方运行。
- 低耦合:容器具有高度的自给自足性和封装性,可以在不破坏其他容器的情况下进行替换或更新。
- 可扩展性:你可以在数据中心增加和自动分发容器副本。
- 安全性:容器会对进程应用进行约束和隔离,不需要用户进行任何配置。
容器和镜像
从本质上讲,容器只是一个运行的进程,并且应用了一些封装的特性使与主机中其他的容器进行隔离。容器的隔离性最重要的地方之一是每个容器都与自己专属的文件系统进行交互,而该文件系统是由 Docker 镜像提供的。镜像包含了运行应用程序所需的所有内容。
容器和虚拟机
容器在 Linux 本地运行,并且与其他容器共享主机的内核。每个容器都运行着一个独立的进程,不会占用其他可执行文件的内存,所以它非常的轻巧和快速。
与虚拟机相比,在虚拟机中会运行一个完整的操作系统,通过管理程序来虚拟访问主机资源。所以虚拟机会消耗更多的开销。
2. 获取镜像
# 从镜像仓库中查找镜像
$ docker search [image name]
# 从镜像仓库中查找 stars 大于 10 的镜像
$ docker search --filter stars=10 [image name]
# 从镜像仓库中下载或更新指定镜像
$ docker pull [image name][:tag]
# 从镜像仓库中下载指定的所有镜像
$ docker pull -a [image name]
3. 镜像操作
# 列出本地镜像
$ docker images
# 删除(一个或多个)本地镜像
$ docker rmi [image name...]
# 强制删除(一个或多个)本地镜像
$ docker rmi -f [image name...]
# 更新镜像
$ docker commit -m=[commit message] -a=[author] [container id] [image name]
# 使用 Dockerfile 构建镜像
$ docker build -t [image name] [dockerfile path]
# 将指定镜像文件保存为 tar 归档文件
$ docker save -o [path] [image name]
# 导入(使用 docker save 命令导出的)归档文件
$ docker load -i [path]
# 导入归档文件
$ docker import [path] [image name]
4. 容器操作
# 列出正在运行的容器
$ docker ps
# 列出所有容器
$ docker ps -a
# 将文件系统保存为 tar 归档文件
$ docker export -o [path] [container name]
# 查看指定容器的端口映射
$ docker port [container name]
5. 容器的生命周期管理
# 使用镜像创建容器,并后台运行
$ docker run -d --name [container name] [image name]
# 创建容器,并将容器端口映射到宿主端口
$ docker run -d --name [container name] -p [local port]:[container port] [image name]
# 启动(一个或多个)已停止的容器
$ docker start [container name...]
# 关闭(一个或多个)运行中的容器
$ docker stop [container name...]
# 重启(一个或多个)容器
$ docker restart [container name...]
# 杀掉一个运行中的容器
$ docker kill [container name]
# 删除(一个或多个)容器
$ docker rm [container name...]
# 创建一个容器但不启动它
$ docker create --name [container name] [image name]
# 在 bash 中执行容器
$ docker exec -it [container name] /bin/bash
6. docker --help
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.