docker基础命令
docker的安装
docker的命令
docker info
docker info的内容
Client: Debug Mode: false Server: Containers: 0 # 服务器的容器个数 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 19.03.6 # 服务器版本 Storage Driver: overlay2 # 存储引擎 Backing Filesystem: xfs # 底层的操作系统 Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: systemd # Cgroup 驱动 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: runc Default Runtime: runc Init Binary: docker-init containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-1062.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 # 使用的cpu核数 Total Memory: 972.4MiB # 内存大小 Name: localhost.localdomain ID: 4653:5R7A:NMOC:G3KH:CBJ2:A2AZ:HMNZ:YSFG:FTZG:UOQP:XODN:RRTT Docker Root Dir: /data/docker # 存放路径 Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: quay.io registry.access.redhat.com 127.0.0.0/8 Registry Mirrors: https://q2gr04ke.mirror.aliyuncs.com/ # 镜像 Live Restore Enabled: true WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled
查看docker版本
docker version
先启动一个docker官方的镜像
docker run hello-world
返回如下信息
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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/
译:
我不能在本地找到一个名为hello-world 的镜像 去远程拉取镜像 已经下载完成了叫做hello-world的最新的镜像 hello 你能看到这个信息说明安装部署docker引擎是非常正确的 为了产生这个信息docker引擎做了以下几件事 1.一个docker的客户端连接到了docker的服务端,这个服务端是以守护进程的方式跑到操作系统进程里的 2.docker的守护进程帮你从DockerHub pull了hello-world镜像过来。 3.docker服务端用拉下来的镜像帮你创建了一个新的容器,然后这个容器执行了一个能看到这个信息的脚本的可执行程序 4.docker服务端把这些信息流返回到你的客户端,然后让你的客户端展示到你的终端上
docker的关系图

镜像管理
镜像的结构:docker.io/library/alpine:latest
docker.io/library/alpine:latest
doocker的一个共有仓库地址: https://hub.docker.com
在上面注册好后执行 docker login docker.io
登入docker
docker login docker.io

docker的配置信息放在 cat /root/.docker/config.json 这个目录下的

echo "c2hpemhlbmd3ZW46c2hpMTU"|base64 -d
搜索一个镜像 docker search alpine
docker search alpine
查找出一堆alpine镜像

下载镜像 docker pull alpine
docker pull alpine
docker pull shizhengwen/alpine # 指定仓库下载

也可以指定版本下载 docker pull alpine:3.9.5
docker pull alpine:3.9.5
还可以写全下载 docker pull docker.io/library/alpine:3.10.1
docker pull docker.io/library/alpine:3.10.1
给镜像打标签 docker tag 82f67be598eb docker.io/Irving/alpine:v5.5 ps:这里的Irving要和用户名一样
docker tag 82f67be598eb docker.io/Irving/alpine:v5.5
推送镜像 docker pull shizhengwen/alpine
docker pull shizhengwen/alpine
查看 镜像 zdocker image ls
docker image ls docker image ls|grep alpine
删除标签 docker rmi docker.io/shizhengwen/alpine:latest
docker rmi docker.io/shizhengwen/alpine:latest
删除镜像 docker rmi 82f67be598eb
docker rmi 82f67be598eb docker rmi -f 82f67be598eb #强制删除
容器
列出本地启动的进程 docker ps
docker ps # 列出本地的docker进程 docker ps -a # 列出本地的进程(包括死了的进程或退出的进程)
启动容器 docker run -it
docker run [OPTION] IMAGE [COMMAND] [ARG]
[OPTION]: 选项 -i # 表示启动一个可交互式的容器,并持续打开标准输入输出 -t # 表示使用终端关联到容器的标准输入输出上 -d # 表示将容器后台运行 --rm # 退出后即删除容器 --name # 表示定义容器唯一名称 IMAGE : 表示要运行的镜像 COMMAND : 表示启动容器时要运行的命令这时在容器里使用 ip add
可以看到这个ip就是之前 /etc/docker/daemon.json 里的bip指定的
这时再用cat /etc/issue 看看发行版
这说明启动的这个镜像里面用的linux发行版就是alpine,alpine里面有它自带的工具和库文件
这时可以用exit 退出去
现在这个容器的状态就是 Exited 了
如果要一次性启动容器 docker run --rm
docker run --rm shizhengwen/alpine:5.5 /bin/echo holle
会自动把进程删掉 还是比较好用的
使用非交互式启动后台容器
[root@localhost ~]# docker run -d --name myalpine1 shizhengwen/alpine:5.5 /bin/sleep 300 26aad025982fde8e5613a6348e63e97c5a6ac0134a4d1ba76d78a89ae85f4221 [root@localhost ~]# ps aux|grep sleep |grep -v grep # 在宿主机上也是能查到这个进程 root 2925 0.0 0.0 1508 248 ? Ss 07:02 0:00 /bin/sleep 300
进入容器 docker exec -ti 33e597e06d8d /bin/sh # 可以用交互式的方法进入到容器挂一个终端
[root@localhost ~]# docker exec -ti 33e597e06d8d /bin/sh
/ # ps
PID USER TIME COMMAND
1 root 0:00 /bin/sleep 300
6 root 0:00 /bin/sh
11 root 0:00 ps
/ #
容器 启动、停止、重启 、删除都得加上容器ID
docker start 33e597e06d8d # 启动容器
docker stop 33e597e06d8d # 停止容器
docker restart 33e597e06d8d # 重启容器
docker restart myalpine1 # 如果指定了容器的名字不用id也可以
docker rm myalpine # 指定名字删除
docker rm 33e597e06d8d # 指定ID删除
docker rm -f myalpine2 # 如果这个容器正在运行就得加 -f 才能删除
for i in `docker ps -a|grep -i exit|awk '{print $1}'`;do docker rm $i;done # 删掉所有死掉的进程
容器固化成镜像
docker commit -p 71e2d3ac6a66 shizhengwen/alpine:v3.33.1_write_1.txt
导出镜像 docker save 3823752a2a9a > alpine:v3.9.3_with_1.txt.tar
[root@localhost ~]# docker save 3823752a2a9a > alpine:v3.9.3_with_1.txt.tar [root@localhost ~]# ll total 5680 -rw-r--r-- 1 root root 5810688 Feb 21 08:12 alpine:v3.9.3_with_1.txt.tar
导入镜像 # 导入镜像后要打标签
docker load < alpine:v3.9.3_with_1.txt.tar docker tag 3823752a2a9a shizhengwen/alpine:v3.10.3_with_1.txt
docker 日志 # 创建了一个进程,查看该进程发现该进程死掉了,用 log + id 能看到该进程的输出
[root@localhost ~]# docker run hello-world 2>&1 >>/dev/null
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3562d38a6205 hello-world "/hello" 7 seconds ago Exited (0) 6 seconds ago musing_pascal
[root@localhost ~]# docker logs 3562d38a6205
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/
[root@localhost ~]#




浙公网安备 33010602011771号