简体   繁体   English

如何从 vscode 远程容器查看 docker 日志?

[英]How to view docker logs from vscode remote container?

I'm currently using vscode's remote containers extension with a .devcontainer.json file that points to my docker-compose.yml file.我目前使用vscode的远程容器扩展了.devcontainer.json文件指向我的docker-compose.yml文件。

Everything works fine and my docker-compose start command gets run (which launches a web server), but I haven't found a way to quickly see the logs from the web server.一切正常,我的 docker-compose start 命令开始运行(启动 Web 服务器),但我还没有找到一种方法来快速查看来自 Web 服务器的日志。 Has anyone found a way to view the docker log output automatically once vscode connects to the remote container?有没有人找到一种方法来在 vscode 连接到远程容器后自动查看 docker 日志输出?

I know as an alternative I could remove my container's start command and, after vscode connects, manually open a terminal and start the web server, but I'm hoping there's an easier way.我知道作为替代方案,我可以删除容器的启动命令,并且在 vscode 连接后,手动打开终端并启动 Web 服务器,但我希望有更简单的方法。

Thanks in advance!提前致谢!

I'm not using remote containers, just local once, so not sure if this applies but for locally running containers, you can go to the "Docker" tab (you need to install the official Microsoft Docker VS Code Plugin ) where you can see your running containers.我没有使用远程容器,只是本地一次,所以不确定这是否适用,但对于本地运行的容器,您可以转到“Doc​​ker”选项卡(您需要安装官方的 Microsoft Docker VS Code Plugin ),您可以在其中看到你正在运行的容器。 Just right-click on the container you want to see the logs for and select "View Logs":只需右键单击要查看其日志的容器,然后选择“查看日志”:

在此处输入图片说明

You'll see a new "Task" appear in the Terminal pane that will show all your docker logs:您将在终端窗格中看到一个新的“任务”,它将显示您的所有 docker 日志:

在此处输入图片说明

This question is really old and I'm not sure it this option was available at this time but just open the Command Palette (F1) and select/find "Remote-Containers: Show Log".这个问题真的很老,我不确定这个选项此时是否可用,但只需打开命令面板(F1)并选择/找到“远程容器:显示日志”。

You see now the log of your container in the terminal.您现在可以在终端中看到容器的日志。

I use VS Code's builtin terminal to see the live logs of the docker container that is connected with VS Code.我使用 VS Code 的内置 终端来查看与 VS Code 连接的 docker 容器的实时日志。

When VS Code is connected to the docker container, you can open the builtin terminal using the View > Terminal menu option.当 VS Code 连接到 docker 容器时,您可以使用View > Terminal菜单选项打开内置终端。 You should see an existing terminal labeled Dev Containers .您应该会看到一个标记为Dev Containers的现有终端。

开发容器

Maybe this is too late?也许这太晚了? But for others, this is how I do it.但对于其他人,我就是这样做的。

First, instead of logging stuff to the stdout , I redirect all of the outputs into one single file and then using the tail command to steam the output to the terminal instead.首先,我没有将内容记录到stdout ,而是将所有输出重定向到一个文件中,然后使用tail命令将输出传输到终端。

For example, I am going Go here:例如,我要去这里:

 logFile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE, 0755) if err != nil { log.Fatal("Fail to open the log file") } logrus.SetOutput(logFile)

Once that's done, I open up my terminal and run my the following command:完成后,我打开终端并运行以下命令:

$ tail -f {logFileName}


That's one way to do it I guess, but I sure hope VSCode can come up with a better solution.我猜这是一种方法,但我当然希望 VSCode 能提出更好的解决方案。

You can open the command palette and search for: Remote Explorer: Focus on containers view.您可以打开命令面板并搜索:远程资源管理器:专注于容器视图。 You should see a sidebar of containers, if you right click your container you can view logs.您应该会看到一个容器侧边栏,如果您右键单击您的容器,您可以查看日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM