Is there a way to debug the PostCreateCommand in VSCode Devcontainers?
Asked Answered
T

4

9

I am currently having this issue:

Command failed: /bin/sh -c ./.devcontainer/postCreateCommand.sh

which is the

    "postCreateCommand": "./.devcontainer/postCreateCommand.sh",

setting from the devcontainer.json.

But the script works when I deactivate the "postCreateCommand" and run it manually after container creation. Which made me think, that the issue might be the path to the script somehow. But that is not the case either. Since an empty script with just an echo command seems to work.

The script is the following:

echo "Installing Developer Requirements"

apt-get update && apt-get install -y man git
pip3 install -r .devcontainer/dev_pip_requirements.txt

Any ideas how to debug the "PostCreateCommand"? The output is less than helpful and I don't want to start reducing this project into a minimum working example.

I changed git to checkout everything with linux file endings on windows. And I triple checked that the shell script has LF endings. So those should not be an issue either. (They were previously).

Trichosis answered 26/1, 2021 at 21:51 Comment(1)
The output can be seen in the container logs ("Remote-Containers: Show Container log")Lightman
N
8

I think that one cause may be that the user does not have enough permissions to even execute the apt-get update command.

Changing the remoteUser to root, in the devcontainer.json configuration file should allow to execute the postCreateCommand.sh shell script successfully when running it using bash.

  • I've tried it using the python:3.8-slim-buster Docker image, having set a non-root user in the Dockerfile.

  • I've added the following to the ./.devcontainer/devcontainer.json:

    "postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",
    "remoteUser": "root",
    
  • I've used the following ./.devcontainer/postCreateCommand.sh:

    #!/usr/bin/env bash
    
    set -ex
    
    apt-get update && apt-get install -y man git && rm -rf /var/lib/apt/lists/*
    
    pip install --requirement ./.devcontainer/dev_pip_requirements.txt
    
    

Generally, root is not recommended for production environments.

Here is a little more information about postCreateCommand

Nonmaterial answered 22/5, 2021 at 10:24 Comment(1)
I actually solved this particular issue much later and forgot I asked: The problem was that the script was not executable. chmod +x solved the problem. But this does not answer the question if there is a way to debug vscode devcontainersTrichosis
W
3

We experienced the same issue recently. Took me a big while to discover this question with no answer. We came across an ugly solution and here's what we did:

  1. In your devcontainer.json, add the following:
"mounts": ["type=bind,source=/home/runner/logs,target=/var/logs"],
  1. Change your commands. In your case:
- "postCreateCommand": "./.devcontainer/postCreateCommand.sh",
+ "postCreateCommand": ["/bin/sh", "-c", "/bin/sh ./.devcontainer/postCreateCommand.sh >> 2>&1 /var/logs/garbage.log"],

Finally, if the container exploded, cat /home/runner/logs/*.log.
You might as well need to run chown /var/logs, or just make it 0777.

Alternatively, you do something like

- "postCreateCommand": "./.devcontainer/postCreateCommand.sh",
+ "postCreateCommand": ["/bin/sh", "-c", "(/bin/sh ./.devcontainer/postCreateCommand.sh >> 2>&1 /var/logs/garbage.log) || (echo lmao)"],

So that the script never fail.

Wilde answered 24/7, 2023 at 7:6 Comment(0)
E
2

Was looking around for solutions to the same issue. Found that it's possible to view these logs in VSCode as well.

Dev Containers Developer: Show all logs...

Search for PostCreateCommand and you should find what you need.

Empressement answered 7/8 at 7:39 Comment(0)
C
1

As of 2024, Codespaces now logs this for you: Cmd/CTRL + Shift + P to view them

enter image description here

The log file is located at /workspaces/.codespaces/.persistedshare/creation.log

Craner answered 29/2 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.