加入到镜像里
#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:Ethernet HWaddr BA:08:86:7F:F8:48
inet addr:172.17.0.4 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::b808:86ff:fe7f:f848/64Scope:Link
UP BROADCAST RUNNING MTU: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.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU: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:Ethernet HWaddr BA:08:86:7F:F8:48
inet addr:172.17.0.4 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::b808:86ff:fe7f:f848/64Scope:Link
UP BROADCAST RUNNING MTU: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: [ OK ]
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
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 [localhost:5000/centos] (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 [5abf7cce3767] 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