Skip to content

iambotcoder/My-First-Docker-Application

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 

Repository files navigation

πŸš€ My First Docker Application


πŸ“– Overview

This project is a simple demonstration of creating and running a Dockerized Node.js application. The steps include creating a Dockerfile and docker-compose.yml, building a Docker image, and running the application using Docker Compose. This application responds with a JSON message "Docker is easy 🐳" when accessed via HTTP.


πŸ“‘ Table of Contents


πŸ”‘ Prerequisites

Before you start, ensure you have the following:

  • An AWS account 🌍
  • Node.js and npm
  • Docker and Docker Compose installed for building and pushing Docker images
  • Basic understanding of JavaScript and Docker πŸ§‘β€πŸ’»

πŸ—ΊοΈ Architecture

The application consists of two main components:

  • Node.js Application: A simple Express server serving an API.
  • MySQL Database: A containerized MySQL instance managed using Docker Compose.

Workflow:

  • The Node.js application runs inside a Docker container.
  • The application connects to a MySQL database running in another container.
  • Both containers are managed using Docker Compose.

πŸ› οΈ Setup & Installation

1⃣ Setup Node Project

  1. Initialize a Node.js project:
    npm init -y
  2. Install dependencies and start the project:
    npm install
    npm start
  3. Create an index.js file with the following content:
    const app = require('express')();
    
    app.get('/', (req, res) =>
        res.json({ message: 'Docker is easy 🐳' })
    );
    
    const port = process.env.PORT || 5000;
    
    app.listen(port, () => console.log(`app listening on http://localhost:${port}`));

🐳 Docker Setup

2⃣ Create a Dockerfile

Create a file named Dockerfile in your project directory:

FROM node:12

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

ENV PORT=5000

EXPOSE 5000

CMD [ "npm", "start" ]

3⃣ Build and Run the Docker Image

  1. Build the Docker image:
    docker build -t iambot/myfirstnodeapp:1.0 .
  2. List Docker images:
    docker image ls
  3. Run the Docker container:
    docker run -p 5000:8080 <imageid>
  4. Check running containers:
    docker ps -a

The application will now be accessible at http://localhost:5000.

🌐 Docker Compose Setup

4⃣ Create a docker-compose.yml

Create a file named docker-compose.yml with the following content:

version: '3'
services:
  web:
    build: .
    ports:
      - "3000:5000"
  db:
    image: "mysql"
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - db-data:/foo

volumes:
  db-data:

5⃣ Run the Application with Docker Compose

  1. Start the services:
    docker compose up
  2. Stop and remove the services:
    docker compose down

πŸ›‘οΈ Accessing the Application

  • Access the Node.js application at: http://localhost:3000.
  • The application responds with:
    { "message": "Docker is easy 🐳" }

🚹 Cleaning Up Resources

To clean up all resources:

  1. Stop and remove containers:
    docker compose down
  2. Remove unused Docker images:
    docker image prune

βœ… Conclusion

This project demonstrates how to containerize a simple Node.js application and manage it using Docker and Docker Compose. The setup ensures an isolated and portable development environment for your application.


πŸ‘¨β€πŸ« Instructor

This project was guided by Navin reddy and Telusko, who provided valuable mentorship throughout the process.

About

This project is a simple demonstration of creating and running a Dockerized Node.js application

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published