Cloud Connections

Cloud Connections

cloud engineering, automation, devops, systems architecture and more…

26 May 2020

Using Docker to Setup Environments Quickly for Coding

I’ve started to learn couple of languages to get better at coding. But before you could start any actual coding, you would need to setup the environment for these languages/frameworks.

Let’s start by describing how I did things earlier. I’d use Virtualbox to create a virtual machine (VM), install the operating system (OS), install the necessary packages, get the binaries, figure out how to share a folder (that has my source code) on the host to the guest OS, enable networking so that I could load stuff of my browser. That’s quite a list of activities.

Now, you could automate all this using something like Vagrant and couple of scripts to bootstrap the machine. But there’s a few disadvantages I find with this method:

  • Having to wait a while to set things up (even though it’s automated, it still takes significant time until everything is ready)
  • Use of a VM starts eating up your resources, especially if you’re trying to use multiple services.
  • This would mean we need really high-end workstations (need moar RAM!) which equals more $$$.
  • Setting up shared folder between host and guest OS. This can get troublesome when you have multiple machines for different projects.

Don’t get me wrong, I think there’s still a place for VMs and using tools like Vagrant or Ansible to set things up. You could be testing a pre-built image, or replicate some infrastructure service like an appliance or cluster etc. But in this case, it’s not for the dev environment setup anymore, in my opinion.

Enter Containers

Docker Container
Containers enables the dev environment to skip the guest OS overhead, start up faster and save more disk space compared to VMs. Nick Janetakis has good comparison on how both containers and virtual machines differ. Check it out if you’d like to learn more about it.

VM vs Docker Container Comparison - Image credits to Nick Janetakis

Basic Usage

Let’s look a simple way to get an environment setup. Say you wanted to to just test a language and remove it once you’re done. You already have the instructions provided on the guide open in your browser.

Start with getting a base OS image. I usually go with an Ubuntu LTS image, since I’m more familiar with it. (But Alpine seems to be a good distro if you’re looking for smaller sized images.)

Pull down the image from Dockerhub:

docker pull ubuntu:bionic

In your project directory, run the container with some options which I’ll explain below:

docker container run --rm -it -w /app -v $PWD:/app -p 5000:5000 ubuntu:bionic

You’ll be taken to the container prompt where you start your installation.

Install Packages in Container

Now you can create your source files, run the application and watch the changes on your browser (or terminal depending on what kind of language/framework you’re testing)

Running app in Container

Important thing here is know what the purpose of the options are when you’re starting up the container.

--rm  # Automatically remove the container when it exits
-it # Allocate an interactive pseudo-TTY
-w /app # Set the working directory inside the container
-v $PWD:/app # Bind mount a volume. Format is hostDirectory:containerDirectory ($PWD passes the current host directory)
-p 5000:5000 # Publish a container’s port(s) to the host. Format is hostPort:containerPort

Once done, just exit and the container is gone, poof! (Note that you’ve lost all your work, which may or may not be a happy thing. 😨)

Get Going Even Faster

Now that you’ve tested a couple of setups and got the general idea of how Docker can be used, I’d like to show an even quicker way of getting things running.

Dockerhub is where you need to look if you want skip the manual commands required and get started with an already built image. You’ll be able to find pretty much a variety of images that can help you get started even faster. That’s the beauty of open source, it allows you to build on top of what others have worked on.

I’ll highlight a few specific languages/runtime examples that you can try out.

PHP

You want to build a PHP application. Great, head over to Dockerhub and search for the available PHP images. Pick one that’s suitable for your needs. These are usually based on the type of OS, size of the image, version etc.

Start up your container using:

docker container run --rm -it -v $PWD:/var/www/html -p 80:80 php:7.4-apache

Note: There are few changes to the command above because of the way the Docker image is built. Refer to the documentation provided by the maintainer to figure out which volumes, ports and other options to use.

Write your code. Run it! Awesome!

PHP Container

NodeJS

Ah, you want to be gain some back-end JS skills. That’s alright. Let’s pick a node image from Dockerhub and run it.

docker container run --rm -it -w /app -v $PWD:/app -p 3000:3000 node:12-slim /bin/bash

Write your code. Run it!

NodeJS Container

Conclusion

VMs does have its uses for testing things out. But using Docker to create your dev environments is much quicker, flexible and space saving. It allows us to skip a lot of work by using already built images available on Dockerhub.

You can focus your efforts on doing actual coding without having to worry much about the setup. If you use a Dockerfile to package up your application and check it into source control, you can be pretty sure that another developer can pick up on your work without having to run into complains like ‘it runs on my machine’.

If there’s any other Docker topics you’d like me to explore, feel free to reach out on Twitter.

Categories

Tags