與執行中的容器互動
建立完容器後,我們常常會需要進入容器來達到一些目的,例如 查看設定檔、安裝套件 等等,這時可以用docker exec進入容器
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]假設我只是要簡單的執行完某個指令(例如 nginx -t)而已,可以直接把nginx -t放在COMMAND的位置,這個指令執行完就會回到本機
root@vm:/home/jennifer# docker exec -it b5e1 nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@vm:/home/jennifer#若是有比較複雜的事情需要完成,COMMAND可以下bash,接著會發現路徑會被改成root@b5e16239b697,表示目前正在ID為b5e16239b697的容器內
root@vm:/home/jennifer# docker exec -it b5e1 bash
root@b5e16239b697:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@b5e16239b697:/# time
real 0m0.000s
user 0m0.000s
sys 0m0.000s若在容器內想離開,則可輸入exit,就會發現路徑會回至本機了
root@b5e16239b697:/# exit # 在b5e1623b697容器內輸入exit
exit
root@vm:/home/jennifer# # 這裡已回到本機如果有些檔案希望能夠從容器複製到本機,或從本機複製到容器,可利用docker cp
Last updated
Was this helpful?