pqwsa 发表于 2018-5-30 08:05:07

1.7.0版本docker安装与使用

  服务器是ubuntu server 14.04,内核:3.13.0-32-generic,硬件是dell R520
  一、安装

  方法一:

  通过系统自带包安装
  Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装。
  $ sudo apt-get update
  $ sudo apt-get install -y docker.io
  $ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
  $ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
  

  方法二:
  通过Docker源安装最新版本
  要想安装最新版本的Docker需要使用Docker源来安装
  # apt-get -y install apt-transport-https
  # apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
  # bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
  # apt-get update
  # apt-get -y install lxc-docker
  # docker -v   //查看docker版本
  # ps -ef | grep docker
  root   17311   10 Feb13 ?      00:00:43 /usr/bin/docker -d
  

  2、下载tar包并加入镜像里
  一般下载镜像的时候,都是先docker search image_name,然后docker pull image_name
但由于最近GFW屏蔽了网络,在现在的时候会出现以下错误,根本pull不了镜像。

Pull ingrepository centos
  2014/05/19 13:35:11 Gethttps://cdn-registry-1.docker.io/v1/repositories/library/centos/tags: read tcp162.159.253.251:443: connection timed out
  

  所以为了解决此问题,我就从别的地方下载了打包好的tar(后边会解释然后自己打包的),然后使用docker load导入先下载(有centos与ubuntu)
  wget http://docker.widuu.com/ubuntu.tar

  
  wget http://docker.widuu.com/centos.tar

  加入到镜像里
#docker load -i centos.tar
#docker load -i ubuntu.tar
  查看镜像列表
  #docker images
REPOSITORY      TAG               IMAGE ID            CREATED             VIRTUAL SIZE
<none>            <none>            607347d2a946      3 months ago      300.2 MB
ubuntu/widuu         latest               963b9d0e10ba      3 monthsago         155 MB
  给centos的改个名
  #docker tag 607 centos:latest
#docker images
REPOSITORY         TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos             latest            607347d2a946      3 months ago      300.2 MB
ubuntu/widuu            latest            963b9d0e10ba      3 monthsago         155 MB
  测试镜像是否可用
  #docker run centos /bin/echo "hello,i'm centos system"
hello,i'mcentos system
#docker run ubuntu/widuu /bin/echo "hello,i'm ubuntu system"
hello,i'mubuntu system
  使用交换模式
  #docker run -i -t centos /bin/bash
bash-4.1#ifconfig
eth0      Link encap:EthernetHWaddr BA:08:86:7F:F8:48
          inet addr:172.17.0.4Bcast:0.0.0.0 Mask:255.255.0.0
          inet6 addr: fe80::b808:86ff:fe7f:f848/64Scope:Link
          UP BROADCAST RUNNINGMTU:1500 Metric:1
          RX packets:6 errors:0 dropped:2overruns:0 frame:0
          TX packets:2 errors:0 dropped:0overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:488 (488.0 b)TX bytes:168 (168.0 b)

lo      Link encap:Local Loopback
          inet addr:127.0.0.1Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNINGMTU:1500 Metric:1
          RX packets:0 errors:0 dropped:0overruns:0 frame:0
          TX packets:0 errors:0 dropped:0overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)TX bytes:0 (0.0 b)

bash-4.1# exit
  退出有2种方式,一种是完全退出,使用exit;另外一中是不完全退出,使用ctrl-p与ctrl-q
  这样你不是完全退出了,但容器状态还是存在。
  可用使用docker attach CONTAINER ID来重新进入。
  如果你是完全退出了,docker容器状态显示Exited,需要重新启动docker容器,在使用attach进入.
  docker start CONTAINER ID
  docker attach CONTAINER ID
  

  在宿主机ubuntu上面测试使用ssh进行连接docker容器
  # docker run -i -t centos /bin/bash
bash-4.1#ifconfig
eth0      Link encap:EthernetHWaddr BA:08:86:7F:F8:48
          inet addr:172.17.0.4Bcast:0.0.0.0 Mask:255.255.0.0
          inet6 addr: fe80::b808:86ff:fe7f:f848/64Scope:Link
          UP BROADCAST RUNNINGMTU:1500 Metric:1
          RX packets:6 errors:0 dropped:2overruns:0 frame:0
          TX packets:2 errors:0 dropped:0overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:488 (488.0 b)TX bytes:168 (168.0 b)
  为docker容器里面root修改密码
  bash-4.1# passwd root
  New password:
/usr/share/cracklib/pw_dict.pwd: No such file or directory
PWOpen: No such file or directory
  解决方法:
  bash-4.1# rpm -e cracklib-dicts --nodeps
bash-4.1# rpm -e pam --nodeps
  bash-4.1# yum -y install cracklib-dicts pam
  bash-4.1# passwd root 就可以成功了。
  bash-4.1# service sshd start
  Starting sshd:                                             
bash-4.1# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp      0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp      0      0 :::22                     :::*                        LISTEN      
  在docker容器上面上面进行ssh连接报下面错误

  # ssh -l root 172.17.0.4
The authenticity of host '172.17.0.4 (172.17.0.4)' can't be established.
RSA key fingerprint is 9f:10:e8:9e:7c:a3:45:4e:ef:d0:19:f0:11:46:43:4e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.4' (RSA) to the list of known hosts.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

  解决方法:
  bash-4.1# vim /etc/ssh/sshd_config
  将PermitRootLogin no改成 PermitRootLogin yes
  RSAAuthentication yes
  PubkeyAuthentication yes
  AuthorizedKeysFile   .ssh/authorized_keys

  bash-4.1#service sshd restart
  bash-4.1#setenforce 0

  bash-4.1# ssh -l root 172.17.0.4
  Last login: Fri Jun 26 07:09:47 2015 from 172.17.0.4
#
  发现登录成功。

  

  

  二、私有库

  由于GFW,所以玩docker没办法pull与push,并且为了安全考虑,为了解决就搭建了私有库。
  Docker 官方提供了 docker registry 的构建方法:https://github.com/docker/docker-registry
  

  方法一,快速构建
  快速构建 docker registry 通过以下两步:

[*]  安装 docker
[*]  运行 registry: docker run -p 5000:5000 registry
  这种方法通过 Docker hub 使用官方镜像 https://registry.hub.docker.com/_/registry/
  

  方法二,不使用容器构建registry

  
安装必要的软件
# apt-get install build-essential python-dev libevent-dev python-pip liblzma-dev gunicornpython-dev -y配置 docker-registry
# pip install docker-registry或者 使用 github clone 手动安装
$ git clone https://github.com/dotcloud/docker-registry.git
$ cd docker-registry/
$ cp config/config_sample.yml config/config.yml
$ mkdir /data/registry -p
$ pip install .运行
# gunicorn -k gevent --max-requests 100 --graceful-timeout 3600 -t 3600 -b localhost:5000 -w 8 -D --access-logfile /tmp/gunicorn.logdocker_registry.wsgi:application  

  客户端推送镜像到私有库
  1、先注册账号
  # docker login localhost:5000
依次输入你的账号、密码、email

  2、给提交的镜像打标签
# docker ps
CONTAINER ID      IMAGE                COMMAND             CREATED             STATUS            PORTS               NAMES
504d16302ad4      
jdeathe/centos-ssh   "/bin/bash"         About an hour ago   Up 42
minutes       22/tcp            serene_bardeen
# docker commit 504d16302ad4 centos:v1
b47276971c2db84bd76659da86a4ea5bda227f008c5004152232183066f20533
# docker images
REPOSITORY         TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos               v1                  b47276971c2d      7 seconds ago       376.8 MB
jdeathe/centos-ssh   latest            b071db8f6e23      4 weeks ago         238 MB
  3、推送到私有库
  # docker push localhost:5000/centos
The pushrefers to a repository (len: 1)
Sendingimage list
Pushingrepository localhost:5000/centos (1 tags)
Image384630bcda7c already pushed, skipping
Image607347d2a946 already pushed, skipping
5abf7cce3767:Image successfully pushed
Pushingtag for rev on{http://localhost:5000/v1/repositories/centos/tags/latest}
  

  # docker images
REPOSITORY            TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos                  v1                  b47276971c2d      About an hour ago   376.8 MB
localhost:5000/centos   latest            b47276971c2d      About an hour ago   376.8 MB
jdeathe/centos-ssh      latest            b071db8f6e23      4 weeks ago         238 MB

  


  http://docker.widuu.com/
  http://aresy.blog.51cto.com/5100031/1553624
页: [1]
查看完整版本: 1.7.0版本docker安装与使用