Let’s Dockerize Your App!

Navindu Jayatilake
3 min readApr 25, 2020

--

Literally no one:

You before Docker: “Well, It works on my machine!!!”

Hello guys, long time no see! I am having a busy time since I started working on my Internship at the end of the last year. So this is going to be a short and a sweet article and I hope you will enjoy it.

In this article, I’m planning to write about how you can dockerize your web application. So in case, you don’t know what Docker is I suggest you go ahead and have a good understanding of Docker concepts.

So here’s the deal, you gonna need….

  1. Node installed
  2. Docker installed on your PC

So to start with, first, we have to initiate a simple node application. To do that simply create an index.js file and hit npm init to initialise a node app. Since we have to set up a simple node webserver and routes, we gonna need express as well.

So our simple node application will finally look like this.

Once done, hit node index and you will see your simple node application running smoothly on port 3000.

Now comes the Docker part. So we are going to create this special file called Dockerfile. So Dockerfile is simply a file which contains all the instructions to build your docker image. You can automate the build process of your application using this file.

Let’s dive deeper into this Dockerfile!

FROM keyword you can easily specify what image/distribution you are going to use. As it’s shown in this example, I have used Node since this is going to be a pure node application.

WORKDIR keyword shows the path to your working directory.

COPY (1) keyword indicates that package.json is copied to your Dockerfile

RUN install all the dependencies you have on your package.json file

COPY (2) adds and bundles your project files to the Docker image.

CMD once all the dependencies are installed this will run your node app on the port specified.

Note: You have to have a .dockerignore file in the same directory level you have your Dockerfile. This will prevent your local node modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.

Great now the hard work is done. Let’s build our Docker Image! 🐋

To build your Docker Image run,

docker build -t mydockerimage:1.0 .
docker build -t imageName:tagName <location_of_dockerfile>

Now if you run docker images command, you will see your newly created Docker image. Okay, let's run our Docker image now. To run simply hit,

docker run <your_image_id>

All right guys here is the great part of this article!

What if one of your friends want to get your application and install it in his machine? That’s where Docker Hub comes in!

Docker Hub is a service provided by Docker for finding and sharing container images with your team.

Now let‘s upload our image to Docker Hub. First, we have to create an account in Docker Hub and tag our Docker image.

docker tag <image_id>  docker.io/<docker_hub_username>/<your_repo>docker push docker.io/<docker_hub_username>/<your_repo>

Now you should be able to see your repository in Docker Hub! ❤

You can use the docker pull command to pull the image and simply find its ID and use the docker run command to run it!

Wrapping Up!

Alright guys, that’s it for this article! I hope you enjoyed it as much as I did.

See you guys in the next one! ❤

--

--

Navindu Jayatilake

Software Engineering Graduate | DevOps Lead @ Insighture | Ex-Sysco LABSter | Ex-Pearsonist