当我们装上docker之后,自然会pull一个或多个镜像玩玩,这时候,docker hub仓库上有很多系列操作系统镜像,每个系列又有很多不同功能的虚拟机镜像,比如centos分6还有7,每类镜像又分为带jdk,ssh,php等运行环境的镜像,下载镜像有一个问题,我们通过docker pull osimg[:tag]来获取镜像,默认是从国外的网站来拉取,这个速度会很慢,可以使用由DaoCloud提供的加速器,配置一个Registry Mirror,这时候拉取速度会成赔提升。
我们知道docker容器运行起来之后,就好比一个虚拟机,我们就想通过使用普通虚拟机一样来使用它,比如ssh远程登录,安装软件,甚至会在上面通过源码编译安装,这时候就需要系统能够有make命令,又比如需要通过ifconfig查看IP,netstat查看系统开启的端口2181,22,27017,3306,6379,80等,而我们pull的系统很有可能是一个裸系统,上面出了bash,ls,cd等基础的命令之外,其余的命令都没有,就像ubuntu:xenial一样,ifconfig,netstat等都无法使用,这里自己亲自测试,如何熟练使用docker来玩转ubuntu裸系统,包括更新源,安装软件,如何进出容器,启动ssh服务,通过外部ssh来访问虚拟机,保存容器的更改等。
我的docker容器ubuntu:xenial
1)更改国内源sohu,163,aliyun等都可以vim /etc/apt/sources.list
:1,$s/archive.ubuntu.com/mirros.163.com/g
:wq
apt-get update
apt-get upgrade
2)解决vi不兼容的问题
apt-get remove vim-common
apt-get install vim
3)安装gcc编译工具apt-get install build-essential //gcc gcc-5 g++ g++-5 make
//不要单独安装gcc,否则后面安装make会提示错误
4)安装net-tools,ifconfig,netstat等命令就可以使用了//ifconfig netstat命令无法使用
bash: netstat: command not found
apt-get install net-tools
5)安装或者配置ssh(有的机器上没有安装ssh,需要apt-get install openssh-server来安装)//默认情况下ssh不会启动,这时候使用service ssh start
root@6b0bf8da388a:~# service ssh start这时候通过客户端访问是不会成功的。
* Starting OpenBSD Secure Shell server sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
[ OK ]
root@6b0bf8da388a:~# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
[root@client init.d]# ssh root@172.17.0.2
Read from socket failed: Connection reset by peer
//解决办法
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key //一路回车
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key //同上
root@6b0bf8da388a:~# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
SHA256:4SyV0bkawtM5GUc/CFtkq0M6maIdtEv+zocvnf2gEdM root@6b0bf8da388a
The key's randomart image is:
+---[RSA 2048]----+
| o+=. |
| .B++ |
| .. .*=o.o |
| . .+O*+. . |
| = *oS+E |
| = + o.+ |
| . + o.o. |
| oo +o.. |
| .++o .. |
+----[SHA256]-----+
root@6b0bf8da388a:~# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
SHA256:7kWoD+7CHDhV/9gMTazXYKkBFIECAVMYHwO/sCzzbqg root@6b0bf8da388a
The key's randomart image is:
+---[DSA 1024]----+
|OB+ o=+ . . |
|.+.o. . . * |
|. o. . . B o |
|.o .. =.o . |
|+..o SB. |
|.oo . o..+ |
| ..+ .o . . |
|... +. + . |
|E.. oo o |
+----[SHA256]-----+
//最后重启ssh服务
service ssh restart
6)通过attach进入容器
能够attach的前提 docker run -t //无需 -i
docker attach redis
7)docker attach进去容器之后退出docker容器不会让容器关机exit
ctrl+p
ctrl+q
ctrl+p,q //或者按住ctrl 先按p,再按q退出
8)docker启动容器启动时开启sshd
[root@client ~]# docker run -d --name redis redis_node /usr/sbin/sshd -D38514e65e8cf1b493e2626c35476ee210677a3af90fc6d2f4f7f6b5ab5e43e51[root@client ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES38514e65e8cf redis_node "/usr/sbin/sshd -D" 12 seconds ago Up 11 seconds redis[root@client ~]# ssh root@172.17.0.2root@172.17.0.2's password:Last login: Fri Mar 24 06:02:40 2017 from 172.17.0.1root@38514e65e8cf:~# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02 inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe11:2/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:47 errors:0 dropped:0 overruns:0 frame:0TX packets:35 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:6877 (6.8 KB) TX bytes:5751 (5.7 KB)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:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@38514e65e8cf:~#
9)docker加载本地镜像
docker load --input tensorflow.tar
或者
docker load < tensorflow.tar
导入带tar.gz格式的镜像文件
[root@client ubuntu]# cat ubuntu-14.04-x86-minimal.tar.gz |docker import - ubuntu:14.04
10)使用python命令,默认直接输入python会提示找不到命令
ln -s /usr/bin/python3 /usr/bin/python //建立一个软链就可以
11)提交对容器做的修改
docker commit container container_other