DEV Community

Pranav Bakare
Pranav Bakare

Posted on • Edited on

Docker Initial configuration Node.js Application

The image you provided outlines a general workflow for using Docker to develop, test, and deploy an application. Here's an explanation of each step with an example:

  1. Develop
    • Write your application code: This is the initial step where you write the code for your application. For example, let's say you're building a simple Node.js web application.
   // app.js
   const express = require('express');
   const app = express();

   app.get('/', (req, res) => {
     res.send('Hello, Docker!');
   });

   app.listen(3000, () => {
     console.log('Server is running on port 3000');
   });
Enter fullscreen mode Exit fullscreen mode
  1. Dockerfile
    • Create a Dockerfile that defines the environment and dependencies for your application: A Dockerfile is a text document that contains the commands to assemble an image. Here’s an example Dockerfile for the above Node.js application.
   # Use an official Node.js runtime as a parent image
   FROM node:14

   # Set the working directory in the container
   WORKDIR /usr/src/app

   # Copy the package.json and package-lock.json files
   COPY package*.json ./

   # Install the dependencies
   RUN npm install

   # Copy the rest of the application code
   COPY . .

   # Expose the port the app runs on
   EXPOSE 3000

   # Command to run the application
   CMD ["node", "app.js"]
Enter fullscreen mode Exit fullscreen mode
  1. Build Image
    • Use docker build to create a Docker image from your Dockerfile: This command creates an image from your Dockerfile.
   docker build -t my-node-app .
Enter fullscreen mode Exit fullscreen mode

This command builds an image named my-node-app.

  1. Run Container
    • Use docker run to launch a container from your image: This command creates a container from your image and runs it.
   docker run -p 3000:3000 my-node-app
Enter fullscreen mode Exit fullscreen mode

This command runs the container and maps port 3000 of the container to port 3000 of the host machine. Now, if you navigate to http://localhost:3000 in your browser, you should see "Hello, Docker!"

  1. Test
    • Test your application within the container: Ensure your application works as expected within the container. If you make changes to your code, rebuild the image and recreate the container.
   docker build -t my-node-app .
   docker run -p 3000:3000 my-node-app
Enter fullscreen mode Exit fullscreen mode
  1. Push (Optional)
    • Use docker push to share your image on a registry (e.g., Docker Hub): If you want others to use your Docker image, you can push it to a registry like Docker Hub.
   docker tag my-node-app mydockerhubusername/my-node-app
   docker push mydockerhubusername/my-node-app
Enter fullscreen mode Exit fullscreen mode
  1. Pull (Optional)
    • Others can use docker pull to download your image and run your application in their own environments: If someone else wants to use your application, they can pull your image from the Docker registry and run it on their own machine.
   docker pull mydockerhubusername/my-node-app
   docker run -p 3000:3000 mydockerhubusername/my-node-app
Enter fullscreen mode Exit fullscreen mode

This process ensures that your application is containerized, making it easy to deploy and run consistently across different environments.

Top comments (0)