docker虚拟化多个Centos7
# 说明
由于测试环境不够,于是想用docker虚拟几个centos系统出来
# 安装docker
略
# 1.运行一个基础镜像
[root@new2 ~]# docker run --rm -d -i -t centos:centos7.5.1804 /bin/bash
[root@new2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ba3c5252384 centos:centos7.5.1804 "/bin/bash" 12 minutes ago Up 12 minutes inspiring_raman
1
2
3
4
2
3
4
# 2.进入运行的Linux系统
[root@new2 ~]# docker attach 4ba3c5252384
[root@4ba3c5252384 /]# ll
total 0
lrwxrwxrwx 1 root root 7 May 31 2018 bin -> usr/bin
drwxr-xr-x 5 root root 360 Nov 19 07:12 dev
drwxr-xr-x 1 root root 66 Nov 19 07:12 etc
drwxr-xr-x 2 root root 6 Apr 11 2018 home
lrwxrwxrwx 1 root root 7 May 31 2018 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 31 2018 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Apr 11 2018 media
drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x 547 root root 0 Nov 19 07:12 proc
dr-xr-x--- 2 root root 114 May 31 2018 root
drwxr-xr-x 10 root root 130 May 31 2018 run
lrwxrwxrwx 1 root root 8 May 31 2018 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Nov 19 03:36 sys
drwxrwxrwt 7 root root 132 May 31 2018 tmp
drwxr-xr-x 13 root root 155 May 31 2018 usr
drwxr-xr-x 18 root root 238 May 31 2018 var
[root@4ba3c5252384 /]# yum install -y net-tools.x86_64 wget vim-enhanced passwd openssl openssh-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 3.修改虚拟出来的Linux系统用户名密码
[root@4ba3c5252384 /]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
1
2
3
4
5
6
2
3
4
5
6
# 4.启动容器ssh
[root@4ba3c5252384 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@4ba3c5252384 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@4ba3c5252384 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
The key fingerprint is:
SHA256:JNwMr0XzOeTDSQPWCRRMW//UjttKHl3BqDRnwadFdM8 root@4ba3c5252384
The keys randomart image is:
+---[DSA 1024]----+
| .+X==...oo.|
| . B.Xo= .+=o|
| o B Oo.+=oE|
| = .o=+ o.|
| . S . o o|
| +.|
| + o|
| o o |
| o |
+----[SHA256]-----+
[root@4ba3c5252384 /]# vim /etc/ssh/sshd_config
# PermitRootLogin yes
# PasswordAuthentication yes
[root@4ba3c5252384 /]# /usr/sbin/sshd -D
[root@4ba3c5252384 /]# ps -ef |grep sshd
root 102 1 0 07:53 ? 00:00:00 /usr/sbin/sshd
root 104 1 0 07:53 pts/0 00:00:00 grep --color=auto sshd
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
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 5.将当前容器保存为镜像
- 退出,但不停止容器 (有点难按呀。。。。)
Ctrl+P+Q
- 生成镜像
[root@new2 ~]# docker commit 4ba3c5252384 summer/centos7-ssh
sha256:9736e16c354ba3a82a1bf5ec65b33d8dbfa6a132e65a5ae0a31c8fe0e5da513f
[root@new2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
summer/centos7-ssh latest 9736e16c354b 6 seconds ago 346MB
1
2
3
4
5
2
3
4
5
# 6.验证一下
[root@new2 ~]# docker run -itd -p 10222:22 9736e16c354b /bin/bash
744fe36d22d3762e9976b41fb3600442776d9a0f2cbaf5f97baf1329a33c7249
[root@new2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
744fe36d22d3 9736e16c354b "/bin/bash" 6 seconds ago Up 4 seconds 0.0.0.0:10222->22/tcp quizzical_beaver
- 用xshell工具连接容器测试
Connecting to 192.168.0.8:10222...
Connection established.
To escape to local shell, press Ctrl+Alt+].
WARNING! The remote SSH server rejected X11 forwarding request.
[root@744fe36d22d3 ~]# ll
total 4
-rw------- 1 root root 3302 May 31 2018 anaconda-ks.cfg
[root@744fe36d22d3 ~]# cd /
[root@744fe36d22d3 /]# ll
total 0
lrwxrwxrwx 1 root root 7 May 31 2018 bin -> usr/bin
drwxr-xr-x 5 root root 360 Nov 19 08:01 dev
drwxr-xr-x 1 root root 66 Nov 19 08:01 etc
drwxr-xr-x 2 root root 6 Apr 11 2018 home
lrwxrwxrwx 1 root root 7 May 31 2018 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 31 2018 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Apr 11 2018 media
drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x 551 root root 0 Nov 19 08:01 proc
dr-xr-x--- 1 root root 22 Nov 19 07:52 root
drwxr-xr-x 1 root root 34 Nov 19 07:52 run
lrwxrwxrwx 1 root root 8 May 31 2018 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Nov 19 03:36 sys
drwxrwxrwt 1 root root 6 Nov 19 07:49 tmp
drwxr-xr-x 1 root root 81 May 31 2018 usr
drwxr-xr-x 1 root root 17 May 31 2018 var
- 测试查看的ip为创建容器随机的ip
[root@744fe36d22d3 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
RX packets 82 bytes 7997 (7.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 67 bytes 10147 (9.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@744fe36d22d3 /]#
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 7.使用固定ip的方式创建Docker容器
- Docker安装后,默认会创建下面三种网络类型:
[root@new2 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f05274e4383e bridge bridge local
1fc2e68d0f32 easymock_easy-mock bridge local
a1a2e4e0720c host host local
b80f2704c864 none null local
[root@new2 ~]#
1
2
3
4
5
6
7
2
3
4
5
6
7
1. bridge:桥接网络
默认情况下启动Docker容器,都是使用bridge,Docker安装时创建Docker容器重启时,会按照顺序获取对应的IP地址,这个就导致重启下,Docker的IP地址就变了。
2. none:无指定网络
使用–network=none,docker容器就不分配局域网的IP
3. host:主机网络
使用–network=host,此时,Docker容器的网络会附属在主机上,两者是互通的。
- 创建自定义网络,并指定网段:192.168.200.0/24 (注意这里使用的200段,不能和宿主机器所在的网段相同)
[root@new2 ~]# docker network create --subnet=192.168.200.0/24 dockernet
39c96ae62ecdea7a8ea057e3e4c12c71c166f726925b4bc3c71e790359fa1c52
[root@new2 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f05274e4383e bridge bridge local
39c96ae62ecd dockernet bridge local
1fc2e68d0f32 easymock_easy-mock bridge local
a1a2e4e0720c host host local
b80f2704c864 none null local
[root@new2 ~]#
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 使用指定ip启动容器
[root@new2 ~]# docker run --privileged -itd --name dockerOS123 --net dockernet --ip 192.168.200.123 -p 10123:22 summer/centos7-ssh:latest /usr/sbin/sshd -D
0bf84b002ffd50b43bc101a20cd7ba630fbdd08980e5b2061dd241593b08a046
[root@new2 ~]# docker run --privileged -itd --name dockerOS124 --net dockernet --ip 192.168.200.124 -p 10124:22 summer/centos7-ssh:latest /usr/sbin/sshd -D
e7171019510dfa8f8f38e1d4ae934a12eebbaac15848068429a10a1c75a27fb4
[root@new2 ~]# docker run --privileged -itd --name dockerOS125 --net dockernet --ip 192.168.200.125 -p 10125:22 summer/centos7-ssh:latest /usr/sbin/sshd -D
19ff8426d4cca170d919694c39cb943993987304e459738acdc2694006907877
[root@new2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19ff8426d4cc summer/centos7-ssh:latest "/usr/sbin/sshd -D" 9 seconds ago Up 7 seconds 0.0.0.0:10125->22/tcp dockerOS125
e7171019510d summer/centos7-ssh:latest "/usr/sbin/sshd -D" 19 seconds ago Up 17 seconds 0.0.0.0:10124->22/tcp dockerOS124
0bf84b002ffd summer/centos7-ssh:latest "/usr/sbin/sshd -D" 30 seconds ago Up 29 seconds 0.0.0.0:10123->22/tcp dockerOS123
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11