Create simple web app Nodejs with Docker
How to get a Node.js application into a Docker container?
Steps to create a web application Nodejs with Docker
- Create NodeJS web app
- Create a Dockerfile
- Build image from Dockerfile
- Run image as container
- Connect to web app from a browser
Getting started
Create root folder of project
Init package.json
Add dependencies and scripts in this package.json file
Create index.js
file (in root project)
Add the following code to run server nodejs with express
framework.
;
;
'/',;
;
port,;
Create Dockerfile
-
The first thing we need to do is define from what image we want to build from. Here is the version 12.18.1 of Nodejs from DockerHub
FROM node:12.18.1
-
Next we create a directory to hold the application code inside the image. This will be the working directory for your application.
# Create app directory WORKDIR /app
-
This image comes with Node.js and NPM already installed so the next thing we need to do is to install your app dependencies using the npm binary. Please note that if you are using npm version 4 or earlier a package-lock.json file will not be generated.
# Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production
-
To bundle your app’s source code inside the Docker image, use the COPY instruction:
# Bundle app source COPY . .
-
Your app binds to port
8080
so you’ll use theEXPOSE
instruction to have it mapped by the docker daemon:EXPOSE 8080
-
Last but not least, define the command to run your app using CMD which defines your runtime. Here we will use
node index.js
to start your server as write in scripts ofpackage.json
file.CMD ["yarn", "dev"]
-
Your
Dockerfile
should now look like this:# Specify a base image FROM node:12.18.1 # Create app directory WORKDIR /app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ # Install dependencies RUN yarn install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 8080 # Default command CMD [ "yarn", "dev" ]
Create .dockerignore
file with the following content:
node_modules
npm-debug.log
Build your image
Go to the directory that has your Dockerfile
and run the following command to build the Docker image. The -t
flag lets you tag your image so it’s easier to find later using the docker images
command:
Your image will now be listed by Docker:
# Example
<your
Run the image
Running your image with -d
runs the container in detached mode, leaving the container running in the background. The -p
flag redirects a public port to a private port inside the container. Run the image you previously built:
Print the output of your app:
# Get container ID
# Print app output
# Example
If you need to go inside the container you can use the exec command:
# Enter the container
Test your Docker container
To test your app, get the port of your app that Docker mapped:
# Example
In the example above, Docker mapped the 8080 port inside of the container to the port 49160 on your machine.
Now you can call your app using curl (install if needed via: sudo apt-get install curl):
; charset=utf-8
Some useful other commands