docker安装centos镜像
1、查找镜像源
$ docker search centos
NAME DESCRIPTION STARS OFFICIAL
centos The official build of CentOS. 3857 [OK]
2、下载镜像
docker pull centos
3、查看已下载的镜像
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 3fa822599e10 6 days ago 204MB
4、启动镜像
$ docker run -itd centos /bin/bash
bce6d9a692b26fdf5f7642303c26ffdcaf26917cbfde703dea5c152c320f375d
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
bce6d9a692b2 centos "/bin/bash" About a minute ago Up About a minute
5、进入centos容器
$ docker attach bce6d9a692b2
[root@bce6d9a692b2 /]#
[root@bce6d9a692b2 /]#
6、centos默认没有ifconfig命令,配置ifconfig
[root@bce6d9a692b2 /]# ifconfig
bash: ifconfig: command not found
[root@bce6d9a692b2 /]#
[root@bce6d9a692b2 /]# yum search ifconfig
Loaded plugins: fastestmirror, ovl
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 130 kB 00:00:01
(2/4): base/7/x86_64/group_gz | 156 kB 00:00:01
(3/4): updates/7/x86_64/primary_db | 3.8 MB 00:00:17
(4/4): base/7/x86_64/primary_db | 5.7 MB 00:00:21
Determining fastest mirrors
* base: centos.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: mirrors.aliyun.com
==================================================== Matched: ifconfig ==================================================
net-tools.x86_64 : Basic networking tools
[root@bce6d9a692b2 /]#
[root@bce6d9a692b2 /]#
[root@bce6d9a692b2 /]# yum install net-tools.x86_64
[root@bce6d9a692b2 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 7560 bytes 11081500 (10.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5595 bytes 305703 (298.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
7、配置ssh
a、修改Centos root密码
#
passwd
Changing password
for
user root.New password:
123456
BAD PASSWORD: The password is shorter than
8
charactersRetype new password:
123456
passwd
: all authentication tokens updated successfully.
b、安装openssh
yum
install
openssh-server -y
c、生成公钥、私钥
[root@378ab88a06c8 /]#
ssh-keygen
-t rsa -f /etc/
ssh
/ssh_host_rsa_keyGenerating public/private rsa key pair.Enter passphrase (empty
for
no passphrase): (直接回车)Enter same passphrase again: (直接回车)Your identification has been saved
in
/etc/
ssh
/ssh_host_rsa_key.Your public key has been saved
in
/etc/
ssh
/ssh_host_rsa_key.pub.The key fingerprint is:
33
:3c:
34
:
49
:e4:
76
:7d:
45
:
cc
:
69
:ac:
46
:
85
:ab:
27
:9e root@378ab88a06c8The key
's randomart image is:
+--[ RSA
2048
]----+| .o +=+|| o . . o =o|| * . o = || + o = || S o || + o . || . + || E || |+-----------------+
[root@378ab88a06c8 /]#
ssh-keygen
-t rsa -f /etc/
ssh
/ssh_host_ecdsa_keyGenerating public/private rsa key pair.Enter passphrase (empty
for
no passphrase): (直接回车)Enter same passphrase again: (直接回车)Your identification has been saved
in
/etc/
ssh
/ssh_host_ecdsa_key.Your public key has been saved
in
/etc/
ssh
/ssh_host_ecdsa_key.pub.The key fingerprint is:
09
:ac:b0:
61
:
55
:de:e8:4f:5e:
20
:d9:fc:1e:b6:d7:
79
root@378ab88a06c8The key
's randomart image is:
+--[ RSA
2048
]----+| ... || . o * || + B = || . + o o + || . . . S = || + + o . . || o o . o E|| . . || |+-----------------+
[root@378ab88a06c8 /]#
ssh-keygen
-t rsa -f /etc/
ssh
/ssh_host_ed25519_keyGenerating public/private rsa key pair.Enter passphrase (empty
for
no passphrase): (直接回车)Enter same passphrase again: (直接回车)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:
63
:0d:b5:fb:
55
:a4:
56
:
47
:
43
:6d:
68
:c0:
47
:2e:
84
:
24
root@378ab88a06c8The key
's randomart image is:
+--[ RSA
2048
]----+| E.ooooo=*|| o.o..++=|| . . .o+..|| o . o . || S o . || . . . . || . || || |+-----------------+
d、编写启动脚本
#
vi
/run.
sh
#!/bin/bash/usr/sbin/sshd -D#
chmod
+x /run.
sh
e、退出容器,保存镜像
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
bce6d9a692b2 centos "/bin/bash" About a minute ago Up About a minute
#从容器退出,但不停止容器: Ctrl+P+Q
[root@default ~]#
[root@default ~]#
$
$ docker ps
#回到Docker下面,停止容器: docker stop <容器ID>
$ docker stop bce6d9a692b2
bce6d9a692b2
#提交当前容器到镜像: docker commit <容器ID> <NAME:VERSION>
$ docker commit bce6d9a692b2 centos_me:v1.0
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_me v1.0 4569dd302889 38 minutes ago 366MB
f、重新启动容器
$ docker run --net=host --name hdoop0 -d -p 5001:22 centos_me:v1.0 /run.sh
备注:1、“-p 5001:22” 是将容器的ssh端口22映射都宿主主机的5001端口上
2、‘--name hdoop0
’重命名容器名
或者:docker run --name hadoop2 -d -p 5002:22 centos_me:v1.0 /usr/sbin/sshd -D