This article refers to and demonstrates Azure Container Instances. If you don’t have an Azure account, get a free one and enjoy some free Azure on the house.
I have always felt that Docker is like the theory of relativity: everyone talks about it like they understand it, but nobody has any idea what it actually is.
Well I still don’t know exactly what Docker is, but I do know what it does, and that at least makes me smarter than Steve.
What Docker Does When It’s Doing Things
For those who are just catching up, Docker bundles your code and its dependencies inside a “container”, which can then be deployed to a server where it “just works”™.
You know that it will work because only the container matters. You are not dealing with an entire operating system and all of it’s variables, versions, configurations ect. It’s like deploying an executable, except maybe you’re deploying an entire web server. If it runs on your machine, it runs when you deploy it
How this works I have no idea.
Docker seems like it’s defying the laws of physics — much like the theory of relativity. But hey, while I’m not here to judge, I am here to start sentences with the word “but”.
What I’m trying to say is that if light always travels at the same speed relative to your current speed because time slows down the faster you go, then I guess it’s not a stretch to accept that Docker works however it works and that’s ok.
Put Some Node In Your Docker And Buzzword
Let’s look at how to deploy a Node / Express application as a Docker container and we’ll even put it up on the web for everyone for everyone to bask in the blinding light of our genius. Mr. Shayne Boyer gets a huge chunk of the credit for this post. He guided me out of the land of darkness and tears.
The easiest way to do this is to follow the docs which walk you through creating an Express app cloned from Github and adding a Docker file.
Here’s a brief breakdown…
1. Install Docker.
I feel like you should have seen that coming.
2. Create an Express app
For this example, I’m going to be using the express-react-starter project because it’s perfect for scaffolding a React/Express project. Also I wrote it and my self-esteem is fragile.
Make sure you build all of your assets to your build folder. This is the folder that we will containerize. If you are using express-react-starter (why would you not be?), run
npm run build.
3. Create a
If you are using express-react-starter, it’s already in there because as I said, it’s the best thing ever. If not, just create a new file called
Dockerfile with no extension. What is with the new trend in files with no extension? Like Prince or Madonna with no last name.
4. Configure the Dockerfile
It should look something like this…
Not to go into too much detail on what is going on here (but watch me), but let’s talk about a few things…
- Create an app directory: This can be anything you want. I don’t know why people do this, but
/user/src/appseems to be the standard configuration and if it’s on the internet, it must be right.
- Install deps: Notice that we are copying package.json over first and doing an npm install and THEN copying in our build directory (in this case the
- Set PORT: In development, Express runs on port 3000. In production it looks for the PORT variable.
5. Build it
> docker build -t yourname/image-name .
6. Test it out
In production the app will run on port 80. However, we cannot run it on port 80 in Docker because that’s a restricted port. That’s information I could have used 4 hours ago, but my pain is your gain. To test, map to a different port — 3000 works fine. Then visit
localhost:3000 in your browser.
> docker run -p 3000:80 yourname/image-name
Note that you have to press
ctrl-ctwice to exit out of a running Docker container.
Me — After an embarrassingly long amount of time
7. Push It
Now we need to push our image to Docker Hub or Azure Container Registry. Docker Hub is great for things that are public, although it does private as well. Azure Container Registry is more of a private repository. Use whichever one you want. I’m not going to tell you how to live your life.
8. Deploy It
Now we can deploy this thing to the web. We’ll use Azure Container Instances because we can deploy there with one command and not have to worry about anything else. I have too much drama in my life as is. Just ask Steve.
// login to Azure
az login// create a Resource Group
az group create -n your-group-name// create the container
az container create -n your-app-name -g your-group-name --image your-image-name --ip-address public
Azure will create a new container instance and you can check it’s status by executing
az container list. When it moves to
Succeeded you are are ready to go.
Since we requested a public IP, we can immediately hit the site.
And we’re done here.
What’s The Catch?
I would like to go on record saying that Docker sounds too good to be true. I want to know what the catch is. Where is the fine print that tells me this doesn’t actually work. Like when you were a kid and you saw a commercial for a toy airplane and they showed it flying, but when you actually buy it you find out that YOU ARE THE ONE THAT MAKES IT FLY.
All to say, Docker is crazy. Am I the only one who thinks this is the greatest thing ever? Or am I just the last to know. It’s high school all over again.
If you liked this, make sure to check out all of the other cool things you can do with Node.js on Azure. The possibilities are endless! Actually, there are a finite number of permutations, but it’s a metaphor.
P.S. If you would like to actually understand the Theory Of Relativity so you can impress your friends (they won’t be impressed), check out this video.