Hello Docker! Creating a super simple local Docker Image
In this article I will show you how to create and launch a Docker Image on your local machine in under 10 minutes. I will explain the Docker File in depth but assume you have experience using node and js.
Why Docker
Before we jump into coding, I want to address why Docker is such a cool and powerful tool to use. My answer to that question (although there are plenty of others) is Containers! I cannot tell you how many times I have created some script or app which runs fine on my local environment, only to then have it break in a hosted setting or when someone else with a different node version, python env etc… tries to run it. With containers you can be sure your code and dependencies are isolated to your container runtime, also you can run containers pretty much anywhere and they can be as fast and lightweight as you want since you get to pick what base image to start from (or create your own if you’re brave).
In short, Docker containers are a great way to isolate, test and deploy your code in a fast and scalable manner, also with the adoption of Kubernetes for managing containers, they are quickly becoming a must know for any new developer.
Walkthrough
Now lets walkthrough creating a small node server in a Docker container.
Install Docker Desktop
The first step is to install Docker Desktop it comes with a nice GUI app to see all your images and running apps as well as a CLI to run Docker commands.
Create our express app
We are going to create a very simple express app which just has one route saying “Hello Docker”.
package.json
{
"name": "dockermedium",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "4.18.2"
},
"author": "",
"license": "ISC"
}
The package.json is very basic, all that I added is a dependency on express.
server.js
import express from "express";
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
app.get('/', (req, res) => {
res.send("Hello Docker");
})
app.listen(PORT, HOST, () => {
console.log(`Running on http://${HOST}:${PORT}`);
});
Our app itself is just as simple we just create a route at ‘/’ which returns a message to the user and host the app at localHost port 8080. The most important thing from this file is to remember the port number as we will use that in our Dockerfile next
Dockerizing our app
Now is the fun part, we are going to create a Docker image and then run our app using Docker.
Dockerfile
# the base image to start with
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Get the package.json
COPY package.json ./
# install dependencies in package.json
RUN npm install
# Copy app files
COPY . .
# expose the port our node app will be hosted on
EXPOSE 8080
# start the server
CMD [ "node", "server.js" ]
We start off by pulling the official node v16 image from docker, this is the base image we will then build our app on.
We then create a directory to work in /usr/src/app.
Next, we will copy our package.json file from our local to this WORKDIR
We then run npm install to get our dependencies in the location we will run our image
We then copy all our app files from our local directory to the WORKDIR
Next we expose that port 8080 (which is where our node app is running) so that we can access it after our app spins up from outside of docker.
Finally, we run the command node server.js
Docker Run
Now we are ready to build and run our image
docker build . -t medium/node-web-app
We build our image according to our Dockerfile and tag it using the -t flag with medium/node-web-app so we can find it later
You should be able to find your image by running “docker images”
docker run -d -p 8001:8080 medium/node-web-app
Now we run the image in a container, we use -d to detach the process so we can still use our console and we use -p to say “map port 8080 on the docker container to port 8001 on my machine’s port” so we can then see our app by checking out localhost:8001.
When you are finished, use “docker ps” to get the container id of your running app and “docker kill <container id>” to end it.
Now have fun containerizing! Be sure to check out the Docker Docs as a resource on your Docker journey.