Docker安装CentOS7及基本配置

Docker安装CentOS7及基本配置

https://victorzhong.github.io/2018/01/15/Docker%E5%AE%89%E8%A3%85CentOS7%E5%8F%8A%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE/open in new window

发表于 2018-01-15 |  分类于 Dockeropen in new window |

选择Docker来创建虚拟机的原因主要有:

  1. Docker开源,提供自由免费的CE版。
  2. macOS下的Virtual Box&VMWare实在难用,相比之下Docker显得非常灵活。
  3. VMWare启动虚拟机慢,至少要几分钟;而Docker启动一个镜像时间在10秒内。
  4. CPU和内存问题。通常一个虚拟机只用来跑一个服务,为了这个服务启动一整个虚拟机会额外消耗大量资源。
  5. 第3、4点在搭建集群的时候体验尤为明显。

这篇文章记录一下使用Docker安装CentOS7,以及配置jdk、ssh(用于远程登录)等的过程。

本文在macOS下操作,但同样适用于Windows和Linux。如还未安装Docker,可以参考《Docker安装和基本使用》open in new window

涉及到的软件

名称版本说明
系统macOS v10.13.3
Docker17.12.0-ce-mac55 (23011)
CentOS7.2.1511在Docker内运行的镜像
jdk1.8.0_151在centos内安装

注:其他CentOS内的软件如sshd等均使用yum安装。

安装运行

目前CentOS最新的版本是7.4,本文使用7.2.1511。

运行Docker Engine后,在终端执行docker search centos可以看到所有CentOS的镜像,也可以到Docker Hubopen in new window查看详细列表。

下载

1
2
# 拉取镜像
docker pull centos:7.2.1511

下载完成后查看本地镜像库:

1
2
3
~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.2.1511 0a2bad7da9b5 4 months ago 195MB

运行

执行以下命令装载容器:

1docker run -dit --name centos7 centos:7.2.1511 /bin/bash

参数解析:

  • -d ——后台运行容器并返回容器ID
  • -i ——交互模式运行容器,通常与 -t 同时使用
  • -t ——为容器重新分配一个伪输入终端,通常与 -i 同时使用
  • –name ——为容器指定一个名称,此例为容器指定名为centos7

注:这里必须加上/bin/bash,让容器后执行/bin/bash命令,否则容器将无法使用bash命令。

此时运行docker ps查看正在运行的容器(加上-a可查看所有创建的容器):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
~ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d54753fe138a centos:7.2.1511 "/bin/bash" 4 seconds ago Up 3 seconds centos7
# 连接容器,参数为容器ID。
~ docker attach d54753fe138a
[root@d54753fe138a /]#
# 查看CentOS系统信息
[root@d54753fe138a ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

至此,CentOS7的系统已经跑起来了,要退出只需在终端键入exit回车。如果要停止容器,在终端执行docker container stop 容器ID即可;运行为docker container start 容器ID;要移除容器则执行docker container rm 容器ID。

配置

安装配置ssh

安装软件:

1[root@d54753fe138a /]# yum install passwd openssl openssh-server -y

启动sshd,会报以下错误:

1
2
3
4
[root@d54753fe138a /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key

执行以下命令解决之:

1
2
3
[root@d54753fe138a /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@d54753fe138a /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@d54753fe138a /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''

修改sshd配置,执行vi /etc/ssh/sshd_config。

  • UsePAM yes 改为 UsePAM no;
  • UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no。

执行passwd给root用户设置登录密码。

查看系统ip:

|1
2
3|[root@d54753fe138a ~]# ip addr | grep eth0
6: eth0@if7: ST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0| | ----- | ----- |

至此,远程SSH需要的设置均已完成,下面来试试远程登录。

镜像与容器

Docker中镜像与容器的关系可以看成是OOP中的类与对象(实例)的关系,一个镜像可以实例化出任意一模一样的容器。为了让刚刚设好的容器可以被复用,而不是每次都pull一次系统镜像再重复设置,我们需要把刚刚的容器保存成镜像,再基于此镜像去“实例化”新容器即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 退出centos系统后,查看当前运行着的容器
~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d54753fe138a centos:7.2.1511 "/bin/bash" About an hour ago Exited (127) 2 seconds ago centos7
# 将容器保存成镜像,local为库名,可自定义
~ docker commit d54753fe138a local/centos7-ssh
sha256:e72dfce8af2ff2fc2de4abc3d6e1858168058f6d075558a20d5b423086a2ae3f
# 本地所有镜像
~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
local/centos7-ssh latest e72dfce8af2f 4 seconds ago 195MB
centos 7.2.1511 0a2bad7da9b5 4 months ago 195MB
# 基于新镜像运行容器
~ docker run -d --name centos7-ssh -p 10033:22 local/centos7-ssh:latest /usr/sbin/sshd -D

参数解析:

  • -p ——将宿主机的10033端口映射到容器的22端口,即sshd默认的端口。
  • /usr/sbin/sshd -D ——执行容器的/usr/sbin/sshd命令,-D将sshd作为前台进程运行,而不是脱离控制台成为后台守护进程。
1
2
3
4
5
6
7
# 查看端口映射情况
~ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d54753fe138a local/centos7-ssh:latest "/usr/sbin/sshd -D" 3 hours ago Up 2 hours 0.0.0.0:10033->22/tcp centos7-ssh
~ docker port d54753fe138a
22/tcp -> 0.0.0.0:10033

从宿主机连接容器

宿主机为Mac

1ssh root@localhost -p 10033

也可以先用ifconfig查看本机ip后用本机ip替换上面的localhost。

宿主机为Linux

1
2
#ip为前面的步骤中查看的容器的ip
ssh root@172.17.0.2 -p 10022

# Macopen in new window # 工具软件open in new window # Dockeropen in new window # Linuxopen in new window

Docker安装和基本使用open in new window

Java中的锁open in new window

  • 文章目录
  • 站点概览
  1. 1. 涉及到的软件open in new window
  2. 2. 安装运行open in new window
  3. 3. 配置open in new window
Last Updated:
Contributors: 刘荣杰