1. 查看docker客户端和服务端信息

  1. docker info
  2. Client:
  3. Context: default
  4. Debug Mode: false
  5. Server:
  6. Containers: 0
  7. Running: 0
  8. Paused: 0
  9. Stopped: 0
  10. Images: 1
  11. Server Version: 20.10.14
  12. Storage Driver: overlay2
  13. Backing Filesystem: extfs
  14. Supports d_type: true
  15. Native Overlay Diff: true
  16. userxattr: false
  17. Logging Driver: json-file
  18. Cgroup Driver: systemd
  19. Cgroup Version: 2
  20. Plugins:
  21. Volume: local
  22. Network: bridge host ipvlan macvlan null overlay
  23. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  24. Swarm: inactive
  25. Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
  26. Default Runtime: runc
  27. Init Binary: docker-init
  28. containerd version: 3df54a852345ae127d1fa3092b95168e4a88e2f8
  29. runc version:
  30. init version: de40ad0
  31. Security Options:
  32. apparmor
  33. seccomp
  34. Profile: default
  35. cgroupns
  36. Kernel Version: 5.15.0-27-generic
  37. Operating System: Ubuntu Core 18
  38. OSType: linux
  39. Architecture: x86_64
  40. CPUs: 6
  41. Total Memory: 7.764GiB
  42. Name: hvbuntu
  43. ID: 4O77:ARQG:GC4K:MNW4:ZAYZ:JMTX:XYVP:LHGI:B2TJ:7IDI:AJL3:4KNZ
  44. Docker Root Dir: /var/snap/docker/common/var-lib-docker
  45. Debug Mode: false
  46. Registry: https://index.docker.io/v1/
  47. Labels:
  48. Experimental: false
  49. Insecure Registries:
  50. 127.0.0.0/8
  51. 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

  1. <a name="O1lsC"></a>
  2. ### 3. 查看镜像构建过程
  3. 语法:docker history 镜像名
  4. ```shell
  5. docker history ansible/centos7-ansible:latest
  6. IMAGE CREATED CREATED BY SIZE COMMENT
  7. 688353a31fde 5 years ago /bin/sh -c #(nop) ENV ANSIBLE_LIBRARY=/opt/… 0B
  8. <missing> 5 years ago /bin/sh -c #(nop) ENV PYTHONPATH=/opt/ansib… 0B
  9. <missing> 5 years ago /bin/sh -c #(nop) ENV PATH=/opt/ansible/ans… 0B
  10. <missing> 5 years ago /bin/sh -c git submodule update --init 0B
  11. <missing> 5 years ago /bin/sh -c #(nop) WORKDIR /opt/ansible/ansi… 0B
  12. <missing> 5 years ago /bin/sh -c git clone http://github.com/ansib… 87.9MB
  13. <missing> 5 years ago /bin/sh -c mkdir /opt/ansible/ 0B
  14. <missing> 5 years ago /bin/sh -c echo '[local]\nlocalhost\n' > /et… 21B
  15. <missing> 5 years ago /bin/sh -c mkdir /etc/ansible/ 0B
  16. <missing> 5 years ago /bin/sh -c yum clean all && yum -y insta… 167MB
  17. <missing> 5 years ago /bin/sh -c #(nop) MAINTAINER Toshio Kuratom… 0B
  18. <missing> 5 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  19. <missing> 5 years ago /bin/sh -c #(nop) LABEL name=CentOS Base Im… 0B
  20. <missing> 5 years ago /bin/sh -c #(nop) ADD file:940c77b6724c00d42… 192MB
  21. <missing> 5 years ago /bin/sh -c #(nop) MAINTAINER https://github… 0B

4. 构建镜像

  1. docker build -f /securitit/dockerfile/dockerfile -t securitit-nginx-maintainer:1.0.0.1 .
  2. 如果Dockerfile在当前目录而且名字为“Dockerfile
  3. docker build -t securitit-nginx-maintainer:1.0.0.1 .

5. 查看镜像信息

docker inspect
docker inspect —format=’{{.NetworkSettings.IPAddress}}’ 容器名称(容器ID)

  1. # 先查看全部
  2. docker inspect 92025240acc8
  3. docker inspect -f {{".Config.Cmd"}} 92025240acc8
  4. 结合jq
  5. docker inspect f7384f2ed04e | jq .[0].NetworkSettings.Networks.bridge.IPAddress
  6. "172.17.0.2"
  7. docker inspect -f "{{json .NetworkSettings.Networks.bridge.IPAddress}}" 68d04
  8. "172.17.0.2"
  9. 如果返回结果是一个 map, slice, array string,则可以使用 index 加索引序号(从零开始计数)来读取属性值

Dockerfile 指令

  1. MAINTAINER 作者

    1. MAINTAINER https://github.com/CentOS/sig-cloud-instance-images
    2. MAINTAINER Toshio Kuratomi <tkuratomi@ansible.com>
  2. LABEL 标签

    1. LABEL name=CentOS Base Image vendor=CentOS license=GPLv2 build-date=20161214

    LABEL是一个更灵活的版本,可以替代MAINTAINER,LABEL可以设置任何需要设置的元数据,并且可以轻松查看,例如docker inspect。使用LABEL设置MAINTAINER可以使用如下命令:

    1. 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 | | | | |

容器构建建议:

  1. 轻量化,能在短时间内停止、销毁、重新生成新的容器。减小镜像大小,只安装依赖的软件包。
  2. 一个容器只做一件事,解耦复杂应用。
  3. 减少镜像层

发布镜像

  • docker login
  • docker push learn/ping

保存对容器的修改

  1. docker commit container_id learn/ping

容器命令如果返回结果是一个 map, slice, array 或 string,则可以使用 index 加索引序号(从零开始计数)来读取属性值

  1. # 输出hello world
  2. docker run ubuntu:15.10 /bin/echo "hello world"
  3. -t 在新容器内指定一个伪终端或终端
  4. -i 允许你对容器内的标准输入 (STDIN) 进行交互
  5. 退出 ctrl + D esc
  6. 后台模式
  7. -d
  8. # 查看日志
  9. docker logs 2b1b7a428627
  10. # 停止容器
  11. docker stop amazing_cori
  12. #显示资源使用情况
  13. docker stats cid
  14. 89c682e266e8 keen_neumann 0.07% 692KiB / 7.764GiB 0.01% 866B / 0B 0B / 0B 2
  15. portainer
  16. lazydocker

导出容器

  1. docker export 1e560fca3906 > ubuntu.tar

导入容器快照

  1. cat docker/ubuntu.tar | docker import - test/ubuntu:v1

清理容器

  1. # 清理所有已经暂停的容器
  2. docker container prune