Dockerize your Nodejs Application
The goal
The goal of this article is to demonstrate an example of?dockerizing a nodejs application?and using?docker volumes?to store the application data. I’m going to use a very simple example, but you can apply the same steps to dockerize any nodejs application.
For this purpose, I will use a simple, one-page?Blog Website ?built with nodejs. If you have your application that you want to dockerize then use that, otherwise clone the same repository that I’m using, and let’s get started.
Repository Link:?https://github.com/hayk-simonyan/dockerized-blog-website
Let’s Dockerize
1. Create a Dockerfile
First, you need to?cd {into_your_project}, in my case?cd dockerized-blog-website
Now, you need to create a?Dockerfile, which is a?file where you define how your docker image should be configured and built.?Later, you will build your image based on this file and docker will execute all the commands that are listed here from top to bottom.
FROM node WORKDIR /blogapp COPY package.json . RUN npm install COPY . . EXPOSE 8080 CMD [“node”, “server.js”]
Let’s see what this file contains. We build our image on top of a node image from?DockerHub . Then we set the main working directory to be the/blogapp?folder (optional). We copy?package.json?into the?/blogapp?repository and run?npm install?to install the?node_modules.
After that, we copy the rest into the?/blogapp. Then we expose port?8080, because our?server.js?listens on port?8080?-?app.listen(8080), if your application runs on a different port you should expose that port instead of?8080. Lastly, we start our server by running?node server.js
Make sure that you created the Dockerfile at the root level of the project.
2. Build image using the Dockerfile
Now that we have our Dockerfile, we can create an image. In your project repository run
docker build . -t blogapp:1.0
docker build .?builds a new image using your Dockerfile. Use?-t option?to give your image a name (blogapp) and version (1.0)
Check if it’s created successfully, run docker images and find your image in the list
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE
blogapp | 1.0 | a6778e6bb622 | 5 seconds ago | 914MB
3. Create a container
In my application, I store the posts as text files in the?/post?folder. So docker will do the same and will save posts in the/post?folder inside of the container. But later, in case if the container is removed, these posts in the?/post?folder will be removed along with the container.
So we need some way to have a backup of this?/post?folder to make sure we don’t lose the user data. For this purpose, we can use?docker volumes?to safely backup the important data.
Volumes ?are folders on your host machine which are “mounted” (made available) in the container. Volumes persist, even if a container is removed. Containers can read and write data into a volume.
So I will create my container based on the image that I just built. And I will also create a volume in the same command to store all the files from the?/post?folder to be safe when the container is removed.
docker run -p 3000:8080 —-name blogapp -v post:/blogapp/post a6778e6bb622
领英推荐
Run?docker ps?to double-check if the container is running
CONTAINER ID | IMAGE | ... | NAMES
ab31a3adb143 | a6778e6bb622 | ... | blogapp
4. Open the app and store data in the volume
Now your container is running on port?3000, if you visit?https://localhost:3000 ?you should see your app running.
Try creating a new post, for instance, with the title?firstpost?and any content. Then visit?https://localhost:3000/post/firstpost.txt . You should see your post here
To see if the volume is created run?docker volume ls
DRIVER | VOLUME NAME
local | post
5. Stop, remove, restart the container and make sure that the data was stored in the volume
Stop the container using the container namedocker stop blogapp
Remove the container?docker rm blogapp
Now, restart your container with the same command we used above
docker run -p 3000:8080 — name blogapp -v post:/blogapp/post a6778e6bb622
And visit?https://localhost:3000/post/firstpost.txt . You should still see the post that you created, which means that it was stored in a docker volume and survived after container removal.
In Summary
This was an example of dockerizing a nodejs application using volumes. As we saw containers can read/write data, but written data is lost if the container is removed. Therefore, we used docker volumes, which are folders on the host machine, managed by docker, and are mounted into the container.
In the example, we used a named volume. There are 3 types of volumes that can be used — named volumes, anonymous volumes, and bind mounts. I’ll leave a link in the resources below in case if you want to learn more about them.