上个笔记中写道在debian里把docker安装完成并运行了hello-world这个神奇的指令,可是具体还能干点啥呐?哈,开始搞起!查看了下docker版本,貌似比网上教程里的都要新一些,也不晓得增加了啥新功能,管他呢,反正还是小白,先搞个东东玩玩再说!
root@instance-1:~# docker version
Client: Docker Engine - Community
Version: 19.03.1
API version: 1.40
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:22:03 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.1
API version: 1.40 (minimum version 1.12)
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:20:35 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.6
GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
runc:
Version: 1.0.0-rc8
GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f
docker-init:
Version: 0.18.0
GitCommit: fec3683
root@instance-1:~# 同样是参考笑虎大佬的文章,先装了个ubuntu,因为对ubuntu比对centos更熟悉一点,指令如下:
只要一小时,零基础入门Docker - 笑虎的文章 https://zhuanlan.zhihu.com/p/23599229
[root@xxx ~]# docker search ubuntu # 查看ubuntu镜像是否存在
[root@xxx ~]# docker pull ubuntu # 利用pull命令获取镜像
[root@xxx ~]# docker images # 查看当前系统中的images信息
root@instance-1:~# docker images
ubuntu latest 3556258649b2 4 days ago 64.2MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
//吼吼,有意思,看到docker三大件里的image了,咋用它呢?
root@instance-1:~# docker run -it ubuntu:latest /bin/bash # 启动一个容器
root@f22588294012:/# ssh 172.17.0.1
bash: ssh: command not found
root@f22588294012:/# ifconfig
bash: ifconfig: command not found
root@f22588294012:/#
//用这个ubuntu的镜像来启动了一个容器,并且这个容器里运行了一个ubuntu linux系统,但是这个linux里没有ssh/ifconfig这个程序呢,没有咋整呢,当然是装一个了!
root@f22588294012:/# apt-get update
root@f22588294012:/# apt-get install -y openssh-server net-tools vim #安装常用的工具
root@f22588294012:/# ssh 172.17.0.1
root@instance-1:~# exit
root@f22588294012:/# # ssh到host成功,那host能不能ssh到ubuntu呢?
root@f22588294012:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.10 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:0a txqueuelen 0 (Ethernet)
RX packets 2169 bytes 44377997 (44.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1350 bytes 111537 (111.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
//按顺序按ctrl+p然后ctrl+q挂起centos的容器回到host
root@instance-1:~# ssh 172.17.0.10
ssh: connect to host 172.17.0.10 port 22: Connection refused
root@instance-1:~#
//哼,ubuntu座位ssh server不听话啊,看来需要再次进到里面配置一下
root@instance-1:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f22588294012 ubuntu:latest "/bin/bash" 15 minutes ago Up 15 minutes sleepy_bhaskara
root@instance-1:~# docker attach f22588294012 #重新进入ubuntu
root@f22588294012:/# service ssh status #原来是sshd 服务没起来
* sshd is not running
root@f22588294012:/# service ssh start #起来试一下看看
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@f22588294012:/# read escape sequence
root@instance-1:~# ssh 172.17.0.10
root@172.17.0.10's password:
Permission denied, please try again. #衰,还是不行,应该是sshd配置有问题2. 给ubuntu配置ssh功能:
上一步已经可以在ubuntu里ssh到host debian了,但是反过来ubuntu座位ssh server却不能被ssh,这样可不行的呀!搞起!
root@instance-1:~# docker attach f22588294012
root@f22588294012:/# passwd #改下root用户的密码
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@f22588294012:/# vi /etc/ssh/sshd_config #这里面总共需要添加如下3行即可
Port 22
PermitRootLogin yes
PasswordAuthentication yes
root@f22588294012:/# service ssh restart #重启ssh服务
root@f22588294012:/# read escape sequence
root@instance-1:~# ssh 172.17.0.10
root@172.17.0.10's password:
Last login: Sat Jul 27 23:02:17 2019 from 172.17.0.1
root@f22588294012:~# #host ssh登录ubuntu成功啦!3. 创建自己独一无二的的镜像文件:
想把已经配置好ssh ifconfig等服务的容器做成镜像以备将来使用呢,这样就下次使用就不用再配这些乱七八糟的东西了,这就需要用到docker commit 指令咯!
root@instance-1:~# docker ps -a 查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f22588294012 ubuntu:latest "/bin/bash" 43 minutes ago Up 43 minutes sleepy_bhaskara
root@instance-1:~# docker commit -m "ssh installed" -a "elaiz" f22588294012 elaiz/ubuntu:ssh
sha256:d4cbb2e508309a2d21c6b63abdaa1ee485b0e20ec3b08aca20d916895beb4160
root@instance-1:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
elaiz/ubuntu ssh d4cbb2e50830 About a minute ago 242MB
ubuntu latest 3556258649b2 4 days ago 64.2MB其中,-m指定说明信息;-a指定用户信息;f22588294012代表容器的id;elaiz/ubuntu:ssh指定目标镜像的用户名、仓库名和 tag 信息。注意这里的用户名elaiz,后边会用到。
4. 用自己的镜像文件生成一个容器
接下来就是验证自己生成的image可以使用啦,
root@instance-1:~# docker run -it --name test1 --hostname tes1 d4cbb2e50830 #在生成容器的时候可以指定容器名和主机名
root@tes1:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.7 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:07 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@tes1:/# passwd #先修改root密码
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@tes1:/# service ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@tes1:/# ssh 172.17.0.1
root@172.17.0.1's password:
Last login: Sat Jul 27 22:40:36 2019 from 93.64.151.250
root@instance-1:~#
root@instance-1:~# ssh 172.17.0.7
root@tes1:~# date #登录成功
Sat Jul 27 23:24:30 UTC 2019
root@tes1:~# 5. 上传自己的镜像文件至镜像仓库
上一步已经证明自己commit的镜像灰常滴好用,怎样备份自己的镜像文件来以后使用或者分享给别人下载使用呢,Docker Hub就提供这种服务啦,不过要先在网站上注册一个账号,账户名要跟3中的用户名一致啦。不过好像第一次login之后再次login就不需要用户名跟密码了呢!
root@instance-1:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
elaiz/ubuntu ssh d4cbb2e50830 20 minutes ago 242MB
root@instance-1:~# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
root@instance-1:~#
root@instance-1:~# docker push elaiz/ubuntu:ssh
The push refers to repository [docker.io/elaiz/ubuntu]
091fd21a3bf1: Preparing
b079b3fa8d1b: Preparing
a31dbd3063d7: Preparing
c56e09e1bd18: Preparing
543791078bdb: Preparing 上传完成后,就可以在Docker Hub中查看自己的镜像文件啦,当然默认是公开的,别人也可以通过docker pull elaiz/ubuntu:ssh来直接使用这个镜像啦。
至此,使用docker就可以很方便的安装自己所需要的linux发行版了,并且可以快速启停,便捷的移植,这都是vmware中虚拟机所不能比滴!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3154951/ ,如需转载,请注明出处,否则将追究法律责任。