Bibbidi Bobbidi Boo… I Dockerize you!

Intro to Docker

Music Meier
4 min readJan 6, 2021

Go ahead! Wave your wand and help others to experience your code more easily.

So why Docker? Why go the extra mile? Docker eliminates the hassle of application deployment and configuration while giving the benefit of portability. Once an image is built your application is ready to be run anywhere. Build once, run everywhere! With Docker, there is no need to worry about installing the proper languages, the right plug-ins, or even the correct version. It takes in the code and combines it with anything you need to run the code. All you have to do is build the container. How does one get started you ask? Simply click: here to download and install Docker and then follow along with the steps below.

Prior experience with Node can be minimal, your fairy God mother will walk you through this step by step. Let the magic begin. Start by creating a new directory and then cd into the folder and initialize the app.

Next, you will need to install express and then add an index.js file. Once you have completed those steps, make your way into the index.js file.

Once inside the index.js file, require in express and invoke it. Then, create a get method in order to send a message to your browser. Don’t forget the app needs to be listening on a port.

I chose port 2021 but feel free to choose any port you are not already using.
I chose port 2021 but feel free to choose any port you are not currently using.

To assure we haven’t broken anything, run: “node index.js in your terminal. Then, go to your browser and type in http://localhost:2021/ or whatever port number you chose. The message from your code should appear on the screen like so:

Let the fun begin by adding a Dockerfile. Be sure to capitalize the “D” and leave off any extensions you may be tempted to add.

Hop inside the Dockerfile, inside this file holds the instructions on how to build a computer that will run the app. First, grab a node image. The official name for this is “base image.” Then, there needs to be a working directory(a fake computer that is used to run the app). The port needs to be exposed so the laptop in use can see the app when it is running. Following that, be sure to add everything from the the current folder into the app, and then, share the command necessary to run the app.

Now for the magic. To officially build the container, run the following command in your terminal: “docker build -t our-first-docker-app:test -f Dockerfile .”. Don’t forget the period at the end. Without that, the exact location of where to run is unknown. When it is done building, use this run command: “docker run -p 2000:2021 -t our-first-docker-app:test”. Voila! Congratulations, you have officially Dockerized your first app. In your browser, got to http://localhost:2000/ to view your message again. Once you are finished gazing at your handy work, inside another tab in your terminal, run: “docker container ls”. In the top left under NAMES you will see a hash. Copy that and run: “docker kill” followed by the hash you just copied. With a wave of the wand that ends your first official Docker session. Don’t let it run for too long, it may turn back into a pumpkin. Until next time, happy coding!

--

--

Music Meier

Full stack developer with a Masters in Classical Piano Performance and an affinity for travel, learning, and creating.