Using Docker Containers for Development

I recently bought a new computer with 64GB of memory because I was always maxing out my other at 16GB.  I work on a lot of different software simultaneously for developing applications.  I might be building an iOS app in XCode while building the API in vs code and running multiple microservices inside Kubernetes for the app to communicate with.  Plus I always end up having a million browser tabs open which love to eat memory.   

Anyway I had a clean slate and wanted to keep this new computer organized.

What I Wanted

  1. Sandbox my development environment for my different applications. 

    I wanted to be able to run my work applications next to my weekend and hobby projects without fear of one interfering with the other.  Yes you can use tools like asdf, pyenv and rbenv to help manage your versions.  But even then you’ll run into issues with application dependencies requiring different system dependencies like Postgres and OpenSSL.   

    If you upgrade to the latest version of some software only to find out it breaks your application, removing it and installing the previous one can be challenging.

    I want to be able to easily remove software and not worry that there will be leftover artifacts.

  2.  Make it quick and easy for someone on my team to be able to get started.

    Setting up a new computer for someone or installing the right tools to be able to use a new application can be a pain.  I’ve seen developers take days trying to get all their applications running at a new job.

    Using a tool like Skaffold definitely helps (https://skaffold.dev/).  Having a developer be able to open an application and run `skaffold dev` and be able to start developing right away is very nice.   You’ll still need to have the right version of skaffold installed and you’ll need to install all of your dependencies locally in order to have things like code completion within your IDE.   

  3. Keep a consistent environment between everyone working on a project.

    This can help to mitigate some of the “it works on my machine” issues.    

    Maybe your app is a python script that needs to be run with a different version of python than comes on the computer by default.   You can use tools like asdf and pyenv with virtualenv, but everyone will still need to download the same version to be consistent.  You’ll have to make sure everyone is using the same tool in order to know if your application needs a .tool-versions or .python-version file.

Upgrading software is hard to sync across developer environments.  Not too long ago I started working on a new application that someone else setup.  I ran the command to start it but it didn’t work.  Looking into the code it looked like the format was wrong but the guy already told me he had it running.  Turns out he was using a newer version of Helm that had a different syntax.  I had to upgrade mine and then update all my other applications to use the new format.   That’s exactly what I don’t want to happen. 

What Do I Do?

It seems that a docker container is exactly what I need.  But is there an easy way to develop inside them?

So I set out on my quest to find a solution.  After a 10 second google search, my quest was finished!  I came across Visual Studio Code Remote – Containers. https://code.visualstudio.com/docs/remote/containers. This was exactly what I wanted, a simple way to create development containers and attach my IDE to them.

If you’re not using Visual Studio Code, I recommend trying it out, otherwise you’ll need to search for ways to edit inside docker containers for your IDE.

The documentation for it is very thorough with some good examples.

Simply install the extension and when you open up a project you can choose to open it up in the container.  If it’s the first time it will build the image and container for you and then attach your window to it.  If you already built the container then it will attach to it immediately.

You can see an example development container I use for a lot of my projects at https://github.com/frenzylabs/devcontainer.

I keep all my project files on my computer and then just mount the volume into the container.  I might create a docker volume for node modules or crates to speed up the some of the file access, it just depends.

Some Other Benefits

1. You can keep your development environment very close to production.

2. You can monitor memory/process usage very easily with command `docker stats`

3. It makes cleaning up your computer much easier.  If I need more space it’s easy to find what items are taking the most space and which ones are in use.  I can run `docker system df -v`  to get details on images, containers, volumes and cache sizes.

4. If you do accidentally delete everything you can easily build it all again!

Go give it a try!