Eren Akbulut's Blog

Docker Quick Start

December 25th, 2020

What is Docker

Docker is a tool that allows developers to create, deploy, and run their applications by using the power of containers. Containers provide developers all the packages and dependencies to make their application run on a sandboxed environment. Unlike a VM a container doesn't contain its own kernel, it uses the host OS's resources.

Further information about the can be found here

Quick Start with a Dockerfile

Even though it's a little bit top to bottom I'll explain docker cli commands that I found useful later.


Dependencies

I'm using the Docker Desktop application on MacOS but all the files and commands are independent of your way of getting Docker. You can find how can you start here

Another dependency we need is the Node on our machine.


Base Files

We should create a folder and navigate into it. After the following commands need to be executed one by one.

  • npm init -y

  • npm install express

Later we should add that line to "package.json" to go along with this tutorial

  • "scripts": { "start": "node src/index.js" }

We also need to create an index.js file in the root directory with the following content:

const express = require("express"); const app = express();

app.get("/", (req, res) => { res.json({ message: "Dockerized basic NodeApp" });});

const port = process.env.PORT || 8000; app.listen(port, () => { console.log(`The app is up at PORT ${port}`);});


Dockerfile

In the root directory, we should create a "Dockerfile". The "Dockerfile" is a special file that our "docker run" commands will take a look into. We also need to create a file called ".dockerignore" which is familiar to the ".gitignore" file. Add the line "node_modules" into ".dockerignore" file.

Docker file is the most critical file for our application. And the content should look like that.

Dockerfilebasic
  • FROM command indicates what our base image is, if we don't have the specified node version in our computer "docker run" command will fire a "docker pull" command under the hood and pull it from the Docker Hub. If we have it the layer will be just pointed to the actual node image.

  • WORKDIR is where we specify our working directory.

  • With COPY command we can copy any file from our system to our container image to be. In this example package*.json from our system and ./ indicates the location that we want our package*.json file in.

  • RUN command runs npm install command.

  • COPY command again copies everything from "."(source) to "."(destination)

  • ENV command lets us define environment variables, in this case we define our port for our webserver.

  • EXPOSE lets us use that port 8080 to interract with program's interface.

  • CMD command executes a command with the given parameters in the string array next to it.


Docker run

After everything is set we can create our docker image by running:

docker build -t <ourdesirednameforourimage> .

After this command is successful we can check our images by running the command docker images and the output should be something like this:

REPOSITORY TAG IMAGE ID CREATED SIZE <ourtag> 1.0 randomimageid 2 hours ago 971MB

We can finally run our container by running the command:

docker run -p 5000:8080 <image id or the tag we set here>

and that's it we can interact with our app from the port we indicated with the -p flag above.

Given our index.js code output should be something like that when we open the page "http://localhost:5000/"

docker-basic-live

I hope you all enjoyed this tutorial as much as I did, see you in the more depth tutorials in the future...

This blog has been created by Eren Akbulut 2020