Docker容器

Linux搭建docker

本文主要是介绍Linux搭建docker,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


### 1. 查看系统的内核版本

```bash
[root@wide ~]# uname -r
3.10.0-1160.el7.x86_64
```

### 2. 将yum更新到最新版本

```bash
[root@wide ~]# yum upate -y
Complete!
```

### 3. 安装Docker所需的依赖包

```bash
[root@wide ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.bupt.edu.cn
* extras: mirrors.bupt.edu.cn
* updates: mirrors.qlu.edu.cn
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Package device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64 already installed and latest version
Package 7:lvm2-2.02.187-6.el7_9.5.x86_64 already installed and latest version
Nothing to do
[root@wide ~]#
```

### 4.设置Docker的yum的源

```bash
[root@wide ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
```

### 5. 查看仓库所有Docker版本

```bash
[root@wide ~]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.qlu.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
* extras: mirrors.bupt.edu.cn
docker-ce.x86_64 3:23.0.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:23.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:23.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:23.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.24-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.23-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.22-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.21-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.20-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.19-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.18-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.17-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.16-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.15-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.14-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.13-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.15-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.bupt.edu.cn
Available Packages
```

### 6. 安装Docker

```bash
[root@wide ~]# yum install docker-ce -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.bupt.edu.cn
* extras: mirrors.bupt.edu.cn
* updates: mirrors.qlu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:23.0.3-1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-23.0.3-1.el7.x86_64
--> Processing Dependency: containerd.io >= 1.6.4 for package: 3:docker-ce-23.0.3-1.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-23.0.3-1.el7.x86_64
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-23.0.3-1.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
---> Package containerd.io.x86_64 0:1.6.20-3.1.el7 will be installed
---> Package docker-ce-cli.x86_64 1:23.0.3-1.el7 will be installed
--> Processing Dependency: docker-buildx-plugin for package: 1:docker-ce-cli-23.0.3-1.el7.x86_64
--> Processing Dependency: docker-compose-plugin for package: 1:docker-ce-cli-23.0.3-1.el7.x86_64
---> Package docker-ce-rootless-extras.x86_64 0:23.0.3-1.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-23.0.3-1.el7.x86_64
--> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-23.0.3-1.el7.x86_64
--> Running transaction check
---> Package docker-buildx-plugin.x86_64 0:0.10.4-1.el7 will be installed
---> Package docker-compose-plugin.x86_64 0:2.17.2-1.el7 will be installed
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> Running transaction check
---> Package fuse3-libs.x86_64 0:3.6.1-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================================
Installing:
docker-ce x86_64 3:23.0.3-1.el7 docker-ce-stable 23 M
Installing for dependencies:
container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k
containerd.io x86_64 1.6.20-3.1.el7 docker-ce-stable 34 M
docker-buildx-plugin x86_64 0.10.4-1.el7 docker-ce-stable 12 M
docker-ce-cli x86_64 1:23.0.3-1.el7 docker-ce-stable 13 M
docker-ce-rootless-extras x86_64 23.0.3-1.el7 docker-ce-stable 8.8 M
docker-compose-plugin x86_64 2.17.2-1.el7 docker-ce-stable 12 M
fuse-overlayfs x86_64 0.7.2-6.el7_8 extras 54 k
fuse3-libs x86_64 3.6.1-4.el7 extras 82 k
slirp4netns x86_64 0.4.3-4.el7_8 extras 81 k

Transaction Summary
.............
Installed:
docker-ce.x86_64 3:23.0.3-1.el7

Dependency Installed:
container-selinux.noarch 2:2.119.2-1.911c772.el7_8 containerd.io.x86_64 0:1.6.20-3.1.el7 docker-buildx-plugin.x86_64 0:0.10.4-1.el7 docker-ce-cli.x86_64 1:23.0.3-1.el7
docker-ce-rootless-extras.x86_64 0:23.0.3-1.el7 docker-compose-plugin.x86_64 0:2.17.2-1.el7 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 fuse3-libs.x86_64 0:3.6.1-4.el7
slirp4netns.x86_64 0:0.4.3-4.el7_8

Complete!
```

### 7.启动Docker并添加开机自启动

```bash
[root@wide ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@wide ~]# systemctl start docker
[root@wide ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2023-04-10 23:41:28 PDT; 26s ago
Docs: https://docs.docker.com
Main PID: 4709 (dockerd)
Tasks: 8
Memory: 107.6M
CGroup: /system.slice/docker.service
└─4709 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Apr 10 23:41:27 wide dockerd[4709]: time="2023-04-10T23:41:27.274527839-07:00" level=info msg="[core] [Channel #4] Channel Connectivity change to READY" module=grpc
Apr 10 23:41:27 wide dockerd[4709]: time="2023-04-10T23:41:27.300770723-07:00" level=info msg="Loading containers: start."
Apr 10 23:41:27 wide dockerd[4709]: time="2023-04-10T23:41:27.940276724-07:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Dae... IP address"
Apr 10 23:41:28 wide dockerd[4709]: time="2023-04-10T23:41:28.094586748-07:00" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
Apr 10 23:41:28 wide dockerd[4709]: time="2023-04-10T23:41:28.179894757-07:00" level=info msg="Loading containers: done."
Apr 10 23:41:28 wide dockerd[4709]: time="2023-04-10T23:41:28.213789267-07:00" level=info msg="Docker daemon" commit=59118bf graphdriver=overlay2 version=23.0.3
Apr 10 23:41:28 wide dockerd[4709]: time="2023-04-10T23:41:28.214098003-07:00" level=info msg="Daemon has completed initialization"
Apr 10 23:41:28 wide dockerd[4709]: time="2023-04-10T23:41:28.236177099-07:00" level=info msg="[core] [Server #7] Server created" module=grpc
Apr 10 23:41:28 wide systemd[1]: Started Docker Application Container Engine.
Apr 10 23:41:28 wide dockerd[4709]: time="2023-04-10T23:41:28.239927052-07:00" level=info msg="API listen on /run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
[root@wide ~]#
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/613824ae9afe4488b631fbedb8e380b3.png)

### 8.查看docker版本

```bash
[root@wide ~]# docker --version
Docker version 23.0.3, build 3e7cbfd
```

### 9.卸载 Docker

```bash
[root@wide ~]#sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-selinux \docker-engine-selinux \docker-engine
```

### 10.安装 Portainer

1.创建目录

```bash
[root@wide ~]# mkdir -p /data/portainer/data /data/portainer/public
```

创建一个 portainer 下的放置扩展包public文件夹
2.可以对这个扩展包进行汉化可以将汉化开展放到public下
3. 创建 portainer 卷

```bash
[root@wide ~]# docker volume create portainer_data
portainer_data
```

4.拉取最新的 Portainer

```bash
[root@wide ~]# docker pull portainer/portainer
Using default tag: latest
latest: Pulling from portainer/portainer
```

5.安装和启动
第一种:

```bash
docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
```

第二种:

```bash
docker run -d --restart=always --name portainer -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v /data/portainer/data:/data -v /data/portainer/public:/public portainer/portainer
```

```bash
[root@laona portainer]# docker run -d --restart=always --name portainer -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v /data/portainer/data:/data -v /data/portainer/public:/public portainer/portainer
a29864c820494afe3e465ce9b58e686851f5c6526532fe52fc4b83c1cc0b705e
```

• -v 为挂载本地文件路径到 Docker 中,用法为:-v 本地路径:docker中的路径
5.1如果防火墙开启中,需要开放端口,不然在第六步访问不了

```bash
firewall-cmd --zone=public --add-port=9000/tcp --permanent
```

6.访问 Portainer
• 地址:<http://【ip地址】:9000/>
• 操作:登录后设置你的用户名和密码,并设置本地Docker即可

这篇关于Linux搭建docker的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!