How to debug in a container?

preface

Before moving a YouTube video, learning a wave of big guys to use VScode to debug container skills, YYDS!

Video link: www.zhihu.com/zvideo/1384…

This is a simple note. I hope to integrate at least three VSCode Debug techniques, including but not limited to:

  • A programming languagedebug
  • The containerdebug
  • k8s debug

No more nonsense, start directly from the second article, free to update other debug techniques!

To prepare

Dockerfile and Python scripts (PS: look at this, it’s definitely worth learning!)

Assume that the above container is already running, named Dalaoyyds ~

Break down

The above tutorial is divided into three steps, step by step with your debug!

entrypoint

The debug method is to start an entryPoint =bash container:

Docker run-i-d -- entryPoint =bash dalaoyydsCopy the code

Using exec, you can enter a container to perform an operation:

Docker exec -it bashContainerIDCopy the code

Then you can do whatever you like.

cp

Following the entryPoint operation above, after executing the command inside the container, use cp to copy the output to the outside of the container:

docker cp xxxx:/src/time.txt ./src/time.txt.Container
Copy the code
debugger

Using the above Dockerfile as the base template, add a debugger image, install the Debupy plug-in, and then use ENTRYPOINT to expose a service port.

Externally, -p 5678:5678 is used to connect the host to the container. By default, the bridge network mode is used:

docker run -p 5678:5678 xxx unitest
Copy the code
The vscode plug-in smells good

Use the Python Remote Attach plugin if there is a py file in the project. If not, check that python is installed.

After you type a breakpoint, you can display the value of the locals variable on the left.

golang debug

The method in this video introduces debugging techniques in Python. Can we debug other programming languages?

Let’s introduce the GO container debug.

Windows workspace debug Remote containers

As a first step, install vsCode, vsCode Remote Containers Extension, Docker, and docker-compose. The remote-containers extension is installed in the VSCode extension, which starts the remote container to debug local code. The remote connection mode is generally SSH. Windows is used to connect to the remote Linux server.

Reference: stackoverflow.com/questions/6…

Step 2: Configure DevContainer Demo and modify it as needed:

Github.com/qdm12/godev…

Step 3, select remote-containers: Open Folder in Container..

The above Settings enable remote Debug.

The above solution is suitable for local Windows to debug remote containers, and docker Desktop and WSL programs need to be installed locally.

Linux workspace debug Remote containers

You can also choose to install the Linux version of vscode to complete the above operations.

dlv

Generally speaking, personally, I use VSCode for SSH remote Linux host development.

Plug-in: remote – SSH

In this case, you can use Golang’s debug tool DLV, install the binary package and DLV to debug in the container, edit the mapping between the host and the container config.yml, and externally connect the exposed service with VSCode to start debugging.

lauch.json

{" version ":" 0.2.0, "" configurations: [{" name" : "Remote Debug", "type" : "go", "request" : "launch", "mode" : "Remote", "remotePath" : ""," port ": 2000," the host ":" 127.0.0.1 ", "the program" : "${workspaceRoot}/cloud/cmd/cloudcore/cloudcore.go", "showLog": true, "env": {}, "args": [] } ] }Copy the code

config.yml

substitute-path:
  - {from: /, to: /root/go/src/github.com/kubeedge}
Copy the code

dockerfile

ENV CGO_ENABLED 0 COPY . /go/src/github.com/kubeedge/kubeedge RUN go build -gcflags "all=-N -l" -o /server github.com/kubeedge/kubeedge/cloud/cmd/cloudcore # Compile Delve RUN apk add --no-cache git RUN go get Github.com/derekparker/delve/cmd/dlv FROM alpine: 3.11 EXPOSE 8080 2000 # Allow delve to run on alpine -based containers. RUN apk add --no-cache libc6-compat WORKDIR / COPY --from=build-env /server / COPY --from=build-env /go/bin/dlv / COPY ./config.yml./ # Start configuration file for service, . According to the need to add the COPY cloudcore yaml/etc/kubeedge/config/cloudcore yaml COPY/root/kube/config/root /. Kube/config # Run delve # CMD  ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "exec", "/server"] #/dlv --listen=:40000 --headless=true --api-version=2 exec /server CMD ["/bin/sh"]Copy the code

docker run

 docker run -it --rm --name cloudcore --security-opt="seccomp=unconfined" --cap-add=SYS_PTRACE -p 2000:2000  cloudcore-debugger:latest sh
Copy the code

The preceding configuration enables the debugging mode.

PS: Code word is not easy, welcome to like the collection ~