部署维护docker环境的详细教程
1,安装环境说明
系统环境:centos6.6
服务应用了: haproxy confd etcd docker
主机名 ip 服务角色
dockerha-152 192.168.36.152 haproxy confd
dockeretcd-153 192.168.36.153 etcd
dockermain-154 192.168.36.154 docker
2,安装依懒包,关闭冲突的服务
官方文档要求linux kernel至少3.8以上,且docker只能运行在64位的系统中。由于rhel6和centos6的内核版本为2.6,因此必须要先升级内核。
cd /etc/yum.repos.d wget http://www.hop5.in/yum/el6/hop5.repo yum install kernel-ml-aufs kernel-ml-aufs-devel -y
修改grub的主配置文件/etc/grub.conf,设置default=0,表示第一个title下的内容为默认启动的kernel(一般新安装的内核在第一个位置)。
然后重启,重启系统,这时候你的内核就成功升级了,版本应该在3.8以上了,
uname -r [root@dockermain-154 shell]# uname -r
3.10.5-3.el6.x86_64
sed -i '/^selinux=/c\selinux=disabled' /etc/selinux/config setenforce 0
在fedora epel源中已经提供了docker-io包,下载安装epel:
rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm sed -i 's/^mirrorlist=https/mirrorlist=http/' /etc/yum.repos.d/epel.repo
3,安装组件
(1)haproxy confd
dockerha-152上操作
1、haproxy
# yum –y install haproxy
2、confd
# wget https://github.com/kelseyhightower/confd/releases/download/v0.6.3/confd-0.6.3-linux-amd64 # mv confd /usr/local/bin/confd # chmod +x /usr/local/bin/confd # /usr/local/bin/confd -version
(2)etcd
dockeretcd-153上操作
# mkdir -p /home/install && cd /home/install # wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz # tar -zxvf etcd-v0.4.6-linux-amd64.tar.gz # cd etcd-v0.4.6-linux-amd64 # cp etcd* /bin/ # /bin/etcd -version etcd version 0.4.6
(3)docker
# yum -y install docker-io # service docker start # chkconfig docker on
4,docker命令使用
(1)查看帮助
docker command --help
(2)搜索可用的境像
docker search name 如示例:
[root@dockermain-154 src]# docker search centos name description stars official automated centos the official build of centos. 817 [ok] ansible/centos7-ansible ansible on centos7 30 [ok] tutum/centos centos image with ssh access. for the root... 13 [ok] jdeathe/centos-ssh-apache-php centos-6 6.5 x86_64 / apache / php / php m... 8 [ok] blalor/centos bare-bones base centos 6.5 image 8 [ok] jprjr/centos-php-fpm 6 [ok] steeef/graphite-centos centos 6.x with graphite and carbon via ng... 6 [ok] tutum/centos-6.4 deprecated. use tutum/centos:6.4 instead. ... 5 [ok] layerworx/centos a general centos 6 image with the epel6 an... 2 [ok] jr42/chef-solo-centos official centos base images with current c... 1 [ok] million12/centos-supervisor base centos-7 with supervisord launcher, h... 1 [ok] internavenue/centos-percona centos-based percona image. 1 [ok] jdeathe/centos-ssh centos-6 6.5 x86_64 / epel repo. / openssh... 1 [ok] jdeathe/centos-ssh-mysql centos-6 6.5 x86_64 / mysql. image include... 1 [ok] yajo/centos-epel centos with epel and fully updated 1 [ok] nimmis/java-centos this is docker images of centos 7 with dif... 0 [ok] lighthopper/orientdb-centos a dockerfile for creating an orientdb imag... 0 [ok] bbrietzke/centos-starter centos 7 with epel and supervisord 0 [ok] tcnksm/centos-node dockerfile for centos packaging node 0 [ok] insaneworks/centos centos 6.5 x86_64 + @update 0 [ok] snowyday/centos provide for emacs and ricty font on x11 en... 0 [ok] dmglab/centos centos with superpowers! 0 [ok] akroh/centos centos 6 container that has been updated w... 0 [ok] timhughes/centos centos with systemd installed and running 0 [ok] solict/provisionous-puppet-centos centos provisions with puppet included 0 [ok]
(3)下载镜像
注意镜像名称要写全,就是用docker search name 搜出来的name列的名称。
docker pull name 如示例:
[root@dockermain-154 src]# docker pull jdeathe/centos-ssh-apache-php pulling repository jdeathe/centos-ssh-apache-php ........ 62203f428b1f: download complete e1812755a4ca: download complete 0910edda3736: download complete status: downloaded newer image for jdeathe/centos-ssh-apache-php:latest
(4)查看已安装的镜像
[root@dockermain-154 src]# docker images repository tag image id created virtual size centos latest dade6cb4530a 10 days ago 210.1 mb jdeathe/centos-ssh-apache-php latest f1a489312a4a 3 months ago 297.7 mb
(5)docker容器中运行命令
docker run命令有两个参数,一个是镜像名,一个是要在镜像中运行的命令。 注意:image=repository[:tag],如果image参数不指定镜像的tag,默认tag为latest。
[root@dockermain-154 run]# sudo docker run jdeathe/centos-ssh-apache-php echo 'hello world!' hello world! [root@dockermain-154 run]# sudo docker run jdeathe/centos-ssh-apache-php hostname db7e1d2269fb
(6)列出容器
查看最近生成的容器:docker ps -l
查看正在运行的容器:docker ps
[root@dockermain-154 run]# docker ps -l container id image command created status ports names db7e1d2269fb jdeathe/centos-ssh-apache-php:latest "hostname" 27 seconds ago exited (0) 26 seconds ago adoring_babbage
(7)显示容器标准输出
[root@dockermain-154 run]# docker ps -l container id image command created status ports names db7e1d2269fb jdeathe/centos-ssh-apache-php:latest "hostname" 27 seconds ago exited (0) 26 seconds ago adoring_babbage [root@dockermain-154 run]# docker logs db7e1d2269fb db7e1d2269fb
(8)给容器中安装程序或服务
[root@dockermain-154 run]# sudo docker run centos yum install -y httpd loaded plugins: fastestmirror ..........
dependency installed:
apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 centos-logos.noarch 0:70.0.6-1.el7.centos httpd-tools.x86_64 0:2.4.6-19.el7.centos mailcap.noarch 0:2.1.41-2.el7
failed:
httpd.x86_64 0:2.4.6-19.el7.centos
complete!
(9)保存对容器中的修改并生成新的镜像
docker commit containerid [repository[:tag]]
repository参数可以是新的镜像名字,也可以是旧的镜像名;如果和旧的镜像名和tag都相同,会覆盖掉旧的镜像。
[root@dockermain-154 ~]# docker commit bd7cc4f4ac92 centos:httpd 1e0915f3247b86414ebc11fd994fc6abfb590ff3b1ab890949c845ee88b2d9f4 [root@dockermain-154 ~]# docker images repository tag image id created virtual size centos httpd 1e0915f3247b 9 seconds ago 320.8 mb centos latest dade6cb4530a 10 days ago 210.1 mb jdeathe/centos-ssh-apache-php latest f1a489312a4a 3 months ago 297.7 mb
(10)停止正在运行的容器
(11)查看容器或镜像详情
下面是我刚保存提交产生的新的容器
[root@dockermain-154 ~]# docker inspect 943e45b6e5f3 [{ "apparmorprofile": "", "args": [], "config": { "attachstderr": true, "attachstdin": true, "attachstdout": true, "cmd": [ "/bin/bash" ], "cpushares": 0, "cpuset": "", "domainname": "", "entrypoint": null, "env": [ "path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "exposedports": null, "hostname": "943e45b6e5f3", "image": "centos:httpd", "macaddress": "", "memory": 0, "memoryswap": 0, "networkdisabled": false, "onbuild": null, "openstdin": true, "portspecs": null, "stdinonce": true, "tty": true, "user": "", "volumes": null, "workingdir": "" },
(12)删除容器
docker rm containerid
查看所有容器id:docker ps -a -q
删除所有的容器:docker rm $(docker ps -a -q)
(13)删除镜像
docker rmi image
(14)查看docker的信息,包括containers和images数目、kernel版本等
[root@dockermain-154 ~]# docker info containers: 14 images: 56 storage driver: aufs root dir: /var/lib/docker/aufs dirs: 89 execution driver: native-0.2 kernel version: 3.10.5-3.el6.x86_64 operating system: <unknown> cpus: 1 total memory: 989.6 mib name: dockermain-154 id: w4pw:w3xr:fqze:sbaa:2ds2:bm6n:dv5b:arf2:3szm:xgst:5zf7:dfzv warning: no swap limit support
(15)创建容器,并像平常ssh登录一样使用
[root@dockermain-154 ~]# docker run -i -t centos /bin/bash [root@7c0414d03fe7 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin selinux srv sys tmp usr var
以上所述是小编给大家介绍的部署维护docker环境的详细教程,希望对大家有所帮助