Deploying a Node.js application with Docker involves several steps. Let’s go through them.
This guide assumes that you have Docker, Node.js and a Node.js app already installed and ready to go.
1. Create a Dockerfile: Docker can build images automatically by reading the instructions from a Dockerfile. It’s a text document that contains all the commands a user could run on the command line to assemble an image. Here is an example Dockerfile for a Node.js app:
```
1. Build the Docker Image: Once the Dockerfile is set up, navigate to the directory that holds your Dockerfile and run the following command to build the Docker image. The dot at the end of this command is docker build context. It is used to specify the directory where Docker will look for files to add to the Docker image.
```
docker build -t [imageName] .
```
Example:
```
docker build -t my-nodejs-app .
```
1. Run the Docker Image: To run the Docker image, you can use the following command:
```
docker run -p 8080:8080 -d [imageName]
```
Example:
```
docker run -p 8080:8080 -d my-nodejs-app
```
The ‘-p’ flag is used to map a port in your container to a port on your machine. The ‘-d’ flag is used to run your Docker image in detached mode, meaning your Docker container will run in the background.
1. Access the Node.js app: Now you can access your node.js app via http://localhost:8080 or using the IP of your docker machine (You can find out by running `docker-machine ip` command).
That’s it! You have successfully deployed a Node.js application with Docker. With this setup, you can easily ship code by building the docker image and ship it to run anywhere Docker is installed.