In this post, we are going to see how to deploy a NodeJS blog application to Docker and how to manage docker containers and publish docker images to Docker Hub.

NodeJS is a JavaScript runtime environment, widely used and popular for its asynchronous request handling. Docker is a container management system, used to create and manage containers efficiently.

After reading this post, you will learn:

  • xxxxx
  • xxxxxxxxxx
  • xxxxxxx

Docker


1. Install Docker

  • Step 1. Download and install Docker Desktop, then execute it. Create a Docker Hub account if you don’t have one, then login to Docker Hub. You will see:

  • Step 2. Start Powershell of windows 10, type docker --version to verify that you have well installed Docker Desktop. Then type docker container ls -a to see if there is any container, type docker info to check all the information of docker.

2. Pull Image

  • Step 3. Visit Node Docker Official Image, and understand the features of variant node docker images, then choose one image according to your needs. In our case, we will choose alpine image to save disk space. Create a directory named DockerRep, then go to that path and type docker pull node:current-alpine3.13.
mkdir DockerRep
cd DockerRep
docker pull node:current-alpine3.13
  • check the downloaded image in docker desktop.

3. Start Container

  • Step 4. Check the docker images and start the Container from the Image we have pulled. Then verify the container status in Docker Desktop.
docker image ls -a
docker container run -it -p 8081:8080 -d --name node-blog-app node:current-alpine3.13
docker container ls -a

Here:

  • -it: sets the container in Interactive mode and allocate a Dedicated TTY id.
  • -p 8081:8080: Port forwarding between Host and the Container.
  • -d: sets the container to run in the background.
  • node-blog-app: name of the Container we are starting.
  • node:current-alpine3.13: name of the Image from which we are going to create a Container

4. Execute in Container

  • Step 5. Docker Container is not a Virtual Machine but a Process created from the Image. We can use Interactive Terminal to get into the Container, the flags -it enables this feature. Now we can easily Login to our Container and do all the changes or updates. Once you are into the Container, you can manage it just like you do manage your Linux Virtual Machine, the command supported would vary based on the base OS you have selected while creating the Image. In our case it is alpine.

  • This will create a new Shell session in the container node-blog-app

docker container exec -it node-blog-app "/bin/sh"

5. Docker Commands

docker version   – Echoes Client’s and Server’s Version of Docker
docker images   – List all Docker images
docker build <image>   – Builds an image form a Docker file
docker save <path> <image>   – Saves Docker image to .tar file specified by path
docker run   – Runs a command in a new container.
docker start   – Starts one or more stopped containers
docker stop <container_id>   – Stops container
docker rmi <image>   – Removes Docker image
docker rm <container_id>   – Removes Container
docker pull   – Pulls an image or a repository from a registry
docker push   – Pushes an image or a repository to a registry
docker export   – Exports a container’s filesystem as a tar archive
docker exec   – Runs a command in a run-time container
docker ps   – Show running containers
docker ps -a   – Show all containers
docker ps -l   – Show latest created container
docker search   – Searches the Docker Hub for images
docker attach   – Attaches to a running container
docker commit   – Creates a new image from a container’s changes
  • Useful Commands
# copy files from Host to Container or from Container to Host
docker cp src/. my-container:/target
docker cp my-container:/src/. target

NodeJS - (Blog-App)


1. Docker As Dev Environment

As we all know, Docker is a system designed to create, deploy and run applications by using containers. Containers allow us to package all libraries, dependencies as well as the source code of an application into an image, others can get exactly what we have in the container by clone and execute the image.

According to Docker features, we can use it as local or remote development environment. When we handle more than one projects, all of them use different dependencies or versions of libraries, it’s very hard to manage different working environments in your machine, so that’s why we want to use docker container to host our Blog-App.

In this part, we will store the source code and the needed data in the disk of local machine (Host), and share this disk with the Container. Then we can create or modify the source code, add or delete the needed data in the Host, and run the application in the Container. Because the disk of the Host is mounted to the Container, our source code and data can be synchronized automatically. After running the application, we can see results in the Browser of the Host in the Port 8081 because we bind the 8081 Port of the Host to Virtual 8080 Port of the Container before.

The figures below show the structure of how to use Docker as local (or remote) development environments.

2. Mount Directory to share data between Host and Container

In windows, we can’t use docker volume create --driver local to create Docker Volume, if you use linux, please reference this. So, we mount the Directory of the Host directly to the Container.

  • Step 6. Stop and remove the node-blog-app container to avoid conflicts.
# stop and remove container
docker container stop node-blog-app
docker container rm node-blog-app
# or: remove all stopped containers
docker container prune
# verify 
docker container ls -a
  • Step 7. Create the node-blog-app container from node:current-alpine3.13 image again and mount source code or data directory of the Host to the node-blog-app container.
# in DockerRep directory, create a blog-app folder, and put a README.md file in it
# the blog-app folder in Host, is the repository of our source code, you can also manage it with git

# create container
docker container run -it -p 8081:8080 -v "$(pwd)/blog-app:/mnt/data,readonly" -d --name node-blog-app node:current-alpine3.13

docker ps

# check the source code folder
ls blog-app

Here:

  • -v /host-directory:/container-directory,readonly: represents to mount host directory into container directory, readonly is optional, means the container can only read the files in shared directory. This will secure our source code and save computing resources when we modify the source code frequently in host machine.

  • Step 8. Develop Blog-App in the host as usual.

blog-app tree

  • Step 9. In node-blog-app container, copy the source code from shared readonly directory to your workspace directory. We use /home/node/blog-app as workspace directory. Install blog-app and start node server.
# in container
mkdir /home/node/blog-app
cd /home/node/blog-app
cp -rf /mnt/data*/*  .
ls

Other Elements — abbr, sub, sup, kbd, mark

GIF is a bitmap image format.

H2O

Xn + Yn = Zn

Press CTRL+ALT+Delete to end the session.

hello Most salamanders are nocturnal, and hunt for insects, worms, and other small creatures.


Math Rendering

Block math: $$ \varphi = 1+\frac{1} {1+\frac{1} {1+\frac{1} {1+\cdots} } } $$


Emoji Rendering

🙈 🙈 🙉 🙉 🙊 🙊