进入docker容器并执行命令的的3中方法

  1. docker exec
     
  2. nsenter
     
  3. docker attach "container"

建议使用nsenter, exec有时候会有问题。 attach执行完之后会自动退出

exec需要在运行中的容器中执行:

nsenter需要安装,默认最小安装里面有,yum -y install util-linux

 

attach需要是启动的容器,退出的需要使用start先启动- docker
start CONTAINER
(使用attach进入容器后退出后容器会退出)

[root@docker01 ~]# docker --help|grep exec

exec        Run a command in a running container

[root@docker01 ~]# docker ps

CONTAINER ID       
IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

9218c822d3a5       
docker.io/centos   
"/bin/bash"             
11 hours ago        Up 11 hours                             cent03

e4d5b979923b       
docker.io/centos   
"bash"                  
21 hours ago        Up 11
hours                             cent02

fe7ab7cd9ec0       
bash               
"docker-entrypoint..."  
21 hours ago        Up 12 hours                             cent01

216dc5feb799        8b89e48b5f15       
"/bin/bash"              21 hours ago        Up 12 hours        
80/tcp              nginx02

[root@docker01 ~]# docker exec cent01 route

Kernel IP routing table

Destination    
Gateway         Genmask         Flags Metric Ref    Use Iface

default        
172.17.0.1      0.0.0.0         UG   
0      0        0 eth0

172.17.0.0     
*               255.255.0.0     U    
0      0        0 eth0

[root@docker01 ~]#

[root@docker01 scripts]# rpm -ql util-linux|grep
nsenter

/usr/bin/nsenter

/usr/share/bash-completion/completions/nsenter

/usr/share/man/man1/nsenter.1.gz

[root@docker01 scripts]# rpm -ql util-linux|grep
nsenter

exec直接进入bash的方法:

[root@docker01 work]# docker exec -it cent02
/bin/bash

[root@e4d5b979923b /]# cat /etc/hosts

127.0.0.1      
localhost

::1    
localhost ip6-localhost ip6-loopback

fe00::0 ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

172.17.0.4     
e4d5b979923b

直接去下载了httpd的容器并启动80端口的httpd服务。

[root@docker01 scripts]# docker run -d -p 83:80
--name cent_80 httpd

Unable to find image 'httpd:latest' locally

Trying to pull repository docker.io/library/httpd ...

latest: Pulling from docker.io/library/httpd

d660b1f15b9b: Pull complete

aa1c79a2fa37: Pull complete

f5f6514c0aff: Pull complete

676d3dd26040: Pull complete

4fdddf845a1b: Pull complete

28ecdadc5f88: Pull complete

5d882098e42b: Pull complete

Digest:
sha256:2edbf09d0dbdf2a3e21e4cb52f3385ad916c01dc2528868bc3499111cc54e937

Status: Downloaded newer image for
docker.io/httpd:latest

c720d0aa41860f2d88cd652f395d52279f0a485b7852da5d7fce4627ac7ef816

[root@docker01 scripts]# curl 10.0.0.181:83

<html><body><h1>It works!</h1></body></html>

进入docker容器并执行命令的的3中方法的更多相关文章

  1. docker镜像、docker容器导入导出命令

    一.docker镜像导入导出命令 导出命令: docker save -o <保存路径> <镜像名称:标签> docker save -o ./test.tar test:la ...

  2. WORKDIR 指定工作目录 每一个 RUN 都是启动一个容器、执行命令、然后提交存储层文件变更

    WORKDIR 指定工作目录 格式为 WORKDIR <工作目录路径>. 使用 WORKDIR 指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指定的目录,如该目录不 ...

  3. docker 容器管理常用命令

    Docker 容器管理: docker create -it centos //这样可以创建一个容器,但该容器并没有启动: create Create a new container 创建一个容器: ...

  4. Docker容器技术-基础命令

    一.基础命令 1.运行一个镜像 [root@bogon ~]# docker run debian echo "Hello World" Unable to find image ...

  5. linux中docker容器安装vi命令详解

    在使用docker容器时,同时你docker里的系统正好是debian或ubuntu的时候,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要 ...

  6. XP_CMDSHELL 执行命令添加 windows 用户的方法

    1. 之前看过不少文档 可以使用 xp_SQLCMD的命令来进行渗透处理, 今天因为公司的服务器又中毒了 自己学习了下. 2. 修改SQLSERVER的设置 远程登录数据库 sqlcmd -S 10. ...

  7. docker进入容器内部执行命令

    [root@bogon ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 38a2cae4c32f jenk ...

  8. docker 容器使用 systemctl 命令是报错

    看了许多解决方案,但是对于新手来说并不友好,不是特别清楚 报错内容: System has not been booted with systemd as init system (PID 1). C ...

  9. Docker容器操作中常用命令集合

    docker pull 从仓库获取所需要的镜像 docker images 显示本地已有的镜像. docker commit 提交更新后的副本. docker build 创建一个新的镜像 ADD 复 ...

随机推荐

  1. wms-springmvc-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. C#基础之类型和成员基础以及常量、字段、属性

    首先吐糟一下今天杭州的天气,真是太热了!虽然没有妹子跟我约会,但宅在方寸大的窝里,也是烦躁不已! 接上一篇<C#基础之基本类型> 类型和成员基础 在C#中,一个类型内部可以定义多种成员:常 ...

  3. TCP/IP三次握手与四次挥手的正确姿势

    0.史上最容易理解的:TCP三次握手,四次挥手 https://cloud.tencent.com/developer/news/257281 A 理解TCP/IP三次握手与四次挥手的正确姿势http ...

  4. 配置了SSH后还是每次都要求输入密码

    保存凭证可以解决问题 git config --global credential.helper store

  5. 003 Longest Substring Without Repeating Characters 最长不重复子串

    Given a string, find the length of the longest substring without repeating characters.Examples:Given ...

  6. 由Python的浅拷贝(shallow copy)和深拷贝(deep copy)引发的思考

    首先查看拷贝模块(copy)发现: >>> help(copy)Help on module copy:NAME    copy - Generic (shallow and dee ...

  7. Linux安装配置相关

    1.常用汇总 useradd usertemp //添加用户 passwd usertemp //修改/设置密码 userdel usertemp //删除用户 2. profile/baserc等配 ...

  8. json java simple-json

    http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-1_-_Encode_a_JSON_object javac ...

  9. Day1上

    上午发挥强差人意.心态不好,编译器一直报错,心里比较慌. t1 每一个P枚举底数 .可二分 T2 暴力30  打标60 x^3-y^3=(x-y)*(x^2+xy+y^2). x-y==1.  ! p ...

  10. Java中的for循环——通过示例学习Java编程(9)

      作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=21 循环用于反复执行同一组语句,直到满足特定条件 ...