Today we are going to take that same application, Dockerize it and deploy it to AWS Elastic Beanstalk. "What about Google App Engine", you ask? Unfortunately, I believe that App Engine only supports Docker in closed "preview" mode at this time so maybe somewhere down the road we can take look at it.

Getting Started with Docker

Docker's documentation is top notch and they even have a 10 minute "try docker" online tutorial that's completely browser based. Once you are ready to get started you can install docker on your machine and fire it up. I'm not going to go over the basics of Docker, their User Guide does a great job of getting you up and running.

The first thing we need to do is create our image. We can either:

Update a container created from an image and commit the results to an image to customize it.

Use a Dockerfile to specify instructions to create an image.

We are going to do the later as it seems much simpler to build an image and easier to share them.

Here is our main Go file that we will be Dockerizing. The only difference from our Heroku version is that we've specified port 8080 for Amazon. We'll need to add two files to our application:

Dockerrun.aws.json - to deploy your application to AWS. (Note, I think if you specify your port in the Dockerfile that you don't need the Dockerrun.aws.json file.)

Since the Dockerfile is the heart of the Docker process, let take an in-depth look at it:

FROM google/golang
WORKDIR /gopath/src/github.com/topcoderinc/cribs
ADD . /gopath/src/github.com/topcoderinc/cribs/
# go get all of the dependencies
RUN go get github.com/codegangsta/martini
RUN go get github.com/codegangsta/martini-contrib/render
RUN go get github.com/codegangsta/martini-contrib/binding
RUN go get labix.org/v2/mgo
RUN go get labix.org/v2/mgo/bson
RUN go get github.com/topcoderinc/cribs
# set env variables to mongo
ENV MONGO_DB YOUR-MONGO-DB
ENV MONGO_URL YOUR-MONGO-URL
EXPOSE 8080
CMD []
ENTRYPOINT ["/gopath/bin/cribs"]

The first line tells Docker what to use for our source image. In this case Google was kind enough to bundle the latest version of golang installed from golang.org into a base image for us so we'll gladly use it.

FROM google/golang

Next we'll set the WORKDIR, which sets the working directory for any RUN, CMD and ENTRYPOINT instructions. We are going to set it to the root of our source files.

WORKDIR /gopath/src/github.com/topcoderinc/cribs

We'll next use the ADD instruction to copy our source code to the container's filesystem for our source directory.

ADD . /gopath/src/github.com/topcoderinc/cribs/

There are a number of dependencies that we need for Martini so we'll have go get them and add them to our image:

RUN go get github.com/codegangsta/martini
RUN go get github.com/codegangsta/martini-contrib/render
RUN go get github.com/codegangsta/martini-contrib/binding
RUN go get labix.org/v2/mgo
RUN go get labix.org/v2/mgo/bson

Finally, we'll get our source code and install it.

RUN go get github.com/topcoderinc/cribs

Since our application still uses the MongoDB sitting on Heroku, we'll use the ENV instruction to set our environment variables we'll need to connect to MongoDB. We would have been better off if we would have created a MongoDB container and linked to it but we'll fight that battle another day.

Make sure you change these values before building your image.

ENV MONGO_DB YOUR-MONGO-DB
ENV MONGO_URL YOUR-MONGO-URL

Another major difference with Elastic Beanstalk, is that we declare the port we are using for our application:

If you run a container with an exposed port, then you should be able to access that server using the IP address reported to you using the following. Typically, it is 192.168.59.103, but it can change as it's dynamically allocated by the VirtualBox DHCP server.

Once we are done running our container we need to of course shut it down using either the container id or name:

$ docker stop stupefied_archimedes
stupefied_archimedes

Deploying to Elastic Beanstalk

Deploying our app to Elastic Beanstalk isn't as fast nor as easy as Heroku but it's relatively painless. Once logged into Elastic Beanstalk, click Create New Application in the upper right to get started.

Enter the Application Name and hit Next. For the Environment tier select 'Web Server' and for Predefined configuration select 'Docker'. Hit Next.

Now we need upload our source code. Zip up the Dockerfile, Dockerrun.aws.json, server.go and the /templates directory into app.zip. Now choose the middle radio button, upload the zip file and hit Next.

Just accept the defaults and hit Next for the next four pages. When you are finally done, scroll down to the bottom of the Review page and click Launch. Now wait 10 minutes or so for the little wheel to stop spinning and your environment and application should be configured and deployed successfully!