# 與執行中的容器互動

建立完容器後，我們常常會需要進入容器來達到一些目的，例如 `查看設定檔`、`安裝套件` 等等，這時可以用`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`

```
# 本機檔案，複製到容器 (容器ID為b5e16239b697)
root@vm:/home/jennifer# docker cp docker_build.sh b5e1:/docker_build.sh

# 容器檔案(容器ID為b5e16239b697)，複製到本機
docker cp b5e1:/do.sh do.sh
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cutejaneii.gitbook.io/docker/docker-container/yu-rong-qi-hu-dong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
