跳转至

Docker exec 命令


2016-03-21 by dongnan

举个栗子

使用 docker exec,执行容器内的 ifconfig 命令(或 ip addr)获取容器的 ip 地址。

# 执行命令
docker exec ca7f0314bdfb 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:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:138 errors:0 dropped:0 overruns:0 frame:0
          TX packets:78 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:29094 (28.4 KiB)  TX bytes:15660 (15.2 KiB)

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)

ca7f0314bdfb 是容器的ID,也可以使用容器名称代替。

exec可用于执行容器中的任何命令,例如查看容器的操作系统版本。

# 执行命令
docker exec ca7f0314bdfb cat /etc/issue

CentOS release 6.7 (Final)
Kernel \r on an \m

帮助

# 执行命令
docker exec --help

Usage:    docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
  -d, --detach=false         Detached mode: run command in the background
  --help=false               Print usage
  -i, --interactive=false    Keep STDIN open even if not attached
  --privileged=false         Give extended privileges to the command
  -t, --tty=false            Allocate a pseudo-TTY
  -u, --user=                Username or UID (format: <name|uid>[:<group|gid>])






回到页面顶部