理论

安装(仅离线)

离线包安装

1.上传软件包 ,解压
tar zxf docker-20.10.6.tgz 
2.复制到/usr/bin
cp docker/* /usr/bin/
3.测试
[root@host-192-168-10-105 ~]# docker version
Client:
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:42:10 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
## 因为Docker还没启动,所以会提示最后这个报错
4.注册为Service
vim /usr/lib/systemd/system/docker.service
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
[Install]
WantedBy=multi-user.target


[root@host-192-168-10-105 ~]# systemctl daemon-reload
5.启动Docker
[root@host-192-168-10-105 ~]# systemctl start docker
[root@host-192-168-10-105 ~]# docker version
Client:
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:42:10 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:46:14 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Rpm包安装

1.上传Rpm包 & 进去上传目录 & 安装
rpm -Uvh --force --nodeps *.rpm

使用

目标:利用Docker部署一个Centos系统里面搭建一个Apache

1.导入Centos镜像

可以自行pull,导入,都可以,因为实验环境没有网,所以选择了导入

[root@host-192-168-10-105 ~]# docker import centos-7-x86_64-docker.tar centos7
[root@host-192-168-10-105 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos7      latest    d4a020b6892c   4 seconds ago   204MB

2.运行Centos镜像

docker run -d --name centostoapache  -p 80:80 --privileged=true -v /media:/media centos7 /usr/sbin/init  
## 解读
    -d 丢后台执行
    -p 映射端口
    --privileged=true 特权模式
    -v 映射路径(为了利用本地yum安装服务)
    /usr/sbin/init  创建容器后执行的命令,必须这么打,不然Docker可能会报错

3.进入容器中

docker exec -it centostoapache env LANG=C.UTF-8 /bin/bash

# env LANG=C.UTF-8 这串代码以便直接将容器镜像的语言修改成utf8

4.安装Apache

  • 1.改本地源
  • 2.安装Apache即可

5.报错总结

1.运行Centos镜像报错
[root@host-192-168-10-105 ~]# docker run --name centos -d -p 80:80 centos7
docker: Error response from daemon: No command specified.

解决办法:run指令后加上/usr/sbin/init
2.容器内无法启动服务
## 重启服务报错
Failed to get D-Bus connection: Operation not permitted

##解决办法:
创建容器时带上参数 
--privileged=true -d
然后再用exec进入容器
docker exec -it centos7 /bin/bash
Last modification:June 28th, 2021 at 03:34 pm