Getting started with your first Docker file

Jumanah Al Asadi
4 min readJul 12, 2021

Alas! sailors, we are going to set sail on this journey to dockerize our nodejs app!

Real Life Scenario

You have just finished coding your web app on your local machine, let’s say a MacBook Pro, with these dependencies:

  • node v12
  • express
  • nosql database on the cloud
  • API keys

OK, great but now you want to run this application on your best friend's computer because it’s a group project (we all love those).

She tries to download everything and run it on her Windows machine (eww windows) but there are just so many errors. She screams:
Ugh.. How am I supposed to run this piece of sh*t now!!!”

Solution 1: You go to her house and help her with setting it up. You perform all the steps you did on your computer to get it to work. And you find missing things that weren’t installed on her computer.

Solution 2: You both install something called Docker. This will act as a common denominator between your machines.

Ok, what is Docker?

Docker is a solution that can run your app on a container in your local machines or on the cloud. Because Docker runs your app on a container, your computer is not really involved in running the app. All your computer needs is Docker installed. Docker will handle the environment set-up and dependencies.

Docker enables developers to package applications into containers — standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. SRC: https://www.ibm.com/cloud/learn/docker

So if you got that from those fancy terms, a container is like a little bubble with its own eco-system, running in isolation.

Photo by Caroline Attwood on Unsplash

The container itself is independent, so it can run anywhere where Docker is installed. It does not need a specific environment, all it needs is docker. In the picture above, your app is your muffin, and the packaging paper around it is all its dependencies. The muffin is the docker image. The yellow container is well the container. It’s okay if you don’t understand it yet, we will go into details about images and containers below. Hope you’re not hungry now. ;-)

Ok, so we realize how important docker is, so let’s go ahead and install it!

Install and open it. Make sure it is running, and ready to go. You will see a friendly whale come into your life. Greet it in a kind manner, for it is now your friend.

First step, defining a Dockerfile

Alright, Docker is running. How do I leverage docker? How do I get shit running on containers?

In the simplest way, you can define a cute little docker file.

All you need to do is go into the root of your project/app and create a text file called “Dockerfile” (no extension needed).

FROM node:12.18.0WORKDIR /appCOPY package.json /appRUN npm installCOPY . /appCMD [“npm”, “start”]

Ok so what went down in this file, seems like a bunch of statements, let’s summarize what happened:

  • We defined our node environment and version
  • We set a work directory (can be whatever you like)
  • We copied the package.json (which defines all our dependencies) into this work directory
  • Then we run an npm install to run all those dependencies
  • Then we copy all the files to the work directory defined
  • Then we run a command npm start

This sounds a lot like a way to get our app working with all dependencies…In fact these sound like instructions.

Creating the docker image from the docker file

Alright, from the Dockerfile, a docker image gets created! The dockerfile acts as a set of instructions to make our docker image. The docker image has everything we need to run the application, all the files, all the dependencies, configuration.

So let’s build our docker image, and let’s give it a name: “node-backend”, and it will use the docker file to do so.

docker build -t node-backend .

Alright cool cool cool, we have the image now, but it’s not running yet. For that we need a container.

Running the docker image in a container

This docker image will run on a container. A container is an independent running space for your application or image. It runs on your machine and has been isolated from all other processes on the host machine. When running a container, it uses an isolated filesystem. This custom filesystem is provided by a container image.

Docker runs processes in isolated containers. A container is a process which runs on a host. The host may be local or remote. When an operator executes docker run, the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.

To run the docker image on a container, let’s run the following command:

docker run \
-it --rm -d \
--name node-backend \
-p 9000:8080 \
-e PORT=8080 \
-e API_KEY=<INSERT_API_KEY> \
-e DATABASE_URL=<INSERT_DATABASE_URL>
node-backend

You’ll notice a few flags being used.

  • -d - run the container in detached mode (in the background)
  • -p 900:8080 - map port 8080 of the host to port 9000 in the container
  • node-backend - the image to use
  • -e environment variable
  • --rm, the container is removed when it exits or when the daemon exits (for clean up)

Make sure you have docker running, and you will see that your container will start spinning up! :)

Visiting our Real Life Scenario again

Back to our scenario, if the friend has docker installed, all she needs to do is run the docker commands, the app will work on her machine in an isolated container, without installing anything else on her laptop.

Of course, docker gets way more complicated than this, and that’s why I will end this article now :-)

--

--

Jumanah Al Asadi

I am just a web developer who loves writing, teaching and music :) My teaching style is playful and fun. At least I hope, lol :p