Docker软件安装
1、配置yum源
# 官方源 curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo # 阿里源【推荐】 curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 清华源【推荐】 curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo # 上面三选一即可 yum makecache
2、安装docker-ce
2.1、yum安装docker
yum install -y docker-ce docker-ce-cli containerd.io
2.2、启动dcoker服务
systemctl --now start docker
2.3、检查服务器是否多出一个docker0网口
]# ifconfig docker0 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:5d:b6:a8:b5 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.4、查看docker版本
]# docker version Client: Docker Engine - Community Version: 24.0.0 API version: 1.43 Go version: go1.20.4 Git commit: 98fdcd7 Built: Mon May 15 18:51:37 2023 OS/Arch: linux/amd64 Context: default ...
2.5、查看docker的基本属性
]# docker info Client: Docker Engine - Community Version: 24.0.0 Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc.) Version: v0.10.4 Path: /usr/libexec/docker/cli-plugins/docker-buildx compose: Docker Compose (Docker Inc.) Version: v2.17.3 Path: /usr/libexec/docker/cli-plugins/docker-compose Server: Containers: 0 Running: 0 ...
3、容器hello-world
]# docker run hello-world 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/
4、设置镜像加速
4.1、设置daemon.json
tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors":["https://qbd2mtyh.mirror.aliyuncs.com"], "insecure-registries":[] } EOF systemctl restart docker
4.2、检查是否修改
]# docker info | tail -4 Registry Mirrors: https://qbd2mtyh.mirror.aliyuncs.com/ Live Restore Enabled: false
5、设置远程连接Docker【一般可以不用设置】
5.1、需求
有些时候,我们需要 远程跨主机执行docker相关的操作,对于docker软件来说,它提供了这种远程登录的 命令,主要是通过两种方式: socket方式 unix:///var/run/docker.sock tcp方式 tcp://0.0.0.0:2375
5.2、修改服务启动参数
sed -i '/^ExecStart=/c ExecStart=/usr/bin/dockerd' /lib/systemd/system/docker.service systemctl daemon-reload
5.3、配置daemon.json
tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors":["https://qbd2mtyh.mirror.aliyuncs.com"], "insecure-registries":[], "hosts":["tcp://0.0.0.0:12306","unix://var/run/docker.sock"] } EOF systemctl restart docker
5.4、客户端远程控制测试
]# docker -H tcp://192.168.10.19:12306 images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 9c7a54a9a43c 12 days ago 13.3kB