Hand-On — Containerizing Development Environment with Visual Studio Code
I was having this problem since few months and I guess most of you are having the same issues.
Do you do development of multiple languages on your laptop and have trouble managing all the environments and dependencies of the projects?
This is one of the biggest burden in my current development environment, specially with slow HDD read/write speed.
Just now, I had issues installing the graphics library files for my development environment as Pillow in python used another version and NodeJS used another version. Updating any of them would break another.
It would be lot better, if I could isolate my programming environment but I can’t run a whole virtual machine just to have a isolation.
I mean, I don’t need a new OS, I just want to isolate my development programs, tools and codebases. Another benefit it comes is that if you stop working in any of the projects in golang, and you can remove the whole development environment with just some commands, and you’re good to go.
Isn’t that already convincing enough to try containerizing your development workflow?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
What a container does is, it comes as a software but contains everything it depends upon. Like if a script requires a example.so linker files and a whole g++ installed to run, a container can include all of those and still remain lightweight than you expect!
This is all the magic of linux namespaces and cgroups. The concept of isolation of processes was thought long back then and kept in linux.
So, if you’re running a virtual machine, then it runs on top of a hypervisor such as virtualbox, hyper-v, vmware, but docker containers run on the same host, but do not share the same PID list and are isolated from each other.
Keep in mind, docker container are also built secure and has minimal security concerns to think of if you configure it properly.
So, docker is just a platform which is used to control the containers effectively.
Or, quoting from the wiki:
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.
You can research further about docker and containers.
So, when you convert your whole development environment inside a docker container, it’s totally isolated from the host operating system and also can be managed as a whole from the host operating system, or via docker cli.
I won’t cover installing docker in this article, but you can look it up here. Installing Docker :https://docs.docker.com/get-docker/
So, in order to convert your visual studio code setup into docker container. the first you need is to choose an docker image to run.
Keep in mind, docker images are the whole software shipped with dependencies which can run isolatedly forming containers.
The most common one to use is ubuntu (if you prefer a big community) or archlinux (if you prefer lightweight)
#if you want to run ubuntu, execute this after installing docker.
docker pull ubuntu:20.04
#if you want to run arch, execute this after installing docker.
docker pull archlinux
After you’ve pulled the images for your development environment, we’ll use a volume so it’ll be easier for you to share files, to and from the container.
docker run -d -it --name ubuntu-dev -v ~/my_files:/my_files ubuntu:20.04 /bin/sh # or /bin/bash
docker run -d -it --name arch-dev -v ~/my_files:/my_files archlinux /bin/sh # or /bin/bash
Now, you can check if your container is running using docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84bc37ed215b ubuntu:20.04 "/bin/bash" 54 minutes ago Up 44 minutes ubuntu-machine
The output should be similar to above one.
Now, you can open vscode and install remote pack extensions.
Install this extension pack, if you don’t have already which will allow you to setup vscode remotely in a container.
Now, you’ll see your container listed here. Just click on it and vscode will prepare a development environment in the container and make it ready to start coding.
Now, you can install all your compilers, interpreters and everything using the provided bash via package managers.
When you need to remove the development environment just remove the container and you’re good to go :)