How to use docker for your virtual development environment

I personally don’t like installing a bunch of development runtimes and software on a brand new computer, I’d rather that stuff be more modular and contained from my machine.

To remedy this in the past, I have used virtualbox, to create an Ubuntu environment with my main runtimes and languages, whilst also using shared folders to sync files with my Mac. However, after a while of constantly iterating provisioning scripts, and dealing with the hassle of a “remote” machine, I got sick of messing around. I just wanted to be able to run the languages I wanted, when I wanted.

That’s when I found docker.

Docker allows you to create small, efficient virtual machines whenever, and wherever I need to.

I won’t go into details about how Docker works here, as there are many better articles that explain how to get setup, I will explain how I use docker to work with different languages on my machine.

How to use NodeJS with docker

My language of choice is Javacript, so I work with NodeJS a lot of the time, so let’s say we start with a folder called “myapp” on the desktop…

First, I will change terminal directory into that folder, then I will run the following command:

docker run -it -v $PWD:/code node bash

Let’s break this command down a bit.

This well... runs docker

docker run

This let’s you run an integrated terminal, it’s pretty much the same as sshing into a remote server.

-it

This mounts your current directory to /code on the virtual machine.

-v $PWD:/code

node specifies which docker image to use. If it can’t find the image node on your machine, it will download from the docker hub.

node

bash is a run argument for docker which specifies that you will run bash when the container is running, without bash it will run the NodeJS REPL

bash

Using node from inside your container

Once you’ve run this command, you are put into a linux shell. In order to see your local working directory from within the container, if you followed the above command, you will need to

cd /code

Inside this folder you can run commands such as node and npm init and any changes will be reflected both on your container, and your local machine.

Working with other languages

You can use the same command to work with other languages that can be found on the Docker hub:

https://hub.docker.com/

docker run -it -v $PWD:/code [image] bash