Dev Blog

Home Pi PicoSystem Dev Server OS & Networking Thoughts Misc Pi Microcontrollers C & Embedded Web Dev

C Dev Environments ft. Docker & Kubernetes, Vim, VSCode & SSH

Today let's cover some more advanced topics; setting up C projects for Vim using Docker and Kubernetes, with a bash script that generates the project based on user input. We'll also cover how to achieve the same with VS Code, using SSH with VS Code to develop remotely, and explore using Vim through an SSH tunnel. By the end of this blog post, we will have some seriously powerful and adaptable C development environments tailored to our workflow. If you can work your way around the command line, have a basic understanding of Vim & writing bash scripts, you should be able to follow this guide with ease.

Multiple SSH
Connecting to CM5 & Pi Zero2 via SSH

This is a long guide, so grab a beverage, get comfortable and let's get started improving your workflow and efficiency.

Why?

Developing C programs in these environments ensures your project is deployable across a myriad of machines, is easily maintainable and scalable. Development is simplified with the use of Docker and Kubernetes, projects are remotely accessible via SSH and remain completely consistent. Bash automates the entire project generation process giving us error free, consistent boilerplates. This doesn't just apply to C projects either, you can do the same with pretty much any language and pretty much any environment, I'm using C as an example because bar JavaScript and PHP, it's my favourite language.

Now that's apt

Before we get started, we need to ensure we have the following packages installed on our system:

Apt
Apting packages

Docker: To containerize our development environments.
Kubernetes (kubectl and minikube): For deploying containers.
Vim: For editing code.
Bash: For scripting.
Git/Github: For version control.
Dev Packages: Essential Development Packages such as build-essentials or gcc.


Bash Scripting for Project Generation

Let's create a bash script that generates a new C project based on user input. This script will set up the project structure and initialize a Git repository.

Create the Script File

touch create_c_project.sh


chmod +x create_c_project.sh

Creating C Dev Envs
Creating a C Dev Env

Download sh script


Dockerizing the Development Environment</h1>

Docker enables us to create consisent dev environments across mutiple machines. In your project directory, create a Dockerfile.

cd projectname
touch Dockerfile

Download Dockerfile

Build the docker image with this command

docker build -t projectname-image .

Run the Docker Container:


docker run -it --name projectname-container project-image

This command starts the container and opens Vim inside it.

Deploying with Kubernetes

Let's deploy our Docker container using Kubernetes to orchestrate it.

Start Minikube

First, ensure Minikube is running.

minikube start

Create a Kubernetes Deployment

Create a file named deployment.yml

Apply the Deployment

kubectl apply -f deployment.yml

Expose the Deployment

kubectl expose deployment $PROJECT_NAME-deployment --type=NodePort --port=8080

Accessing the Application

Since our application is set to run Vim inside the container, we can exec into the pod.

kubectl get pods

kubectl exec -it \<pod-name\> -- /bin/bash

Now we're inside the container's shell and can run Vim or any other commands.

Setting Up C Projects for VS Code with Docker and Kubernetes

Let's begin by configuring VS Code for our C projects.

Configuring VS Code for C Development

Ensure you have VS Code installed along with the following extensions:

C/C++ (ms-vscode.cpptools):
Provides C/C++ language support.
Docker (ms-azuretools.vscode-docker):
For Docker integration.
Kubernetes (ms-kubernetes-tools.vscode-kubernetes-tools):
For Kubernetes support.</sp
</p>

Docker Integration with VS Code

VS Code can interact directly with Docker to build and run containers. In your project directory, create a .devcontainer folder with two files: devcontainer.json and Dockerfile

devcontainer.json
Dockerfile

Open the Project in a Dev Container

1. Press F1 in VS Code.
2. Select **Remote-Containers: Open Folder in Container...
3. Choose your project folder.


VS Code will build the Docker image and reopen the project inside the container.


Automating Project Setup with Bash Scripts

Create a script create_vs_project.sh that automates the setup.

Using SSH with VS Code

VSCode has robust support for remote development over SSH, allowing us to work seamlessly on remote machines. Extremely useful for purposes like mine, using a CM5 DevServer to develop remotely... I can work on entire projects in any language, in any environment where ever I am.

VSCode SSH
VSCode & SSH

Setting Up SSH Connections

First, we need to install the Remote Development Extension Pack, which can be located in the Extensions Marketplace
(Ctrl + Shift + X):

Remote - SSH (ms-vscode-remote.remote-ssh)


Core extension for SSH.</p>

Configure SSH in VS Code

1. Press F1 and select Remote-SSH: Add New SSH Host...
2. Enter your SSH connection string, e.g. mine: sysadmin@cm5.local
3. Choose the SSH configuration file to update (usually ~/.ssh/config).

Connecting to SSH
Connecting to SSH with VSCode

Remote Development over SSH

Press F1 and select Remote-SSH: Connect to Host...

VSCode SSH
VSCode & connect to SSH host
Choose your configured host.
VSCode SSH
VSCode & CM5 SSH
VS Code will connect to the remote server. You'll see [SSH: remote-server] in the bottom-left corner.

VSCode SSH
VSCode & connected via SSH to cm5.local

Enhancing Workflow with Extensions

Install Extensions on Remote:
When connected to the remote server, install any necessary extensions. VS Code will prompt you to install them on the remote host.
Work with Remote Files:
Open folders and files on the remote server as if they were local.
Integrated Terminal:
Use the integrated terminal to run commands on the remote machine.

Using Vim through an SSH Tunnel

When you need to edit files on a remote server using Vim over SSH, tunneling can enhance security and flexibility.

Vim SCP
Connected to CM5 with Vim via SCP

Establishing an SSH Tunnel

An SSH tunnel forwards a local port to a remote server.

Basic SSH Connection

ssh user@remote-server

ssh</codde> Tunnel Command

ssh -L local_port:localhost:remote_port user@remote-server

SSH Tunnel
SSH Tunnel

Example

Forward local port 8080 to remote port 80:

ssh -L 8080:localhost:80 sysadmin@cm5.local

Vim for Remote Development

Simply SSH into your remote server and run Vim to edit files.

ssh user@remote-server

vim /path/to/your/file.c

Using Vim's Native Remote Editing

Vim supports editing remote files using protocols like scp and sftp.

vim scp://user@remote-server//path/to/your/file.c

Vim SCP
Connecting to CM5 with Vim via SCP

Note the double slashafter the hostname.

Leveraging Vim Plugins for Remote Work

Enhance your Vim experience with plugins designed for remote development.

vim-scp

vim scp://user@remote-server//path/to/your/project/

Now you can navigate directories and edit files as needed.

Wrapping it up

We've journeyed through setting up C projects for Vim and VS Code using Docker and Kubernetes, automated project creation with bash scripts, and explored remote development over SSH in both editors. By harnessing containers and remote connections, we can create a consistent, portable development environment that suits our workflow, we have achieved a lot in this post so be proud of yourself if you got this far and start to utilise some of the aspects we've discussed here.

Catch you next time
Dru


</div>

Blog GitHub LinkedIn ORCID Stack Overflow