:::info This will work for c and c++. You will need 2 ide extensions, one local (on your machine) and one remote (in the container). :::
- Have the Docker daemon running on your machine.
- Install the Dev Containers Extension locally.
- Jump into the repo root and hit
cmd + shift + p
and selectReopen in Container
. - Install the CodeLLDB Extension in the container (if not done automatically by the devcontainer.json config file in the root of this repo).
- Open any c or c++ file and go to the debugger window
cmd + shift + d
. - Click on the green play button to run the launch configuration
lldb
.
C++ is a superset of C, just extern C functions in C++ files due to the way C++ handles overloading.
extern "C" {
void my_c_function();
}
:::warning You will be prompted to install the C++ extension in the container, but it is not strictly necessary with this setup. :::
- lldb means llvm debugger; it hooks into the compiler architecture of llvm which is the next generation of compiler for c/c++
- clang++ uses llvm and is the compiler for c/c++
- a c/c++ file needs to be compiled with the -g flag to enable debugger metadata, which is then used by lldb
- the launch config does just what a terminal session would do but dynamically for the active file in the ide and it runs the build task (clang++ with -g flag) beforehand to enable debugging in the window for active file.
- .vscode/launch.json
// launch config
{
"version": "0.2.0",
"configurations": [
{
"name": "lldb",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"cwd": "${workspaceFolder}",
"preLaunchTask": "clang++"
}
]
}
- .vscode/tasks.json
// build task
{
"version": "2.0.0",
"tasks": [
{
"label": "clang++",
"type": "shell",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
]
}
]
}
- Dockerfile
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
build-essential \
clang \
gdb \
cmake \
git \
curl \
vim \
lldb \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN pip3 install --upgrade pip
RUN pip3 install matplotlib numpy
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 10
CMD ["bash"]
- .devcontainer/devcontainer.json
// https://code.visualstudio.com/docs/devcontainers/containers#_create-a-devcontainerjson-file
{
"name": "Existing Dockerfile",
"build": {
"context": "..",
"dockerfile": "../Dockerfile"
},
"customizations": {
"vscode": {
"extensions": ["vadimcn.vscode-lldb"]
}
}
}
chmod +x ./kustomize-deploy.sh
./kustomize-deploy.sh
kubectl apply -f <filename>.yaml
kubectl exec -it <pod-name> -- /bin/sh
kubectl describe pod <pod-name>
kubectl get deployments
kubectl scale deployment <deployment-name> --replicas=0
kubectl get pods
kubectl delete pod <pod-name>
kubectl delete deployment <deployment-name>
kubectl delete pods --all
Generate a Personal Access Token (PAT): Navigate to GitHub Settings. Click on "Generate new token". Select Scopes: For public repositories: read:packages write:packages delete:packages (optional) For private repositories: All the above scopes. Example Name: ghcr-access-token Click "Generate token" and copy the token. Note: You won't be able to see it again. Login to GHCR via Docker CLI: Open your terminal and execute:
echo <YOUR_PAT> | docker login ghcr.io -u <github-username> --password-stdin
docker build -t <image-name>:<tag> .
docker tag <image-name>:<tag> ghcr.io/<username>/<image-name>:<tag>
docker push ghcr.io/<username>/<image-name>:<tag>
apiVersion: apps/v1
kind: Deployment
metadata:
name: busy-knuth-deployment
spec:
replicas: 1
selector:
matchLabels:
app: busy-knuth
template:
metadata:
labels:
app: busy-knuth
spec:
containers:
- name: busy-knuth-container
image: ghcr.io/bronifty/busy-knuth:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh"]
args:
- "-c"
- |
echo Container started
trap "exit 0" 15
exec "$@"
while sleep 1 & wait $!; do :; done
- "-"
env:
- name: PATH
value: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
- name: DEBIAN_FRONTEND
value: "noninteractive"
volumeMounts:
- name: c-debugging
mountPath: /workspaces/c-debugging
- name: vscode
mountPath: /vscode
volumes:
- name: c-debugging
hostPath:
path: /Users/bronifty/codes/temp/c-debugging
type: Directory
- name: vscode
persistentVolumeClaim:
claimName: vscode-pvc
docker run -it ghcr.io/<username>/<image-name>:<tag>
kubectl apply -f deployment.yaml
kubectl get pods
kubectl describe pod busy-knuth-deployment-xxxxxxxxxx-yyyyy
kubectl exec -it busy-knuth-deployment-xxxxxxxxxx-yyyyy -- /bin/bash
kubectl logs busy-knuth-deployment-xxxxxxxxxx-yyyyy
kubectl get pods
kubectl describe pod busy-knuth-deployment-xxxxxxxxxx-yyyyy
kubectl exec -it busy-knuth-deployment-xxxxxxxxxx-yyyyy -- /bin/bash
kubectl logs busy-knuth-deployment-xxxxxxxxxx-yyyyy