Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH。如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可。当我启动了五个容器后,每个容器默认是没有配置SSH Server的,安装配置SSHD,映射容器SSH端口,实在是麻烦。

我发现很多Docker镜像都是没有安装SSHD服务的,难道有其他方法进入Docker容器?

浏览了Docker的文档,我没有找到答案。还是要求助于无所不能的Google,万能的Google告诉我用nsenter吧。

在大多数Linux发行版中,util-linux包中含有nsenter.如果没有,你需要安装它.

cd /tmp
curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz \
| tar -zxf-
cd util-linux-2.24
./configure --without-ncurses
make nsenter
cp nsenter /usr/local/bin

使用shell脚本 docker-enter,将如下代码保存为docker-enter, chomod +x docker-enter

  #!/bin/sh

  if [ -e $(dirname "$0")/nsenter ]; then
# with boot2docker, nsenter is not in the PATH but it is in the same folder
NSENTER=$(dirname "$0")/nsenter
else
NSENTER=nsenter
fi if [ -z "$1" ]; then
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
echo ""
echo "Enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
else
PID=$(docker inspect --format "{{.State.Pid}}" "$1")
if [ -z "$PID" ]; then
exit 1
fi
shift OPTS="--target $PID --mount --uts --ipc --net --pid --" if [ -z "$1" ]; then
# No command given.
# Use su to clear all host environment variables except for TERM,
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
# and start a login shell.
"$NSENTER" $OPTS su - root
else
# Use env to clear all host environment variables.
"$NSENTER" $OPTS env --ignore-environment -- "$@"
fi

If your OS has SELinux enabled and you want to run Weave Net as a systemd unit, then follow the instructions below. These instructions apply to CentOS and RHEL as of 7.0. On Fedora 21, there is no need to do this.

Once weave is installed in /usr/local/bin, set its execution context with the commands shown below. You will need to have the policycoreutils-python package installed.

sudo semanage fcontext -a -t unconfined_exec_t -f f /usr/local/bin/weave
sudo restorecon /usr/local/bin/weave
 

nsenter into docker. selinux(semanage,restorecon)的更多相关文章

  1. 分布式进阶(九)Ubuntu下使用nsenter进入Docker容器

    使用nsenter进入Docker容器 Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH.如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可.当我启动 ...

  2. 通过nsenter连接docker容器

    通常连接Docker容器并与其进行交互有四种方法.详情见:https://github.com/berresch/Docker-Enter-Demo,下面摘录nsenter连接的方式. 查看是否安装n ...

  3. Docker selinux

    编辑/etc/sysconfig/docker文件,把OPTIONS='--selinux-enabled'的--selinux-enabled注释掉,也可以通过这个错误. 最大的问题就是Linux的 ...

  4. 使用nsenter进入docker容器后端报错 mesg: ttyname failed: No such file or directory

    通过nsenter 进入到docker容器的后端总是报下面的错,, [root@devdtt ~]# docker inspect -f {{.State.Pid}} mynginx411950 [r ...

  5. 【云计算】使用nsenter进入Docker容器进行调试

    [nsenter安装使用] cd /tmp curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.t ...

  6. docker底层技术概览

    docker解决了云计算环境难于分发并且管理复杂,而用KVM.Xen等虚拟化又浪费系统资源的问题.Docker最初是基于lxc构建了容器引擎,为了提供跨平台支持,后又专门开发了libcontainer ...

  7. 记一次docker问题定位(perf,iostat等性能分析)

    背景 最近参与的项目是基于 OpenStack 提供容器管理能力,丰富公司 IaaS 平台的能力.日常主要工作就是在开源的 novadocker 项目(开源社区已停止开发)基础上进行增强,与公司的其他 ...

  8. centos7(debian,manjora,freebsd)命令及安装mysql、git、gpg、gogs,安装docker,zsh,chrome

    最小安装: 1. 选择English 2. DATE & TIME 修改好本地时间 SOFTWARE SELECTION默认的Minimal Install就好 INSTALLATION DE ...

  9. docker 学习操作记录 5-2

    记录5-2 root@53d0a643a2c7:/# quit bash: quit: command not found root@53d0a643a2c7:/# exit exit -->@ ...

随机推荐

  1. MySQL的优化技术总结

    MySQL的优化技术总结 如果Cache很大,把数据放入内存中的话,那么瓶颈可能是CPU瓶颈或者CPU和内存不匹配的瓶颈: seek定位的速度,read/write即读写速度: 硬件的提升是最有效的方 ...

  2. ITEYE中的读书笔记:重构其实就是持续改进

    原文地址:http://hawkon.iteye.com/blog/2093338#bc2355877 前段时间同事参加ITEYE的试读有奖, 没想到得了个奖,拿到一本书.由于同事的推荐我也认真读了一 ...

  3. http://blog.csdn.net/jiazimo/article/details/17265061

    http://blog.csdn.net/jiazimo/article/details/17265061

  4. 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram

    题目地址:http://codeforces.com/contest/435/problem/C /* 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 模拟题:蛮恶心的,不过也简单 ...

  5. 添加Bootstrap 到MVC项目中(vs2010)

    环境:Visual Studio 2010   ASP.NET MVC3 手动添加: 下载地址:http://twitter.github.com/bootstrap/ 一个js,两个css,两张图 ...

  6. IOS 怎么设置UIButton UITextField 不可点击且变灰

    [button setEnable:NO]; [button setAlpha:0.4];

  7. Scala:使用Sublime开发Scala

    Scala:使用Sublime开发Scala 第一步:[Tools][Build System][New Build System] 第二步:在打开的新文件中输入: { //"cmd&quo ...

  8. Apache开启不了------The requested operation has failed!

    1.这个问题通常是由于端口被占用了,cmd输入netstat -ano查看端口号,根据端口号80的程序的pid在任务管理器下杀掉它. 2.我遇到这个问题不是这个原因.我由于刚配置了虚拟目录.查看虚拟目 ...

  9. 通过SEP禁用USB

    1      Introduction 1.1      Scope This document provides comprehensive information of the reinforce ...

  10. mongodb3.2.3 复制集安装步骤

    mongodb 复制集 测试 node1: 172.18.20.161 47000 (主)node2: 172.18.20.162 47000 (副)node3: 172.18.20.163 4700 ...