Linux安装docker详解
一、下载以及安装docker
1.下载docker依赖包
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 device-mapper-persistent-data.x86_64.0.0.8.5-3.el7 将被 升级
---> 软件包 device-mapper-persistent-data.x86_64.0.0.8.5-3.el7_9.2 将被 更新
---> 软件包 lvm2.x86_64.7.2.02.187-6.el7 将被 升级
---> 软件包 lvm2.x86_64.7.2.02.187-6.el7_9.5 将被 更新
--> 正在处理依赖关系 lvm2-libs = 7:2.02.187-6.el7_9.5,它被软件包 7:lvm2-2.02.187-6.el7_9.5.x86_64 需要
---> 软件包 yum-utils.noarch.0.1.1.31-54.el7_8 将被 安装
--> 正在处理依赖关系 python-kitchen,它被软件包 yum-utils-1.1.31-54.el7_8.noarch 需要
--> 正在处理依赖关系 libxml2-python,它被软件包 yum-utils-1.1.31-54.el7_8.noarch 需要
2.docker默认官方的镜像源地址是国外的,速度很慢,这里将数据源设置为阿里云的镜像。
[root@localhost ~]# sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
- 安装docker-ce版本(Docker CE(Community Edition) 社区免费版,属于docker引擎。)
sudo yum install docker-ce
sudo yum install docker-ce
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
docker-ce-stable | 3.5 kB 00:00:00
(1/2): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00:00
(2/2): docker-ce-stable/7/x86_64/primary_db | 80 kB 00:00:00
4.查看docker版本以及运行测试容器
[root@localhost ~]# docker -v
Docker version 20.10.17, build 100c701
5.添加服务、启动docker服务
[root@localhost ~]# sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# sudo systemctl start docker
6.运行hello-world容器,以及查看镜像
[root@localhost ~]# docker run -it hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
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/
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 8 months ago 13.3kB
二、设置远程访问
1.修改docker的/usr/lib/systemd/system/docker.service配置文件,添加以下配置
-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
2.修改防火墙配置,开放2375端口以及8080端口,修改后刷新防火墙配置
[root@localhost ~]# firewall-cmd --list-ports
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=2375/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success