官术网_书友最值得收藏!

Exec into a running container

In debugging and testing scenarios, it is often required to execute another process inside a running container in an ad hoc manner. This is especially useful for creating a shell instance (for Windows, with cmd.exe or powershell.exe, and for Linux, with bash or sh) in the container and interactively debugging the container. Such an operation is called performing exec into a running container.

Visual Studio Code enables this through Docker Explorer. In the Docker Explorer tab, find the container you would like to exec into, right-click it, and choose Attach Shell:

By default, for Windows containers, this command will exec using the  powershell.exe command. If you are running images based on Windows Nano Server, you will not be able to use powershell.exe and you'll have to use cmd.exe instead. To customize which command is used during Attach Shell, open Settings ( Ctrl + ,), search for docker, and customize the  docker.attachShellCommand.windowsContainer setting.

In the Powershell command line, the equivalent docker exec command is as follows:

docker exec -it <containerId> powershell.exe

The preceding command creates a new powershell.exe process inside the running container in interactive mode with the attached terminal (the -it parameter). As you can see, a new interactive instance of the Powershell terminal opens:

You can only exec into containers that have the main process running. If the container is exited, terminated, or placed in a paused state, it will not be possible to use the  exec command.

Let's try inspecting the contents of index.html inside the container's working directory:

cat .\index.html

The following screenshot shows the output of the preceding command:

This reveals the expected contents of the index.html file that we created and added to the image earlier.

We can also inspect the IIS worker process (w3wp.exe) for the application pool hosting index.html. This is a common scenario during debugging when not all the logs are available directly via container output logs:

cat ..\logs\LogFiles\W3SVC1\u_ex<current date>.log

The following screenshot shows the output of the preceding command:

Using docker exec is one of the most powerful commands that you have in your container toolbox. If you learn how to use it, you'll be able to debug your applications almost as if they were hosted in a non-containerized environment.

主站蜘蛛池模板: 海林市| 乌鲁木齐市| 洛川县| 建水县| 思南县| 广宗县| 信丰县| 彭山县| 商南县| 东乡族自治县| 延长县| 八宿县| 德江县| 江津市| 全州县| 凭祥市| 五河县| 高唐县| 青田县| 西乌珠穆沁旗| 上蔡县| 白山市| 吕梁市| 宁陕县| 广宁县| 工布江达县| 南开区| 台南县| 桃江县| 凉城县| 栾川县| 石渠县| 温州市| 天柱县| 伊金霍洛旗| 麟游县| 香港| 贵南县| 阳江市| 富蕴县| 积石山|