目錄

docker exec 指令:在執行中的容器內執行指令

作者 · 更新於 2026年10月26日

docker exec 在一個執行中的容器裡執行指令,最常用來進入容器檢查問題。除錯的完整流程見 查看日誌與除錯。

語法

docker exec [參數] 容器 指令 [引數...]
docker exec -it web sh        # 進入容器的 shell
docker exec web nginx -t      # 執行一個指令並取得結果

參數

docker exec 的參數不多,以下是 docker exec --help 列出的全部參數:

參數說明
-i, --interactive保持標準輸入開啟,可以輸入內容
-t, --tty配置一個終端機。互動操作時和 -i 一起用:-it
-u, --user 使用者[:群組]以指定的使用者(名稱或 UID)執行
-w, --workdir 路徑在容器內的指定目錄執行
-e, --env 名稱=值設定這次執行的環境變數
--env-file 檔案從檔案讀取環境變數
-d, --detach在背景執行,指令立刻返回
--privileged給這個指令額外的權限(除錯用,平常不要使用)
--detach-keys修改離開的快捷鍵(預設 Ctrl+P、Ctrl+Q)

以下範例都對一個名為 web 的 nginx 容器執行,它啟動時設定了環境變數 APP_MODE=demo:

docker run -d --name web -e APP_MODE=demo nginx:1.30-alpine

執行一個指令

$ docker exec web nginx -v
nginx version: nginx/1.30.5

$ docker exec web sh -c "ls /etc/nginx/conf.d && echo \$APP_MODE"
default.conf
demo
  • 第一個指令在容器中執行 nginx -v,結果直接印出來。
  • 要使用 &&、管線、變數時,用 sh -c "..." 交給容器內的 shell 處理。\$APP_MODE 中的 \ 讓主機的 shell 不要先替換變數,demo 是容器內的值。

互動式進入容器

docker exec -it web sh

進入後提示字元變成 / #,可以像在一般 Linux 中操作,輸入 exit 離開,容器不會停止。如果映像檔有 bash,也可以用 bash。

-u、-w、-e

$ docker exec -u nginx web id
uid=101(nginx) gid=101(nginx) groups=101(nginx)

$ docker exec -w /usr/share/nginx/html web ls
50x.html
index.html

$ docker exec -e GREETING=hi web sh -c "echo \$GREETING"
hi
  • -u nginx:以 nginx 使用者執行(容器預設是 root)。檢查檔案權限問題時很有用。
  • -w /usr/share/nginx/html:在指定的目錄執行,相當於先 cd。
  • -e GREETING=hi:只對這次執行設定環境變數,不會改變容器本身。

-d:在背景執行

$ docker exec -d web sh -c "sleep 2; touch /tmp/done"

$ docker exec web ls /tmp
done

-d 讓指令在背景執行並立刻返回,2 秒後檢查,/tmp/done 已經建立。適合啟動不需要等待結果的工作。

容器沒有在執行

$ docker stop web
web

$ docker exec web ls
Error response from daemon: container 1ec4fdc5c592b09b64e8f28869c37f89ab17163f88bfcb09e82815a44b0e1164 is not running

docker exec 只能用在執行中的容器。已停止的容器要先 docker start;容器一啟動就退出的話,exec 也進不去,要改看 docker logs,或用 docker run --rm -it --entrypoint sh 映像檔 啟動一個新容器檢查。

常見問題與誤解

不能對映像檔 exec

docker exec 的目標是已存在且正在執行的容器。映像檔只是建立容器的材料,不能直接當成 exec 的目標:

$ docker exec nginx:1.30-alpine nginx -v
Error response from daemon: No such container: nginx:1.30-alpine
  • nginx:1.30-alpine 是映像檔名稱,Docker 會把它當成容器名稱查找,所以回覆 No such container。
  • 要在新容器中執行映像檔的指令,使用 docker run --rm nginx:1.30-alpine nginx -v。

對已停止的容器 exec

容器存在不代表它正在執行。docker exec 不能在已停止的容器中啟動程序:

$ docker exec job echo hi
Error response from daemon: container 1d271133e82d802099ef0728c0e511450eabac9bc85ef4721b1b8f6c3ef49019 is not running
  • is not running 表示容器目前已停止;exec 只對執行中的容器有效。
  • 若容器可以持續執行,可先啟動它再 exec;若只需要一次性指令,使用 docker run --rm 建立新容器。

在容器內執行 curl、wget 等一次性指令

有些映像檔本身已包含檢查工具。例如這個 nginx 容器同時有 curl 和 wget,可直接用 exec 發出請求:

$ docker exec web curl -s localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy, 
API gateway, load balancer, content cache, or other features.</p>

<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional 
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

$ docker exec web wget -qO- localhost | head -n 4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

$ docker exec web sh -c 'which curl wget'
/usr/bin/curl
/usr/bin/wget
  • curl 和 wget 都已存在於 nginx:1.30-alpine;是否能用某個工具取決於映像檔內容。
  • wget 的範例把輸出管線交給 head,只顯示前四行。

若容器內缺少工具,可以啟動輔助映像檔,並加入目標容器的網路命名空間:

$ docker run --rm --network container:web curlimages/curl:8.17.0 -s -o /dev/null -w '%{http_code}\n' http://localhost
Unable to find image 'curlimages/curl:8.17.0' locally
8.17.0: Pulling from curlimages/curl
bd9ddc54bea9: Pulling fs layer
2d35ebdb57d9: Pulling fs layer
f2f82549a04b: Pulling fs layer
2d35ebdb57d9: Download complete
bd9ddc54bea9: Download complete
f2f82549a04b: Download complete
2d35ebdb57d9: Pull complete
f2f82549a04b: Pull complete
bd9ddc54bea9: Pull complete
Digest: sha256:935d9100e9ba842cdb060de42472c7ca90cfe9a7c96e4dacb55e79e560b3ff40
Status: Downloaded newer image for curlimages/curl:8.17.0
200
  • --network container:web 讓輔助容器共用 web 的網路命名空間,因此可以用 localhost 連到 nginx。
  • --rm 會在指令結束後移除輔助容器;第一次使用時會先下載映像檔,之後可直接執行。

docker exec 與 docker compose exec 的差別

Compose 指令使用服務名稱;一般 docker exec 則使用實際容器名稱或 ID。以下服務 web 對應的容器名稱是 proj-web-1:

$ cd /work/proj && docker compose ps --format 'table {{.Service}}\t{{.Name}}'
SERVICE   NAME
web       proj-web-1

$ cd /work/proj && docker compose exec web nginx -v
nginx version: nginx/1.30.6

$ docker exec proj-web-1 nginx -v
nginx version: nginx/1.30.6

$ cd /work/proj && docker compose exec -T web cat /etc/os-release | head -n 2
NAME="Alpine Linux"
ID=alpine
  • docker compose exec web ... 的 web 是 Compose 服務名稱;一般 docker exec 要指定實際容器名稱,例如 proj-web-1。
  • -T 關閉偽終端機配置,適合把輸出接到管線等非互動情境。

docker compose run 會建立另一個新容器來執行指令,不會在現有服務容器上執行 exec:

$ cd /work/proj && docker compose run --rm web nginx -v
 Container proj-web-run-26942a9370bc Creating 
 Container proj-web-run-26942a9370bc Created 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx version: nginx/1.30.6
  • run 建立了名稱帶有 -run- 的新容器,並在指令完成後因 --rm 移除。
  • 新容器會先執行 nginx 映像檔的 entrypoint 設定流程,因此除了版本資訊,還會看到額外的啟動輸出。

相關章節

動手練習

練習:用一個指令,以 nginx 使用者的身分,在 web 容器的 /etc/nginx 目錄中列出檔案。

答案
docker exec -u nginx -w /etc/nginx web ls

常見問題

docker exec 和 docker run 有什麼不同?

docker run 用映像檔**建立一個新容器**;docker exec 在**已經在執行的容器**中多執行一個程序。exec 結束不會影響容器本身。

docker exec -it 容器 bash 出現 executable file not found?

容器裡沒有 bash,例如 Alpine 為基礎的映像檔只有 sh。改成 docker exec -it 容器 sh。

在容器裡修改的檔案,重建容器後還在嗎?

不在。用 exec 修改的是容器的可寫層,刪除容器就消失了。需要保留的改動要寫進 Dockerfile,或放在 Volume 中。

docker exec 可以直接對映像檔執行嗎?

不行。docker exec 的第一個位置參數是**執行中的容器名稱或 ID**,不是映像檔名稱。要從映像檔建立新容器並執行指令,使用 docker run。

更新於 2026年10月26日