Neuroengineer’s Garage

Make your code last forever

Setting up Docker + devcontainer in your neuro-code

Vineet Tiruvadi

--

Containers are great, even for your code. You put your code into a container, it can last forever.

Pretty self explanatory…

Let’s talk briefly about what a container is, and why you should set up a container for all of your code this very instant and forever.

What is a Container?

Containerization sets up an isolated sandbox for our code to work in, giving us a chance to develop (or get it running again) in peace.

By separating the code’s environment from our day-to-day environment, we can develop code in a stable way while still updating your system as needed for your other goals.

A container basically sits between your App(s) and the Operating System (Windows, Mac, Linux, etc.) and the hardware below. Source: https://www.docker.com/resources/what-container/

One of the most popular approaches to containerization is Docker — a powerful tool that you feed instructions to so it can build an image of your code+environment that runs anywhere that Docker itself does.

If you’re wanting a bit more detail about all the pieces and parts of this puzzle, I’ll cover that in a later post.

Setting up a Devcontainer

Setting up a container is actually very easy, so let’s walk through the basic steps here of setting up a devcontainer.

A devcontainer is a special type of container that is actually very simple and straightforward since it doesn’t need to worry about deployment or production or some Greek God called Kubernetes or whatever.

It’s a simple box for us to load in libraries related to our code and then develop our code in peace.

What you need

Both Docker and VSCode are free. Remote Containers Extension is free as well, but VScode specific.

There are alternatives to both, and I encourage you to find them, but as of right now this is really the easiest way to containerize your code. I hope this changes quickly and we get more variety outside of the Microsoft bubble.

Step 1: Open up your folder in vscode

  1. Run vscode, go to “Extensions”, search for “Remote Container…” and install the extension.
The steps, from left to right, to install Remote — Containers extension

2. Then go to File -> Open Folder. Find your project folder (hopefully you separate your code into projects) then open it up.

An old project from years before I knew about containers

If you’re like me, you’ll be staring at a bit of a mess. Don’t worry — first things first.

3. Press Ctrl+Alt+P (Mac will be different) and type in “devcontainer”. You’ll see this option. Select it.

4. Choose a Programming Language that your code predominantly uses. For the sake of this example, let’s assume it’s Python. Select “Python” and then the version of Python your code was developed on. This last part is very important — indeed, it’s the point.

Steps to setup a template devcontainer configuration

You’ll end up with an interesting document called a devcontainer.json — this is one set of instructions on how to build the devcontainer.

If you keep this, and the other Dockerfile, alongside your code, then you’re on your way to having timeless code.

5. Go ahead and click yes to all the rest of the defaults — you can customize your devcontainer + associated Dockerfile later.

Step 2: Reopen your folder in vscode + container

1a. At this point, your vscode may detect that you’ve got a devcontainer and ask you if you want to reopen the folder inside of it. Remember, we’re still out in the system environment.

This might pop up in the bottom right.

1b. If you don’t see the message, press ctrl + shift + P and start typing “Rebuild…” Then press Enter.

We have instructions to build the container, but the container hasn’t been made yet. After doing 1a or 1b, your container will be built.

“Starting Dev Container” is your system building the devcontainer from the base Python image you specified, and any other instructions in the devcontainer + Dockerfile.

If there’s an error at this point, make sure Docker is actually running: https://docs.docker.com/get-started/

After a bit of a pause, we’re back… But something’s different.

What your window should look like (generally)

At the bottom left you’ll see “Dev Container:…” This is a good sign — it means you’re developing inside an isolated environment.

Step 3+: Setting up your environment

At this point, there are a lot of things you can do, including just getting back to coding.

One of the first things I do is setup a code branch for my git repository, commit my new devcontainer.json into it, and then start adding the Python packages that I know I need.

This whole filesystem is inside your container and separate from your system’s. Get your container’s Python environment filled with the libraries you need for your code.

Remember: our container already contains the version of Python we wanted, so it’s ready to go. We can start pip install-ing packages or we can get modern and set up poetry.

Another thing I do is set up my favorite extensions and development tools — this is just something you build preference for slowly.

Parting Thoughts

By setting up a separate, buffered box for your code, what we call a container, and keeping your instructions to build that box alongside your code, you’ve gone a long way to future-proofing your code.

Containerization is a great thing to do from the very beginning of a coding project. Don’t worry if you didn’t start with one though, the same basic steps actually work for an established project. That’s what we did above — it didn’t change the steps.

You can do the same thing with various other programming languages (Julia, Haskell, C/C++, etc.) so this is actually a great way to learn a new language or prototype some code without worrying about screwing up your system — you can always just rebuild your container from scratch!

--

--

Vineet Tiruvadi

Engineer. Emotions, brains, and data-efficient control. Community > Technology.