Linux中安装Docker详解

47 阅读3分钟

Linux中安装Docker详解:

建议使用Centos7 或者 Centos8,Centos7和Centos8用法配置都是一样的。

linux安装docker官方文档:docs.docker.com/install/lin…

1、Linux中安装Docker的准备工作***

为了方便测试请关闭selinux、关闭防火墙。

SELinux防火墙的设置:

[root@localhost ~]# getenforce  
Disabled

修改/etc/selinux/config 文件  
将SELINUX=enforcing改为SELINUX=disabled

Firewalld防火墙的设置:****

1、firewalld的基本使用:  
启动: systemctl start firewalld  
关闭: systemctl stop firewalld  
查看状态: systemctl status firewalld  
开机禁用 : systemctl disable firewalld  
开机启用 : systemctl enable firewalld  
  
2、firewall-cmd的基本使用:  
那怎么开启一个端口呢:  
firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效)  
重新载入:  
firewall-cmd --reload       修改firewall-cmd配置后必须重启  
查看:  
firewall-cmd --zone= public --query-port=80/tcp  
删除:  
firewall-cmd --zone= public --remove-port=80/tcp --permanent  
  
查看所有打开的端口:  
firewall-cmd --zone=public --list-ports

2、Linux Centos中安装Docker**

安装需要的软件包:

yum install -y yum-utils

配置docker源

yum-config-manager  --add-repo   <https://download.docker.com/linux/centos/docker-ce.repo>
或者阿里云源  
  
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum search docker

image.png

安装docker

yum install docker-ce docker-ce-cli containerd.io -y

• containerd.io - daemon to interface with the OS API (in this case, LXC - Linux Containers), essentially decouples Docker from the OS, also provides container services for non-Docker container managers

• docker-ce - Docker daemon, this is the part that does all the management work, requires the other two on Linux

• docker-ce-cli - CLI tools to control the daemon, you can install them on their own if you want to control a remote Docker daemon

3 、启动docker**

systemctl start docker

开机启动

systemctl enable docker

看docker 状态

systemctl status docker

查看自启动****

systemctl list-unit-files|grep enabled

systemctl list-unit-files | grep enabled |grep docker

image.png

docker --version 第一个docker 命令

docker info

image.png

第二个命令查看docker版本

docker --version

4、安装指定版本的docker

要安装特定版本的 Docker Engine,请在 repo 中列出可用版本,然后选择并安装:

一种。列出并排序您的存储库中可用的版本。此示例按版本号对结果进行排序,从高到低,并被截断:

yum list docker-ce --showduplicates | sort -r
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

5、docker daemon.json配置阿里云加速器**

当我们需要对docker服务进行调整配置时,不用去修改主文件 docker.service的参数,通过daemon.json配置文件来管理,更为安全、合理。对于

mkdir -p /etc/docker

vi /etc/docker/daemon.json

{  
  "registry-mirrors": ["https://02xz0m84.mirror.aliyuncs.com"]  
}

如果没有/etc/docker这个目录就创建这个目录:

image.png

重新加载daemon 重启docker

加载配置文件

systemctl daemon-reload

systemctl restart docker

docker info

image.png

6、通过运行hello world映像来验证Docker引擎安装是否正确**

启动hello-world容器

docker run hello-world

[root@localhost /]# docker run hello-world  
Unable to find image 'hello-world:latest' locally  
latest: Pulling from library/hello-world  
b8dfde127a29: Pull complete  
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e  
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/

7、卸载docker**

1.卸载 Docker Engine、CLI 和 Containerd 包:

 $ sudo yum remove docker-ce docker-ce-cli containerd.io

2.主机上的映像、容器、卷或自定义配置文件不会自动删除。删除所有镜像、容器和卷:

 sudormrf/var/lib/dockersudo rm -rf /var/lib/docker sudo rm -rf /var/lib/containerd

我们必须手动删除任何已编辑的配置文件。