Once you learn the basic commands, building a Docker image is fairly simple.
There’s only 2 options required in a Dockerfile, after all:
FROM <base image>
That’s it. Now you can “docker build .” and move on, right?
Technically, yes – you have a complete Dockerfile and you can build an image… but it might not run without further configuration (like copying the files that are to be run, into the image).
It’s that need for additional configuration that tripped me up, recently.
I wanted to deploy a Node.js app to server using Docker, so I grabbed my editor and started writing the Dockerfile for the image.
I set a base image that includes my preferred linux distribution, with the right version of Node.
I copied the code into the image and ran npm install.
I called the node runtime and told it which script to start.
Then I built the image, tried to run it, and … error.
Oh, wait… working directory… rebuild, and ERROR?
Oh, right… I forgot to set NODE_ENV environment variable… so I’ll just rebuild now, and … ERROR
ANOTHER ERROR?! BAH!!!
sigh… right, there’s also …
It took about an hour for me to get the image built and working correctly, but I did get it working.
When I saw the working Dockerfile, I realized something.
My desire to deploy a Node.js application got me to copy some code and tell node which script file to run.
But that wasn’t sufficient.
I had to do more, to configure working directory, and install dependencies, and setup port numbers, and …
Dockerfile I had sitting in front of me resembled something more than just a script to start a node application.
It looked like an automated server configuration script – something my devops friends would use to build and deploy a new server.
I don’t know why I didn’t realize this before, but it was plain to see now.
Docker forced me to improve my understanding of Devops.
Clearly, there’s more to DevOps than just configuring servers or Docker images.
But even if this is just the first step into what has previously been a mystic realm (to me, at least), it’s an important step.
Understanding how servers are configured and what it takes to run an application in production is a necessary part of our job as software developers.
Knowledge of production configuration and constraints inform our decisions around file system organization, runtime dependencies and other forms of software architecture.
Docker can help you understand those requirements and constraints.
And WatchMeCode can help you understand Docker.
Take the first steps toward DevOps and better development tooling.
With the Guide to Learning Docker, you’ll be up and running with Docker in no time.
With the (FREE!) Docker Cheatsheets, you won’t have to memorize arcane command-line options and configurations.
And with the Guide to Developing Node.js Apps in Docker, you’ll take your new skills and apply your existing development knowledge and tooling.
Solve the “works on my machine” problem, permanently.