1. 查看docker客户端和服务端信息
docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 1
Server Version: 20.10.14
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
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:
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-27-generic
Operating System: Ubuntu Core 18
OSType: linux
Architecture: x86_64
CPUs: 6
Total Memory: 7.764GiB
Name: hvbuntu
ID: 4O77:ARQG:GC4K:MNW4:ZAYZ:JMTX:XYVP:LHGI:B2TJ:7IDI:AJL3:4KNZ
Docker Root Dir: /var/snap/docker/common/var-lib-docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
docker存储驱动
- overlay2
网络
- overlay ?
2. 查看docker版本(简化版的docker info)
```shell docker version Client: Version: 20.10.14 API version: 1.41 Go version: go1.16.15 Git commit: a224086349 Built: Thu Mar 24 17:14:32 2022 OS/Arch: linux/amd64 Context: default Experimental: true
Server:
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 17:15:03 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit:
docker-init:
Version: 0.19.0
GitCommit: de40ad0
<a name="O1lsC"></a>
### 3. 查看镜像构建过程
语法:docker history 镜像名
```shell
docker history ansible/centos7-ansible:latest
IMAGE CREATED CREATED BY SIZE COMMENT
688353a31fde 5 years ago /bin/sh -c #(nop) ENV ANSIBLE_LIBRARY=/opt/… 0B
<missing> 5 years ago /bin/sh -c #(nop) ENV PYTHONPATH=/opt/ansib… 0B
<missing> 5 years ago /bin/sh -c #(nop) ENV PATH=/opt/ansible/ans… 0B
<missing> 5 years ago /bin/sh -c git submodule update --init 0B
<missing> 5 years ago /bin/sh -c #(nop) WORKDIR /opt/ansible/ansi… 0B
<missing> 5 years ago /bin/sh -c git clone http://github.com/ansib… 87.9MB
<missing> 5 years ago /bin/sh -c mkdir /opt/ansible/ 0B
<missing> 5 years ago /bin/sh -c echo '[local]\nlocalhost\n' > /et… 21B
<missing> 5 years ago /bin/sh -c mkdir /etc/ansible/ 0B
<missing> 5 years ago /bin/sh -c yum clean all && yum -y insta… 167MB
<missing> 5 years ago /bin/sh -c #(nop) MAINTAINER Toshio Kuratom… 0B
<missing> 5 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 years ago /bin/sh -c #(nop) LABEL name=CentOS Base Im… 0B
<missing> 5 years ago /bin/sh -c #(nop) ADD file:940c77b6724c00d42… 192MB
<missing> 5 years ago /bin/sh -c #(nop) MAINTAINER https://github… 0B
4. 构建镜像
docker build -f /securitit/dockerfile/dockerfile -t securitit-nginx-maintainer:1.0.0.1 .
如果Dockerfile在当前目录而且名字为“Dockerfile”
docker build -t securitit-nginx-maintainer:1.0.0.1 .
5. 查看镜像信息
docker inspect
docker inspect —format=’{{.NetworkSettings.IPAddress}}’ 容器名称(容器ID)
# 先查看全部
docker inspect 92025240acc8
docker inspect -f {{".Config.Cmd"}} 92025240acc8
结合jq
docker inspect f7384f2ed04e | jq .[0].NetworkSettings.Networks.bridge.IPAddress
"172.17.0.2"
docker inspect -f "{{json .NetworkSettings.Networks.bridge.IPAddress}}" 68d04
"172.17.0.2"
如果返回结果是一个 map, slice, array 或 string,则可以使用 index 加索引序号(从零开始计数)来读取属性值
Dockerfile 指令
MAINTAINER 作者
MAINTAINER https://github.com/CentOS/sig-cloud-instance-images
或
MAINTAINER Toshio Kuratomi <tkuratomi@ansible.com>
LABEL 标签
LABEL name=CentOS Base Image vendor=CentOS license=GPLv2 build-date=20161214
LABEL是一个更灵活的版本,可以替代MAINTAINER,LABEL可以设置任何需要设置的元数据,并且可以轻松查看,例如docker inspect。使用LABEL设置MAINTAINER可以使用如下命令:
LABEL maintainer="Securitit@163.com"
| FROM | 指定基础镜像 | 从哪里开始构建 | | —- | —- | —- | | RUN | 运行命令 | | | COPY | 复制文件 | | | ADD | 更高级的复制文件 | | | ENV | 设置环境变量 | ENV key value
或 ENV key=value key2=value2 | | EXPOSE | 暴露端口 | EXPOSE 80
EXPOSE 8080 | | VOLUME | 定义匿名卷 | | | WORKDIR | 指定工作目录 | | | USER | 指定当前用户 | | | CMD | 用于指定在容器启动时所要执行的命令 | CMD [“executable”,”param1”,”param2”] CMD [“param1”,”param2”] CMD command param1 param2 | | | | |
容器构建建议:
- 轻量化,能在短时间内停止、销毁、重新生成新的容器。减小镜像大小,只安装依赖的软件包。
- 一个容器只做一件事,解耦复杂应用。
- 减少镜像层
发布镜像
- docker login
- docker push learn/ping
保存对容器的修改
docker commit container_id learn/ping
容器命令如果返回结果是一个 map, slice, array 或 string,则可以使用 index 加索引序号(从零开始计数)来读取属性值
# 输出hello world
docker run ubuntu:15.10 /bin/echo "hello world"
-t 在新容器内指定一个伪终端或终端
-i 允许你对容器内的标准输入 (STDIN) 进行交互
退出 ctrl + D 或 esc
后台模式
-d
# 查看日志
docker logs 2b1b7a428627
# 停止容器
docker stop amazing_cori
#显示资源使用情况
docker stats cid
89c682e266e8 keen_neumann 0.07% 692KiB / 7.764GiB 0.01% 866B / 0B 0B / 0B 2
portainer
lazydocker
导出容器
docker export 1e560fca3906 > ubuntu.tar
导入容器快照
cat docker/ubuntu.tar | docker import - test/ubuntu:v1
清理容器
# 清理所有已经暂停的容器
docker container prune