Execute a command in a running container 在运行的容器中执行命令
用法
1
exec [options] [-e KEY=VAL...] SERVICE COMMAND [ARGS...]
选项
1 2 3 4 5 6 7 8
Options: -d, --detach Detached mode: Run command in the background. --privileged Give extended privileges to the process. -u, --user USER Run the command as this user. -T Disable pseudo-tty allocation. By default `docker-compose exec` allocates a TTY. --index=index index of the container if there are multiple instances of a service [default: 1] -e, --env KEY=VAL Set environment variables (can be used multiple times, not supported in API < 1.25) -w, --workdir DIR Path to workdir directory for this command.
注意
无
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 交互方式进入容器sh命令行,注意部分镜像没有内置bash,如alpine $ docker-compose exec demo sh / # ls /usr/share/nginx/html/ demo.txt index.html / # cat /usr/share/nginx/html/index.html demo # 非交互式执行容器中的命令 $ docker-compose exec demo cat /usr/share/nginx/html/index.html demo # 如果有特殊字符需要使用如下方式执行,sh -c 'cmd' $ docker-compose exec demo sh -c 'echo test > /usr/share/nginx/html/index.html' # 验证 $ curl localhost test