Build and Run Docker Image - Sample NodeJS App
In the last tutorials, we saw how we can use a docker image available on the Docker hub and run it. We also know what Docker Hub is, so in this tutorial, we will create a docker image for a sample Nodejs application, and create a new container running the docker image.
Here is the GitHub repository, where the application code is available: docker-sample-application
This tutorial expects that you have Docker Desktop installed, if not please follow the following tutorials:
The application that we will run in this tutorial is a NodeJS application, but don't worry you need no experience in NodeJS and need no setup as well.
Scope of this Tutorial
This is what we will do in this tutorial, and you have to do everything alongside.
-
Download code for the sample NodeJS application. You can make a pull request as well because this is a git repository.
-
Then we will create a docker image for this application
-
Once we have the docker image, we will run the docker container.
-
And, finally, test the application.
So let's get started.
Step 1: Download the Sample Application
Download the sample application from the Github repository. Here is the URL for the GitHub repository: docker-sample-application
Either use the git commands to take a pull of this repository to your local machine, or just download the zip file of the sample application code.
Step 2: Create Docker Image (docker build
)
Now it's time to create a docker image. In the sample application that you have downloaded, you will find the app/ directory, inside it the complete code is available, but don't worry about the code right now.
Create a file named DockerFile with the following contents inside the app/ directory where the package.json file is also kept,
# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
Please make sure there is no extension of the Dockerfile, sometimes test editors add a default extension.
There is no extension of the docker file.
Next, open the terminal and go to the app/ directory inside the sample application directory, and run the below docker build
command to build the docker image.
docker build -t first-sample-app .
In the docker build
command above, the -t
flag tags our image. By tagging, we mean giving a name to the docker image, in this case, the name is first-sample-app, and the .
at the end of the docker build
command tells that Docker should look for the Dockerfile in the current directory.
When you build a new docker image, it can take a minute or two because it will download and collect all the required layers to build the docker image.
Like in this case, we have the following lines in the Dockerfile,
node:12-alpine image, which is an image with Alpine Linux distribution(very compact Linux), with NodeJS version 12 installed in it. So this will be the base operating system of our Docker container when this image is run, and there will be NodeJS preinstalled in it.
After the image was downloaded, we specified in the Dockerfile to use yarn
to install our app's dependencies. The CMD
directive is used to specify the default command to run when starting a container from this image.
Step 3: Start Docker container (docker run
)
To start the docker container using the generated docker image, we will use the docker run
command,
docker run -dp 3000:3000 first-sample-app
In the docker run
command above, notice the two flags used -d
and -p
, what are these for?
Well, -d
, instructs Docker to run the new docker container in detached mode.
Whereas, the -p
flag is used to map the port of the docker container to the host machine. So we run a docker container and map its 3000 port to your machine's 3000 port so that when you open the URL http://localhost:3000 in your browser, it will automatically connect with the 3000 port of our new docker container.
Once you have run the above command, then if you will open your browser with the following URL http://localhost:3000 then you should see the NodeJs application load with the following User interface:
You can try using the application, and add a few items to the TODO list.
Step 4: Verify the Container
Now open the Docker Desktop application, and in the Container section, you will see a new docker container is listed. If you followed the Docker Hello World tutorial, then you will see two containers running (One for the hello-world docker image).
And if you want to see the new docker image that we created, then click on the Images option, and you will find your new docker image listed.
Conclusion
That's it for this tutorial. We learned building a new docker image and how to run the docker container using the docker image. In the next tutorial, we will learn how to update the docker image and stop/restart the docker container. So let's move on.