文章目录
一、docker基本命令
1.1 查看镜像–docker images
#查看下载到本地的所有镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 7 months ago 204MB
--------------------------------------------------------------------------------
REPOSITORY :镜像属于的仓库;
TAG :镜像的标签信息,标记同一个仓库中的不同镜像;
IMAGE ID :镜像的唯一ID 号,唯一标识一个镜像,经过md5方式加密过;
CREATED :镜像创建时间;
VIRTUAL SIZE :镜像大小;
1.2 查看所有状态容器——docker ps -a
docker ps -a #-a 选项可以显示所有的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
471f86645335 hello-world "/hello" 6 minutes ago Exited (0) 6 minutes ago friendly_benz
##字段说明
CONTAINER ID :容器的ID号
IMAGE :加载的镜像
COMMAND :运行的程序
CREATED :创建时间
STATUS :当前的状态
PORTS :端口映射
NAMES :名称
1.3 docker —— run 指令
docker run hello-world
run指令 运行
1. 检测本地有没有该镜像(没有的话直接到docker hub上下载)
2. create(将镜像创建为容器)+ start 将创建好的容器运行起来
工作流程:
- docker client客户端连接到了服务端(服务端是以一个守护进程的形式跑在操作系统里面的) restful api典型的c/s架构
- 由docker服务端的守护进程从docker hub 上下载了镜像(Ps:服务端会先检查本地系统是否有这个镜像)
- 服务端创建了一个新的容器,然后从拉去的这个镜像启动了一个容器,容器执行了脚本/可执行程序让我们可以查看/使用(client)
- docker 服务端把这些信息流(传递)返回到客户端并展示出来,(展示在终端上)
docker client可以是多种形式,比如"docker"命令工具所在的终端
1.4 查看docker版本命令——docker -v
docker -v
docker version
1.5 查看docker信息——docker info
docker info
#可以用该命令统计每一个状态的容器有多少个
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.1-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 3
Server Version: 20.10.14
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: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc version: v1.0.3-0-gf46b6ba
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.683GiB
Name: docker
ID: 6ELU:P5H7:SJVH:RA2Z:ROVP:PNDE:GV3E:OENR:DNHX:X2SE:S2UX:KPYM
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://cnqz5vcz.mirror.aliyuncs.com/
Live Restore Enabled: false
1.6 docker帮助命令文档——docker help
docker help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides
DOCKER_HOST env var and default context set with "docker
context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.8.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.17.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
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.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
二、docker 镜像操作
2.1 搜索镜像(公共仓库)——docker search
默认是在公共仓库找,如果有私有仓库,会在私有仓库找
#格式:docker search 关键字
#示例:
docker search nginx
docker search centos:7
也可以自己制作镜像推送到docker hub上
2.2 下载镜像——docker pull
#格式:docker pull 仓库名称[:标签]
#如果下载镜像时不指定标签,则默认会下载仓库中最新版本的镜像,即选择标签为 latest 标签。
docker pull centos:7
docker pull nginx
2.3 查看镜像列表——docker images
docker images
2.4 获取镜像信息——docker inspect
#格式:docker inspect 镜像ID
#示例:查看hello-world镜像信息
docker insect feb5d9fea6a5
2.5 添加镜像标签——docker tag
#格式:docker tag 名称:[旧标签] 新名称:[新标签]
#示例:
docker tag nginx:latest nginx:lnmp #给nginx打上标签lnmp,原来的标签是latest
2.6 删除镜像——docker rmi
#格式:
docker rmi 仓库名称:标签 #当一个镜像有多个标签时,只是删除其中指定的标签
docker rmi 镜像ID号 #会彻底删除该镜像
PS:如果最后只剩下一个标签,删除这个标签,这个镜像就会被删除
2.7 批量删除镜像
#docker images -q 可以加载镜像id
#批量删除所有镜像
docker rmi `docker images -q`
#批量删除nginx镜像
docker rmi `docker images|grep "nginx"`
2.8 导出/导入镜像——docker save/load
#导出镜像
#格式:docker save -o 存储文件名 存储的镜像
docker save -o nginx_v nginx:latest #存出镜像命名为nginx存在当前目录下
scp nginx_v @root:192.168.24.20:/opt #将导出的镜像以scp方式导到别的服务器上
#导入镜像,可以异地导入,但是必须要有docker引擎,并且版本不可以差太多
#格式:docker load < 存出的文件
docker load < nginx_v
三、容器操作
3.1 查询所有容器运行状态——docker ps -a
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
471f86645335 hello-world "/hello" About an hour ago Exited (0) About an hour ago friendly_benz
容器的ID号 加载的镜像 运行的程序 创建时间 当前的状态 端口映射 名称
3.2 创建容器——docker create
新创建的容器默认处于停止状态,不运行任何程序,需要在其中发起一个进程来启动容器。
#格式:docker create [选项] 镜像
#常用选项:
-i:让容器的输入保持打开
-t:让 Docker 分配一个伪终端
#示例:
docker create -it nginx:latest /bin/bash
3.3 启动容器——docker start
#格式:docker start 容器的ID/名称
docker start 3fbd3ec23e41
docker ps -a
3.4 启动容器(一次性执行)—— docker run
#加 -d 选项让 Docker 容器以守护形式在后台运行。并且容器所运行的程序不能结束。
#示例1:
docker run -itd nginx:latest /bin/bash
#示例2:执行后退出
docker run centos:7 /usr/local/bash -c ls /
#示例3:执行后不退出,以守护进程方式执行持续性任务
docker run -d centos:7 /usr/local/bash -c "while true;do echo hello;done"
#示例1:
#示例2:
#示例3:
3.5 查看容器ip地址——docker inspect
#格式:docker inspect 容器id
docker ps -a #先查看运行时容器的id
docker inspect dc8fb01e3535
3.6 进入容器——docker exec
进入容器的容器状态必须是up状态
和shell 是两种运行模式
-
docker run -it会创建前台进程,但是会在输入exit后终止进程。
-
docker attach会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止容器进程
-
docker exec -it 会连接到容器,可以像sSH一样进入容器内部,进行操作,可以通过exit退出容器,不影响容器运行。
#需要进入容器进行命令操作时,可以使用 docker exec 命令进入运行着的容器。
#格式:docker exec -it 容器ID/名称 /bin/bash
-i 选项表示让容器的输入保持打开;
-t 选项表示让 Docker 分配一个伪终端。
#示例:进入(三种方式)
docker run -itd centos:7 /bin/bash #先运行容器
docker ps -a
1. 使用run进入,可以使用ctrl+d退出,直接退出终端
docker run -it centos:7 /bin/bash
2. 想永久性进入,退出后还是运行状态,用docker exec
docker ps -a
docker exec -it a35f87305c71 /bin/bash
3. docker attach,会通过连接stdin,连接到容器内输入输出流,输入exit后终止容器进程(临时性的,不推荐)
#1.使用run进入,是一次性进入
#2.永久性进入,用docker exec
3.7 容器导出/导入——docker export
用户可以将任何一个 Docker 容器从一台机器迁移到另一台机器。
在迁移过程中,可以使用docker export 命令将已经创建好的容器导出为文件,无论这个容器是处于运行状态还是停止状态均可导出。
可将导出文件传输到其他机器,通过相应的导入命令实现容器的迁移。
#导出格式:docker export 容器ID/名称 > 文件名
docker export b99e0771c4e1 > centos_7
#导入格式:cat 文件名 | docker import – 镜像名称:标签
法①
docker import centos_7 centos:v1 #导入后会生成镜像,但不会创建容器
法②
cat centos_7 |docker import - centos:v2
#导出
#导入
方法1 :
方法2:
3.8 删除容器——docker rm -f
#格式:docker rm [-f] 容器ID/名称
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
#1. 不能删除运行状态的容器,只能-f强制删除,或者先停止再删除
#2. 已经退出的容器,可以直接删除
3.9 查看docker消耗的资源状态
docker stats