DEV Community

Cover image for How to host NodeJs server into the docker container
Developer Sabbir
Developer Sabbir

Posted on • Edited on

How to host NodeJs server into the docker container

In this tutorial, I'm going to show you how you can create your Nodejs server to your docker container.
Ever wondered how streamlined your workflow can be when you host your NodeJS server in a Docker container? 💭💼

Stay tuned, as I’m thrilled to share insights around it. Master the art of simplifying complex systems with easy implementation! 💡

Follow these step to create and run your docker container

Creating Dockerfile & Configure

  • Create a file called Dockerfile in your root direcotry
  touch Dockerfile
Enter fullscreen mode Exit fullscreen mode
  FROM node:slim

  WORKDIR /shopify_server

  COPY . ./package*.json ./

  RUN npm install

  EXPOSE 5000

  CMD ["npm", "run", "start"]
Enter fullscreen mode Exit fullscreen mode
  • Run a docker build command
  docker build -t shopify-docker-image .
Enter fullscreen mode Exit fullscreen mode
  1. docker build This part of the command tells Docker to build a Docker image. Docker images are like templates or blueprints for creating containers.
  2. -t shopify-docker-image: The -t flag is used to specify a name and optionally a tag for the image you're building. In this case, you're giving the image the name "shopify-docker-image." The name/tag combination is typically used to identify and manage different versions of an image.
  3. .: The period (dot) at the end of the command specifies the build context. The build context is the set of files and directories that Docker will use to build the image. In this case, Docker will look for a Dockerfile in the current directory (the directory where you're running the docker build command) and use it as the instructions for building the image.
    • Create container and start
  docker run --rm -d -p 5000:5000 --name shopify-docker-container shopify-docker-image
Enter fullscreen mode Exit fullscreen mode
  1. docker run: This part of the command tells Docker to run a container.
  2. --rm: This flag tells Docker to automatically remove the container when it exits. This is useful for cleaning up containers after they've finished running to avoid cluttering your system with unused containers.
  3. -d: This flag tells Docker to run the container in detached mode, which means the container runs in the background, and you get your terminal prompt back immediately.
  4. -p 5000:5000: This flag is used to publish ports from the container to the host system. In this case, it's mapping port 5000 from the container to port 5000 on the host. This allows you to access any services running inside the container on port 5000 from your host machine.
  5. --name shopify-docker-container: This flag assigns a name to the running container. In this case, the container will be named "shopify-docker-container." You can use this name to reference the container in other Docker commands.
  6. shopify-docker-image: This is the name of the Docker image that you want to use for creating the container. In your previous command, you built an image with the name "shopify-docker-image," and now you are running a container based on that image.
  • docker ps
    • Container ID: A unique identifier for the container.
    • Image: The Docker image from which the container was created.
    • Command: The command that is running inside the container.
    • Created: The timestamp indicates when the container was created.
    • Status: The current status of the container (e.g., Up, Exited, etc.).
    • Ports: Information about port mappings between the container and the host.
    • Names: The name(s) assigned to the container (if any).

Top comments (0)