DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Deploy Express API with Docker
MC Naveen
MC Naveen

Posted on • Originally published at Medium

Deploy Express API with Docker

Docker is a cross-platform virtualization technology that provides OS-level virtualization to run applications on the cloud. It enables developers to ship their apps as containers and avoid system conflicts.

Deploy Express API with Docker

Docker containers can be published via DockerHub or can be distributed locally using zip files. As of writing this article, DockerHub hosts more than one million public docker containers and images.

Setting up our Express API

Our Express API is pretty simple and contains only one GET route which says hello message.

This is what my index.js file looks like.

index.js file

Now, create a new file called Dockerfile and paste the below code inside it.

FROM node:16

# Create app directory
WORKDIR /usr/src/app

# Copy package.json
COPY package*.json ./

# Install dependencies
RUN yarn install

# Copy all files
COPY . .

# Expose port 3000
EXPOSE 3000

# Run app
CMD [ "node", "index.js" ]
Enter fullscreen mode Exit fullscreen mode

Breakdown of the Above file contents.

  • FROM Node:16 will pull the Node v16 image from the DockerHub
  • WORKDIR /usr/src/app β€” Here is where our app lives inside the container
  • COPY package*.json ./ β€” Copy the package.json from local dir to docker image.
  • RUN yarn install β€” This will install all the dependencies.
  • COPY . . β€” This will copy all the project files into the Docker image.
  • EXPOSE 3000 β€” This will allocate a docker container to use port 3000
  • CMD [ β€œnode”, β€œindex.js”] β€” This will start the Node JS app.

The Dockerignore file:

Now create a .dockerignore file and add the below content inside it.

node_modules
yarn*.log
Enter fullscreen mode Exit fullscreen mode

Similar to .gitignore this will ignore the below-mentioned files when building the docker image.

Building the Docker Image:

Once the above process is done, We have to build our project as a docker image to use it.

Docker image can be built using a single line of command.

docker build . -t="timeless-hello-world"
Enter fullscreen mode Exit fullscreen mode

Running the Docker Image:

docker run -d -p 3000:3000 --name my-app timeless-hello-world
Enter fullscreen mode Exit fullscreen mode

Breakdown of the above command:

  • -d will detach the terminal after the successful run. Otherwise, the server will stop if we close the terminal.
  • -p 3000:3000 is the port we want our application to run.
  • --name my-app is the name of the container.
  • Finally, timeless-hello-world is the docker image we built in the above step.
  • Once you press the enter key. You’ll be prompted with the container ID.

We can also verify whether the container is running using the docker ps command.

If you open http://localhost:3000 in the browser, You’ll get an API response like this.

API Response

That’s it for this article. Hope this will be helpful to deploy your Express API using docker.

Feel free to comment your thoughts and let me know if you face any difficulty.

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.