环境:CentOS 6.5 64位
https://registry.hub.docker.com/
https://www.gitbook.com/book/yeasy/docker_practice/details Docker —— 从入门到实践
docker安装 # yum install docker-io 最新为1.5的版本
如果没有安源,先安装http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# service docker restart # ps aux|grep docker |grep -v grep root 2470 1.4 2.0 365780 15640 pts/0 Sl 17:17 0:00 /usr/bin/docker -d # docker --help #查看docker的命令 # docker info # docker version Client version: 1.5.0 Client API version: 1.17 Go version (client): go1.3.3 Git commit (client): a8a31ef/1.5.0 OS/Arch (client): linux/amd64 Server version: 1.5.0 Server API version: 1.17 Go version (server): go1.3.3 Git commit (server): a8a31ef/1.5.0
安装一个centos容器
# docker search ubuntu #搜索ubuntu镜像 [[email protected] ~]# docker search centos #搜索centos镜像 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 1024 [OK] ansible/centos7-ansible Ansible on Centos7 42 [OK] tutum/centos Centos image with SSH access. For the root... 13 [OK] blalor/centos Bare-bones base CentOS 6.5 image 9 [OK] [[email protected] ~]# docker pull tutum/centos #拉取一个docker镜像到本地 Pulling repository tutum/centos 8daafe0f270b: Download complete 6941bfcbbfca: Download complete 41459f052977: Download complete fd44297e2ddb: Download complete e22c88592c50: Download complete ffce4358d6e4: Download complete 5f0574f254a5: Download complete 77ad887d94d4: Download complete 009a97f615dc: Download complete 1d9e8ea240cb: Download complete 1f071815e864: Download complete Status: Downloaded newer image for tutum/centos:latest [[email protected] ~]# docker images #查看镜像,这样就可以了 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE tutum/centos latest 8daafe0f270b 13 days ago 292.2 MB
下面说docker容器的运行,ssh登录进去
[[email protected] ~]# docker run -it tutum/centos /bin/bash #开始运行一个docker [[email protected] /]# ls #根目录下有个run.sh,是开启ssh服务的 bin etc lib lost+found mnt proc run sbin srv tmp var dev home lib64 media opt root run.sh set_root_pw.sh sys usr [[email protected] /]# cat run.sh #!/bin/bash if [ "${AUTHORIZED_KEYS}" != "**None**" ]; then echo "=> Found authorized keys" mkdir -p /root/.ssh chmod 700 /root/.ssh touch /root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys IFS=$‘\n‘ arr=$(echo ${AUTHORIZED_KEYS} | tr "," "\n") for x in $arr do x=$(echo $x |sed -e ‘s/^ *//‘ -e ‘s/ *$//‘) cat /root/.ssh/authorized_keys | grep "$x" >/dev/null 2>&1 if [ $? -ne 0 ]; then echo "=> Adding public key to /root/.ssh/authorized_keys: $x" echo "$x" >> /root/.ssh/authorized_keys fi done fi if [ ! -f /.root_pw_set ]; then /set_root_pw.sh fi exec /usr/sbin/sshd -D [[email protected] /]# cat set_root_pw.sh #!/bin/bash if [ -f /.root_pw_set ]; then echo "Root password already set!" exit 0 fi PASS=${ROOT_PASS:-$(pwgen -s 12 1)} _word=$( [ ${ROOT_PASS} ] && echo "preset" || echo "random" ) echo "=> Setting a ${_word} password to the root user" echo "root:$PASS" | chpasswd echo "=> Done!" touch /.root_pw_set echo "========================================================================" echo "You can now connect to this CentOS container via SSH using:" echo "" echo " ssh -p <port> [email protected]<host>" echo "and enter the root password ‘$PASS‘ when prompted" echo "" echo "Please remember to change the above password as soon as possible!" echo "========================================================================" #需要对ssh配置做如下改动 [[email protected] /]# sed -ri ‘s/UsePAM yes/#UsePAM yes/g‘ /etc/ssh/sshd_config [[email protected] /]# sed -ri ‘s/#UsePAM no/UsePAM no/g‘ /etc/ssh/sshd_config #先不要退出
#这时候需要新开或复制一下这个ssh,进行下面操作:
[[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87d9ae33bd8b tutum/centos:latest "/bin/bash" About a minute ago Up About a minute 22/tcp elegant_hodgkin [[email protected] ~]# docker commit 87d9ae33bd8b new_centos aaf36507c751aed68eb2589136cc5ade1b6f062a18fe00733a10afa3780e5c91 [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE new_centos latest aaf36507c751 6 seconds ago 292.2 MB [[email protected] ~]# docker run -d -p 0.0.0.0:2222:22 new_centos /run.sh 683198a36fcbb8d95cbe63d70c0daa1dc81d0bce9ebd304b731e20aef77cb0ff [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 683198a36fcb new_centos:latest "/run.sh" 3 seconds ago Up 1 seconds 0.0.0.0:2222->22/tcp reverent_jang [[email protected] ~]# docker logs 683198a36fcb 2>&1 |grep ‘^and enter‘ and enter the root password ‘I2ihX1Fj4Aq2‘ when prompted [[email protected] ~]# ssh -p 2222 [email protected] [email protected]‘s password: #密码即是上面写的‘I2ihX1Fj4Aq2‘ [[email protected] ~]# ls / bin etc lib lost+found mnt proc run sbin srv tmp var dev home lib64 media opt root run.sh set_root_pw.sh sys usr
为什么会有些麻烦?
原因在于如果你是Ubuntu的系统,直接就可以使用了;然而centos的系统需要在ssh配置里面把“UsePAM yes”禁用才可以ssh登录,刚开始一直卡到这里:
[[email protected] ~]# ssh -p 2222 [email protected]
[email protected]‘s password:
Connection to localhost closed.
也可以先下载centos的镜像包,这样就不会出现问题:
# wget https://github.com/tutumcloud/tutum-centos/archive/master.zip # unzip tutum-centos-master.zip # cd tutum-centos-master # docker build -t tutum/centos:centos6 centos6
#给docker的内存设置为最大100M,cpu使用率不超过50%,本地40000端口映射到docker的22端口 [[email protected] ~]# docker run -m 100m -c 512 -d -p 40000:22 new_centos /run.sh 566d0985af12d1a8aadfc39e94cf826a053347f1bec4084ee4b81476dcf4e7d1 [[email protected] ~]# docker ps #查看运行的docker CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe10324ba99ca centos:centos6 "/bin/bash" 38 minutes ago Up 38 minutes elegant_lalande
docker容器安装nginx
ssh进入容器后,安装一个nginx
# yum install nginx -y # /usr/sbin/nginx # ps aux|grep nginx root 154 0.0 0.2 109392 2136 ? Ss 03:28 0:00 nginx: master process /usr/sbin/nginx nginx 155 0.0 0.3 109812 3000 ? S 03:28 0:00 nginx: worker process
到这里,大家应该就明白以后怎么继续做了,具体怎么配置docker的容器,生成自己定制的一套环境,大家各自大显身手,这篇是希望通过这些步骤明白docker具体的操作方法
tutum/centos 这个镜像也可以这么玩,先设定密码或者私钥:
docker run -d -p 0.0.0.0:2222:22 -e ROOT_PASS="mypass" tutum/centos
docker run -d -p 2222:22 -e AUTHORIZED_KEYS="`cat ~/.ssh/id_rsa.pub`" tutum/centos
docker的常用命令
# docker pull <镜像名:tag> #拉取一个镜像 # docker images #查看images # docker run -i -t image_name /bin/bash #交互式进入容器 # docker ps #查看运行的docker # docker logs <镜像名:tag> #查看容器的logs # docker start/stop/kill <镜像名:tag> # docker rm $(docker ps -a -q) #删除所有容器 # docker rmi $(docker images | grep none | awk ‘{print $3}‘ | sort -r) #删除所有镜像 # docker build -t <镜像名> <Dockerfile路径> #构建自己的镜像 # docker cp 683198a36fcb:/run.sh . #从docker复制一个文件到当前目录 # docker save new_centos:latest > net_centos.tar #保存镜像 # docker load < net_centos.tar #载入镜像 # docker run --help #更多的命令自己查看一下,相当丰富
http://yeasy.gitbooks.io/docker_practice/content/appendix_command/index.html
更精彩的docker教程,在http://www.osstep.com/course/32 PHP沙盒基于docker的自动化实现