01
入门
基本命令
docker run 创建并启动容器。-d 在后台运行,-p 将主机端口映射到容器端口。pull 从镜像仓库(默认 Docker Hub)下载镜像。
docker
# check version
docker --version
# pull an image
docker pull nginx:latest
# run a container
docker run -d --name mynginx -p 8080:80 nginx
# list running containers
docker ps
# list all containers (including stopped)
docker ps -a
# stop and remove a container
docker stop mynginx
docker rm mynginx
# list images
docker images容器生命周期
create + start 就是 run 在底层所做的操作。stop 发送 SIGTERM 进行优雅关闭;kill 立即发送 SIGKILL。pause 使用 cgroup 冻结器——适用于在不停止进程的情况下进行快照。
docker
# create a container without starting it
docker create --name web nginx
# start a created (or stopped) container
docker start web
# stop gracefully (SIGTERM then SIGKILL after 10s)
docker stop web
# force kill immediately
docker kill web
# restart a container
docker restart web
# pause/resume (freezes process with cgroups)
docker pause web
docker unpause web镜像基础
镜像是只读的层。rmi 在容器仍引用该镜像时会失败——先移除容器。history 显示创建每一层的指令及其大小,对优化构建很有用。
docker