Why and How To Use Docker for Development

Compile it in a Java 1.

6 Docker image.

Deployment is easy.

If it runs in your container, it will run on your server just the same.

Just package up your code and deploy it on a server with the same image or push a new Docker image with your code in it and run that new image.

Can still use your favorite editor/IDE as you normally do.

No need for running a VM in VirtualBox and SSHing in and developing from the shell just so you can build/run on a Linux box.

How To Use Docker for DevelopmentIt’s not much different than what you normally do for development, except for two things:Ensure all your dependencies are in your working directory, along with your code.

Change your build and run commands to run inside your Docker container.

I’ll show how to do this with a few different languages, starting with a long explanation in Ruby, then quick run-throughs in other languages.

RubyLet’s start with a very simple Hello World example in Ruby.

You don’t even need Ruby installed to run this.

Copy the code below and put it in a file called hello.

rb.

Now run it:It should print “Hello Ruby!”.

Easy peasy.

This is the really the magic command you’ll use for almost everything.

Now let’s add a dependency just to show how vendoring your dependencies works so you can run it inside the container easily without having to install the dependencies into the container.

Copy this into a file called Gemfile:And change hello.

rb to this:If you run bundle install to install the gems, then run ruby hello.

rb, this will work — but if you run it inside the Docker container it will fail because the iron_mq gem isn’t installed inside the container.

We could install it inside the container, but then you have to manage the container, remember the state of the container and how you got it to that state before deploying your code to another server, etc.

I like my containers ephemeral.

You’re much better off vendoring your dependencies and keeping all your code and dependencies all together in a nice little package.

So instead, let’s run this:This will install your dependencies into the working directory and, as a bonus, if there is anything that needs compiling (native extensions), they will be built on the right architecture!Now one last change to hello.

rb to use this new bundle, notice the require_relative line at the top:Now let’s try running it again with our new dependency:All good!.How about running it on a different version of Ruby, like Ruby 1.

9?.Just run it in a different container:Web appsIf you’re making a web app and need to open ports, just pass the -p PORT:PORT flag to docker run.

Here’s an example web app to try it out.

Copy this into webapp.

rb:Add gem ‘sinatra’ to your Gemfile, run the bundle install command above again.

Then run this command:And go to http://localhost:8080 to check it out.

That’s about it!.Most of these things apply to every language, with some minor differences.

There are a few more language examples below, without all the details, and here’s the full source code for all of these.

JavaSimilar to the Ruby example, get your dependencies (jar files) into your working directory, then compile your program in the container and run it.

The example repo has the dependencies for this example.

Copy this into Hello.

java:Compile it in a container:Run it in a container:NodeNode is pretty nice since npm install defaults to installing dependencies in the working directory.

First, install our dependency:Now copy the program into a file called hello.

js :And run it:GoCopy this code into a file called hello.

go:Now run the following lines to get dependencies, build it and run it:Boom.

One Final TipIn all the previous examples, the container is deleted right after it runs ( — rm) flag.

To keep the container for whatever reason, maybe you a dependency that takes a while to build, like RocksDB, you can use this trick:This will do a docker run the first time you start the container and docker start on the existing container after the first run.

If you tried any of the examples above, you can see that it doesn’t change your development process too much and can provide many benefits as listed at the start of this post.

And obviously, all of this can be applied to other languages as well.

Give it a try — would love to hear what you think about it.

.

. More details

Leave a Reply