Chinaunix首页 | 论坛 | 博客
  • 博客访问: 162573
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 622
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-19 19:12
文章分类

全部博文(67)

分类: 虚拟化

2017-10-25 16:33:34


docker pull centos
进入由此image创建的container可以查看centos版本为7

在container中安装一些常用工具
1、安装ssh
yum install openssh-server
systemctl start sshd  (centos7中service,systemctl取代之)
Failed to get D-Bus connection: Operation not permitted      无法使用service、systemctl,后面有解决方法

sshd
sshd re-exec requires execution with an absolute path     要求用绝对路径执行

/usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

/usr/sbin/sshd
至此启动sshd成功

修改/etc/ssh/sshd_config
#UsePAM yes
PermitRootLogin yes

用-p映射端口后即可登陆,但是要求输入root密码
可以在container中修改root的密码,用password命令

2、ifconfig,ping等网络命令
yum install net-tools.x86_64

3、lsb_release
yum provides lsb_release 
查看哪个包支持就装哪个

4、Failed to get D-Bus connection: Operation not permitted
无法使用service  systemctl命令,原因在于系统未启动init进程
docker run -itd --privileged imagename /usr/sbin/init
然后用docker exec进入container


关于container的启动命令
docker run -itd container myconmand
如果myconmand为空,则默认运行/bin/bash,container在后台运行,但container中除了bash之外没有启动其他进程,bash的进程号为1
如果myconmand启动一个运行完就退出的程序,或启动后台运行的守护进程在,则程序退出时或进入后台运行时container立即退出
如果myconmand启动的守护进程前台运行,则container保持后台运行,且其中运行着由myconmand启动的进程,其进程号为1

也就是说在container中由myconmand启动的进程其进程号为1,只要这个1号进程一退出,container就退出

假设/run.sh脚本负责启动一个守护程序在前台运行
docker run -itd container /run.sh container在后台运行/run.sh,/run.sh在container中进程号为1,且不会退出(因为run.sh中有前台启动的守护进程)
此时用attach进入container后无法运行任何命令,因为此时进入的shell中正在运行/run.sh启动的在前台运行的进程,
可以用exec运行/bin/bash进入container,此时启动了另外一个bash,可以查看系统中运行的程序
docker exec -it container /bin/bash

docker run -it container  启动到container中,手动运行/run.sh,然后ctrl p,ctrl q退出,此时container不会退出
此时可以用attach或exec进入container,attach不能用exit退出,否则container退出,可以用ctrl p,ctrl q,exec可以用exit

如果run.sh启动的是一些后台运行的守护进程,则conmand不能是/run.sh
只能用docker run -it container启动container,并手动启动run.sh,此时的1号进程是bash,可ctrl p,ctrl q退出

综上,可用如下步骤启动
docker run -itd --privileged --name containername imagename /usr/sbin/init  启动一个container,其中前台运行init
docker exec -it containername /bin/bash 进入container并启动一个新的shell,至此可以运行一个run.sh脚本来启动需要的服务


阅读(1026) | 评论(0) | 转发(0) |
0

上一篇:反转int

下一篇:docker使用记录

给主人留下些什么吧!~~