docker 安装和使用

文章目录

    • 安装
    • 配置
      • 国内镜像
      • 容器路径
    • daemon.json详解
    • docker 基本操作

安装

centos7

yum install -y yum-utils device-mapper-persistent-data lvm2 #相关依赖
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #添加软件源
yum -y install docker-ce # 安装docker  版本18.09.6-3.el7 
systemctl enable docker #开机启动
systemctl start docker #开启docker 

deepin

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common #安装 apt 依赖包,用于通过 HTTPS 来获取仓库。
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - #添加 Docker 的官方 GPG 密钥
sudo apt-get install docker-ce docker-ce-cli containerd.io -y   #安装最新版本的 Docker Engine-Community  18.09.6~3-0~debian-stretch
# apt-cache madison docker-ce # 安装特殊版本 
# sudo apt-get install docker-ce= docker-ce-cli= containerd.io
systemctl enable docker #开机启动
systemctl start docker #开启docker 

配置

国内镜像


vi /etc/docker/daemon.json 
{
    "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

重启docker 生效

systemctl daemon-reload
systemctl restart docker

容器路径

vi /lib/systemd/system/docker.service
在ExecStart后加入:
–graph=/data/docker/
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph=/data/docker/

重启docker 生效

systemctl daemon-reload
systemctl restart docker

daemon.json详解

vi /etc/docker/daemon.json

{
    "authorization-plugins": [],
    "data-root": "", 
     #Docker运行时使用的根路径,根路径下的内容稍后介绍,默认/var/lib/docker
    "dns": [],  
     #设定容器DNS的地址,在容器的 /etc/resolv.conf文件中可查看
    "dns-opts": [],
     #容器 /etc/resolv.conf 文件,其他设置
    "dns-search": [],
     #设定容器的搜索域,当设定搜索域为 .example.com 时,在搜索一个名为 host 的 主机时,DNS不仅搜索host,还会搜索host.example.com。注意:如果不设置,Docker 会默认用主机上的 /etc/resolv.conf来配置容器。
    "exec-opts": [],
    "exec-root": "",
    "experimental": false,
    "features": {},
    "storage-driver": "",
    "storage-opts": [],
    "labels": [],
     #docker主机的标签,很实用的功能,例如定义:–label nodeName=host-121
    "live-restore": true,
    "log-driver": "",
    "log-opts": {},
    "mtu": 0,
    "pidfile": "",
     #Docker守护进程的PID文件
    "cluster-store": "",
    "cluster-store-opts": {},
    "cluster-advertise": "",
    "max-concurrent-downloads": 3,
    "max-concurrent-uploads": 5,
    "default-shm-size": "64M",
    "shutdown-timeout": 15,
    "debug": true, 
     #启用debug的模式,启用后,可以看到很多的启动信息。默认false
    "hosts": [],
    #设置容器hosts
    "log-level": "",
    "tls": true,  
     #默认 false, 启动TLS认证开关
    "tlscacert": "", 
     #默认 ~/.docker/ca.pem,通过CA认证过的的certificate文件路径
    "tlscert": "", 
     #默认 ~/.docker/cert.pem ,TLS的certificate文件路径
    "tlskey": "",
     #默认~/.docker/key.pem,TLS的key文件路径
    "tlsverify": true,
     #默认false,使用TLS并做后台进程与客户端通讯的验证
    "tls": true,
    "tlsverify": true,
    "tlscacert": "",
    "tlscert": "",
    "tlskey": "",
    "swarm-default-advertise-addr": "",
    "api-cors-header": "",
    "selinux-enabled": false, 
     #默认 false,启用selinux支持
    "userns-remap": "",
    "group": "",
     #Unix套接字的属组,仅指/var/run/docker.sock
    "cgroup-parent": "",
    "default-ulimits": {
        "nofile": {
            "Name": "nofile",
            "Hard": 64000,
            "Soft": 64000
        }
    },
    "init": false,
    "init-path": "/usr/libexec/docker-init",
    "ipv6": false,
    "iptables": false,
    "ip-forward": false,
    #默认true, 启用 net.ipv4.ip_forward ,进入容器后使用sysctl -a|grepnet.ipv4.ip_forward查看
    "ip-masq": false,
    "userland-proxy": false,
    "userland-proxy-path": "/usr/libexec/docker-proxy",
    "ip": "0.0.0.0",
    "bridge": "",
    "bip": "",
    "fixed-cidr": "",
    "fixed-cidr-v6": "",
    "default-gateway": "",
    "default-gateway-v6": "",
    "icc": false,
    "raw-logs": false,
    "allow-nondistributable-artifacts": [],
    "registry-mirrors": [],
     #镜像加速的地址,增加后在 docker info中可查看。
    "seccomp-profile": "",
    "insecure-registries": [],
     #配置docker的私库地址
    "no-new-privileges": false,
    "default-runtime": "runc",
    "oom-score-adjust": -500,
    "node-generic-resources": ["NVIDIA-GPU=UUID1", "NVIDIA-GPU=UUID2"],
    "runtimes": {
        "cc-runtime": {
            "path": "/usr/bin/cc-runtime"
        },
        "custom": {
            "path": "/usr/local/bin/my-runc-replacement",
            "runtimeArgs": [
                "--debug"
            ]
        }
    },
    "default-address-pools":[{"base":"172.80.0.0/16","size":24},
    {"base":"172.90.0.0/16","size":24}]
}

编辑后重启docker生效

systemctl daemon-reload
systemctl restart docker.service

docker 基本操作

docker pull  nginx #下载nginx
docker pull nginx:1.17.9 # 下载指定tag nginx
docker run --name nginx-test -p 8080:80  -d nginx:1.17.9 #创建并运行docker容器
docker stop d68a62810b4d # 停止容器
docker rm d68a62810b4d # 删除容器
docker exec -it d68a62810b4d bash # 命令行和容器交互

你可能感兴趣的:(docker)