windows 10下Docker安装centos,并使用xshell连接


Docker下安装centos,并使用xshell连接

  • 安装最新版centos
  • 查看安装的镜像
  • 运行容器
  • 通过 exec 命令进入 CentOS 容器
  • 安装sshd
  • 启动ssh服务
  • 使用Xshell连接
  • 保存该镜像
  • 各种错误处理
      • 运行容器失败
      • yum事项
            • 如果失败提示Failed to set locale, defaulting to C.UTF-8 ,可以重新配置语言环境
            • 如果失败如下,可以切换仓库地址为阿里云地址

【windows 10下Docker安装centos,并使用xshell连接】win10下安装Docker Tools很简单省略,启动桌面快捷方式Docker Quickstart Terminal,正常情况如下显示,docker宿主机默认ip192.168.99.100
docker is configured to use the default machine with IP 192.168.99.100 For help getting started, check out the docs at https://docs.docker.comStart interactive shellnt_vc@DESKTOP-48CTFH2 MINGW64 /d/Program Files/Docker Toolbox

安装最新版centos
docker pull centos

查看安装的镜像
$ docker images REPOSITORYTAGIMAGE IDCREATEDSIZE centoslatest470671670cac3 months ago237MB

运行容器
docker run -itd -p 50022:22 --privileged --name myCentos centos /usr/sbin/init

–privileged: 可以使container内的root拥有真正的root权限
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
–name=“myCentos”: 为容器指定一个名称,如果不写,会自动生成一个名字,如modest_bouman;
/usr/sbin/init:因为后面会使用systemctl启动sshd服务,所以添加该项,否则会报错Failed to get D-Bus connection: Operation not permitted,因为覆盖了默认的 /bin/bash,所以不能使用docker attach进入容器了,需要使用以下方式进入
运行错误解决方法
通过 exec 命令进入 CentOS 容器
$ docker exec -it myCentos /bin/bash [root@cd1c0666efb4 /]#

安装sshd
// 安装网络工具,可以使用ifconfig查看网卡ip信息 yum -y install net-tools // 安装ssh服务 yum -y install openssh-server // 需要的话,安装ssh客户端 yum -y install openssh-clients // 需要的话,安装passwd,修改root用户密码,这里就不细说了 yum -y install passwd

需要的话可以使用vi或vim修改sshconfig 【vi /etc/ssh/sshd_config
比如是否允许root登陆,端口号等,默认可以root远程登陆
安装和语言错误解决方法
启动ssh服务
systemctl start sshd

使用Xshell连接 windows 10下Docker安装centos,并使用xshell连接
文章图片

192.168.99.100是咱们docker宿主机的ip,启动docker时映射的端口号是50022,所以需要使用这个端口连接ssh服务,点击连接按钮后再弹出的窗口输入你之前设置的密码,即可完成连接
已经登入root用户,可以操作了
[root@fb7593d8a891 ~]# cd / [root@fb7593d8a891 /]# ls bindevetchomeliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvar [root@fb7593d8a891 /]#

保存该镜像 可以使用docker commit 命令床一个新带有ssh的镜像,以后可以直接使用该镜像
// 使用exit退出centos的shell,进入宿主机shell // 前面说过也可以使用Ctrl+p+q组合键 [root@fb7593d8a891 locale]# exit exit// 查看运行中的容器 nt_vc@DESKTOP-48CTFH2 MINGW64 /d/Program Files/Docker Toolbox $ docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES fb7593d8a891centos"/usr/sbin/init"3 hours agoUp 3 hours0.0.0.0:50022->22/tcpmyCentos// 使用当前运行的容器创建了一个版本号v1的centos镜像 nt_vc@DESKTOP-48CTFH2 MINGW64 /d/Program Files/Docker Toolbox $ docker commit -m "myCentos with ssh" fb7593d8a891 centos:v1sha256:9d80428a50c64f25d04a6149895e782af310a5dba50042c30fd16fbadb13e3f4// 查看镜像列表,可以看到新的镜像 nt_vc@DESKTOP-48CTFH2 MINGW64 /d/Program Files/Docker Toolbox $ docker imagesREPOSITORYTAGIMAGE IDCREATEDSIZE centosv19d80428a50c630 seconds ago341MB centoslatest470671670cac3 months ago237MB

各种错误处理 运行容器失败
如果docker run运行容器报错如下
D:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: cgroups: cannot find c group mount destination: unknown.

解决办法:
  1. 使用Ctrl+p+q组合键可以返回到docker外层操作命令模式
  2. 进入Linux虚拟机 docker-machine ssh
  3. 创建目录 sudo mkdir /sys/fs/cgroup/systemd
  4. 挂载目录 sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd
  5. 重启Docker Quickstart Terminal后再次执行上面的命令,即可
yum事项
如果失败提示Failed to set locale, defaulting to C.UTF-8 ,可以重新配置语言环境
//解决方法,执行`locale`能够看到当前系统支持字符,按需设置。 //设置系统环境变量 echo "export LC_ALL=en_US.UTF-8">>/etc/profile source /etc/profile //或设置个人环境变量 echo "export LC_ALL=en_US.UTF-8">>~/.bashrc source ~/.bashrc

如果还报错,可以使用yum install glibc-common后再去执行上述命令
如果失败如下,可以切换仓库地址为阿里云地址
Failed to synchronize cache for repo 'AppStream', ignoring this repo. Failed to synchronize cache for repo 'BaseOS', ignoring this repo. Failed to synchronize cache for repo 'extras', ignoring this repo.

修改CentOS-Base.repo文件,方法如下
cd /etc/yum.repos.dvim CentOS-Base.repo# CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client.You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # #[BaseOS] name=CentOS-$releasever - Base # 注释掉mirrorlist,使用baseurl # 修改地址为https://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra baseurl=https://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

修改CentOS-AppStream.repo同上
然后清理缓存,重新创建缓存
yum clean all rm -rf/var/cache/yum/ yum makecache

重新yum install 即可

    推荐阅读