MacOS:- Download and install the dmg file here.
i. Add the GPG key for the official Docker repository to the system
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
ii. Add the Docker repository to APT sources
sudo add-apt-repository “deb [arch=amd64]https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
iii. Update the package database with the Docker packages from the newly added repo
sudo apt-get update
iv. Run apt-cache policy docker-ce to make sure that you are installing from the Docker repository.https://github.com/collins-b/blogpost
v. Finally, install Docker using the command: sudo apt-get install -y docker-ce. And confirm if Docker is running by running the command sudo systemctl status docker
Windows: Download and install using the Windows installer here.
i. Clone this project:
ii. cd blogpost
iii. Run npm install, to install packages, then run npm start. Then visit http://localhost:3000, where you should see something similar to the below image:
We’re going to use the project you cloned above. Now, let’s see how to integrate Docker into your existing project.
i. In the root directory, create a file named Dockerfile, if the name does not exist. This is a text document that contains all the commands that a user could call on the command line to assemble an image.
ii. Populate the file with the following contents:
FROM node ENV NPM_CONFIG_LOGLEVEL warn RUN mkdir -p /usr/src/app EXPOSE 3000 WORKDIR /usr/src/app ADD package.json /usr/src/app/ RUN npm install --production ADD . /usr/src/app/ ENTRYPOINT ["npm", "start"]
The commands are explained below:
The base image for building a new image in our case is the node. This command must be on top of the Dockerfile.
- It defines an environment variable.
- This is used to execute a command during the build process of the docker image.
- Copies a file from the host machine to the new docker image.
- Defines the default command that will be executed when the container is running.
- This is directive for ENTRYPOINT command to be executed.
- Exposes a specified port on which the container will run.
Next, run docker build -t web-app to build a Docker image. Don’t forget to include the period. You can tag your image with any name. Here, I have tagged web-app as shown in the command:
Next, we’re going to create a Docker container by running the created image and by using the command below:
docker run -d -p 3000:3000 web-app
This creates a writeable container layer over the specified image and then starts using the specified command.
I’m using -d flag to run the container in background and print container ID. -p specifies the port in this case and here I’m exposing port 3000.
Visit http://localhost:3000 to access the application on your favorite browser. Yes, now you have successfully dockerized the application!
Now is time to check the contents of the created container. This is to prove all the files are in the container.
List the running containers using the command docker ps.
Then, execute into the container by using the container ID and list the files, as shown below:
You should see all the project’s files, as shown in the above image.
We have successfully dockerized a NodeJs application. The most important configuration is the Dockerfile where we have passed the various commands that are useful in provisioning a Docker image. If you are new to Docker, I will recommend you to visit their online documentation to get a deeper understanding of various Docker components.