Getting Started with Docker and Node.js
Table of Contents
- Introduction
- Requirements
- Create Node.js App
- Create Dockerfile
- Build and Run Docker
- Test
- Useful Docker Commands
- Conclusion
Introduction
This blog demonstrates a quick start guide for trying Docker with Node.js.
Requirements
Before we get started, you'll need to download Docker and Node.js so that we can run docker, npm, or node from the local environment.
Create Node.js App
Here, we'll create a very simple Node.js web app. First, let's run the commands below.
npm init -y
npm install --save express
nano server.js
These commands will first initialize an npm repo in the local directory, install express as the web server, and create a new file called server.js.
Inside server.js, we'll put the following content.
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('hello world!')
})
app.listen(4455, () => {
console.log('server is up on 4455')
});
This code will start a web server on port 4455 and return the message hello world!.
If you type http://localhost:4455 in your browser, you should see the message hello world!.
Create Dockerfile
Docker can build images, and later you can load and run those images. The Dockerfile contains the instructions for how to build the image.
Create a file called Dockerfile and add the following content. This will use the Alpine Docker image, copy all the current directory content, run npm install, and expose port 4455.
FROM node:8-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 4455
CMD ["node", "server.js"]
Build and Run Docker
The Node.js code is ready and the Dockerfile is ready too. Now we can use the Dockerfile to build an image and run it in our local environment.
Run the command below in the same directory as the Node.js app. This will create a Docker image.
docker build -t my-first-docker-app .
If you run this command, you should be able to see the image you just created.
docker images
Now, run this command to execute the Docker image in a new container with port 4455. The -p flag is for --publish, which publishes the container's port to the host.
docker run -p 4455:4455 {the image id}
Test
Run this command on your local environment. We should see hello world! from the Node.js app.
curl http://localhost:4455
Useful Docker Commands
Here are a few other commands that might be useful when working with Docker.
List All Docker Images
docker images
List All Containers
docker container ls
Stop a Container
If you have a website running in a Docker container, once you stop the container you won't be able to access it.
docker stop {container ID}
Find All Containers Including Stopped Ones
docker container ls --all
Delete a Container
docker container rm {container id}
Delete All Containers
docker rm $(docker ps -aq)
Delete All Images
docker rmi $(docker images -q)
Conclusion
The above is a simple set of steps to create a Node.js app, including creating a Docker image based on the Node.js image, running the Docker image in a container, and accessing it from outside the environment with the port opened. Hopefully you found this blog helpful, and feel free to reach out if you want to see more content like this.