Docker 安装Centos7
背景:我需要三台Centos7来部署分布式系统,但是只有一台Centos7,故想用Docker虚拟三台出来1、下载Centos7镜像,通过docker search centos7查看镜像列表,用第一个。 123456789101112131415[root@free-share local]# docker pull docker.io/ansible/centos7-ansibleUsing
背景:我需要三台Centos7来部署分布式系统,但是只有一台Centos7,故想用Docker虚拟三台出来
1、下载Centos7镜像,通过docker search centos7查看镜像列表,用第一个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@ free -share local ] # docker pull docker.io/ansible/centos7-ansible Using default tag: latest Trying to pull repository docker.io /ansible/centos7-ansible ... latest: Pulling from docker.io /ansible/centos7-ansible 45a2e645736c: Pull complete 1c3acf573616: Pull complete edcb61e55ccc: Pull complete cbae31bad30a: Pull complete aacbdb1e2a62: Pull complete fdeea4fb835c: Pull complete Digest: sha256:39eff7d56b96530d014083cd343f7314c23acbd1ecf37eb75a71a2f6584d0b02 Status: Downloaded newer image for docker.io /ansible/centos7-ansible :latest [root@ free -share local ] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io /ansible/centos7-ansible latest 688353a31fde 5 years ago 447 MB |
2、使用镜像创建容器
1 2 3 4 5 | [root@ free -share local ] # docker run -itd -p 6022:22 --name=vm01 -v /bodata:/bodata -h vm01 --privileged=true 688353a31fde 1a055acb4e761a61775667a3193c811b5327ef0fc2aeb77e33d1fb325474fe32 [root@ free -share local ] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7ec547fc37e5 688353a31fde "/bin/bash" 31 seconds ago Up 27 seconds 0.0.0.0:6022->22 /tcp vm01 |
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-d:后台运行容器,并返回容器ID;
-p:指定端口映射,格式为:主机(宿主)端口:容器端口
--name:为容器指定一个名称;
-h:为容器指定hostname
-v:将宿主机的目录挂载在容器中,格式为:主机(宿主)目录:容器目录,且不存在时会自行创建
3、此时使用ssh工具连ip:6022是不通的,要进入容器安装一些基础工具(sshd等网络工具)后才可以用ssh连接。
1 2 3 4 5 6 | [root@ free -share bodata] # docker exec -it 7ec547fc37e5 /bin/bash [root@vm01 ~] # yum install -y openssh-server [root@vm01 ~] # yum install -y net-tools [root@vm01 ~] # ssh-keygen -A ##为ssh生成必要的秘钥等 [root@vm01 ~] # /usr/sbin/sshd ##启动ssh后台服务 [root@vm01 ~] # echo 1|passwd --stdin root ##修改容器root用户密码为1,也可以用passwd root来修改 |
4、用xshell连接ip:6022,连接成功。
1 2 3 4 5 6 | Connecting to 192.168.2.54:6022... Connection established. To escape to local shell, press 'Ctrl+Alt+]' . WARNING! The remote SSH server rejected X11 forwarding request. [root@vm01 ~] # |