Day 17 Task: Docker Project for DevOps Engineers

Lokesh TelangeLokesh Telange
2 min read

Dockerfile

Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.

A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts

  1. Create a Dockerfile: Create a file named Dockerfile in your project directory with the following content:

     DockerfileCopy code# Use an official Node.js runtime as a base image
     FROM node:14
    
     # Set the working directory in the container
     WORKDIR /usr/src/app
    
     # Copy package.json and package-lock.json to the working directory
     COPY package*.json ./
    
     # Install app dependencies
     RUN npm install
    
     # Copy the application files to the working directory
     COPY . .
    
     # Expose the port the app runs on
     EXPOSE 3000
    
     # Define the command to run your application
     CMD ["npm", "start"]
    

    Make sure to adjust the above file based on your actual application structure and dependencies.

  2. Build the Docker Image: Open a terminal, navigate to the directory containing the Dockerfile, and run the following command:

     bashCopy codedocker build -t yourusername/yourwebapp:latest .
    

    Replace yourusername and yourwebapp with your Docker Hub username and the name you want for your web application image.

  3. Run the Docker Container: Once the image is built, you can run the container using the following command:

     bashCopy codedocker run -p 3000:3000 -d yourusername/yourwebapp
    

    This command maps port 3000 on your host machine to port 3000 on the Docker container.

  4. Verify the Application: Open a web browser and navigate to http://localhost:3000 to check if your web application is working as expected.

  5. Push the Image to Docker Hub: Before pushing the image to Docker Hub, you need to log in to your Docker Hub account using the following command:

     bashCopy codedocker login
    

    Then, push the image to Docker Hub:

     bashCopy codedocker push yourusername/yourwebapp:latest
    

    Now, your Docker image is available on Docker Hub and can be pulled by others.

10
Subscribe to my newsletter

Read articles from Lokesh Telange directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lokesh Telange
Lokesh Telange