Skip to content

Commit

Permalink
Merge pull request #4 from hasibiqbal207/docker-fix
Browse files Browse the repository at this point in the history
Docker ignore, Readme added.
  • Loading branch information
hasibiqbal207 authored Sep 5, 2024
2 parents 7b66e7a + eb10af9 commit 7c764ab
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 26 deletions.
49 changes: 49 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Exclude node_modules to ensure dependencies are installed inside the container
node_modules

# Exclude build output directories if you're using multi-stage builds
build
dist

# Exclude logs and temporary files
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.tmp
*.swp
*.swo

# Exclude coverage reports
coverage

# Exclude environment variable files to avoid leaking sensitive information
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Exclude version control directories and files
.git
.gitignore

# Exclude Docker-related files to prevent them from being copied into the image
Dockerfile
docker-compose.yml

# Exclude IDE/editor-specific directories and files
.vscode
.idea

# Exclude OS-generated files
.DS_Store
Thumbs.db
ehthumbs.db
Desktop.ini

# Exclude miscellaneous files
*.bak
*.backup
*.orig
223 changes: 223 additions & 0 deletions README.Docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
# Docker Guide

This document provides instructions for building and running the application using Docker. It covers both development and production environments.

## Table of Contents
1. [Prerequisites](#prerequisites)
2. [Docker Setup](#docker-setup)
3. [Development Environment](#development-environment)
- Build the Docker Image
- Run the Development Container
4. [Production Environment](#production-environment)
- Build the Production Image
- Run the Production Container
5. [Managing Containers](#managing-containers)
6. [Common Docker Commands](#common-docker-commands)
7. [Docker Compose](#docker-compose)
8. [Troubleshooting](#troubleshooting)

## Why the App is Containerized

**1. Consistency Across Environments**: Containerizing the app ensures that it runs consistently across different environments (development, testing, staging, and production). By packaging the application and its dependencies into a single Docker image, you eliminate the "it works on my machine" problem, as the container behaves the same regardless of where it is deployed.

**2. Simplified Dependency Management**: Docker containers bundle the application with all its dependencies, libraries, and runtime, making it easier to manage and deploy. This encapsulation prevents issues related to missing dependencies or version mismatches, ensuring that the correct versions of libraries and tools are always used.

**3. Scalability**: Containerization makes scaling the application straightforward. Containers can be easily replicated and distributed across multiple servers or orchestrated using tools like Kubernetes. This allows the app to handle increased load efficiently by simply running more instances of the containers.

**4. Isolation and Security**: Docker containers provide process isolation, which enhances security by keeping the application isolated from the host system and other containers. This reduces the risk of conflicts between applications and minimizes the attack surface.

**5. Streamlined CI/CD Pipeline**: By using Docker, the app can be integrated into a CI/CD pipeline more easily. Containers can be built, tested, and deployed automatically, ensuring that every code change is validated in an environment identical to production.

## Prerequisites

Before you begin, ensure that you have the following software installed:

- [Docker](https://docs.docker.com/get-docker/) (version `XX.X` or later)
- [Docker Compose](https://docs.docker.com/compose/install/) (if using multi-container setup)

Check that Docker is installed by running:

```bash
docker --version
docker compose version # if applicable
```

---

## Docker Setup

The Docker configuration for this project includes:

- A **development Dockerfile** located at `docker/Dockerfile.dev` that is optimized for local development.
- A **production Dockerfile** located at `docker/Dockerfile.prod` that is optimized for production builds.

Each environment uses a slightly different configuration, as outlined in the following sections.

---

## Development Environment

### Build the Docker Image

To build the Docker image for the development environment, run the following command from the root of the project:

```bash
docker build -f docker/Dockerfile.dev -t your-app-dev .
```

### Run the Development Container

Once the image is built, you can run the development container with the following command:

```bash
docker run -p 3000:3000 -v $(pwd):/app -v /app/node_modules --rm your-app-dev
```

- **Port Mapping**: This maps port `3000` in the container to port `3000` on your host machine.
- **Volume Mounting**: The local directory is mounted to `/app` in the container to allow live reloading.
- **Remove Option**: The `--rm` flag ensures the container is removed after it stops.

To run the container in the background (detached mode), add the `-d` flag:

```bash
docker run -d -p 3000:3000 -v $(pwd):/app -v /app/node_modules --rm your-app-dev
```

---

## Production Environment

### Build the Production Image

To build the Docker image for production, run the following command:

```bash
docker build -f docker/Dockerfile.prod -t your-app-prod .
```

This command builds the production-ready image using the `docker/Dockerfile.prod` configuration.

### Run the Production Container

After building the production image, run it using:

```bash
docker run -p 6002:6002 --rm your-app-prod
```

This command:
- Exposes port `6002` to match the production app.
- Uses the `--rm` flag to remove the container after it stops.

For detached mode:

```bash
docker run -d -p 6002:6002 --rm your-app-prod
```

---

## Managing Containers

To stop a running container:

```bash
docker stop <container-id>
```

To remove a stopped container:

```bash
docker rm <container-id>
```

To list all running containers:

```bash
docker ps
```

---

## Common Docker Commands

Here are some additional commands you may find useful:

- **List Docker Images**:
```bash
docker images
```

- **Remove a Docker Image**:
```bash
docker rmi <image-id>
```

- **View Logs of a Running Container**:
```bash
docker logs <container-id>
```

- **View the Running Processes in a Container**:
```bash
docker exec -it <container-id> ps
```

---

## Docker Compose

If you have multiple services (like a database or additional backend services), you can use **Docker Compose** to manage them more easily.

### Using Docker Compose for Development

To start your development environment with Docker Compose:

1. Ensure you have a `compose.yaml` file configured for development.
2. Run the following command:

```bash
docker compose up --build
```

This command:
- Builds the images.
- Starts the containers.
- Automatically handles networking between services.

To stop and remove the containers:

```bash
docker compose down
```

### Using Docker Compose for Production

If you have a `compose.prod.yaml` for production, you can start the services with:

```bash
docker compose -f compose.prod.yaml up --build -d
```

## Troubleshooting

### Common Issues and Fixes

- **Port Already in Use**: If you see an error about the port being in use, make sure no other containers or applications are running on the same port by running:
```bash
docker ps
```

- **Cannot Connect to Docker Daemon**: If you encounter issues connecting to Docker, try restarting the Docker service or ensuring you have the correct permissions.

```bash
sudo service docker restart
```

- **Volume Permission Issues**: Sometimes, volume permissions can cause issues. Ensure that the user inside the container has the appropriate permissions to access the mounted volumes.

---

## Conclusion

This guide provides all the necessary steps for building, running, and managing your Docker containers in both development and production environments. If you encounter any issues, refer to the [Troubleshooting](#troubleshooting) section or consult Docker's official [documentation](https://docs.docker.com/).
33 changes: 8 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,17 @@ This project was created using the following technologies.
- Dot ENV ( To store secret variables )

#### Database
- Data is stored in 'Javascript Object' format. No Relational Database is used.

### Containerization Overview

#### Why the App is Containerized

**1. Consistency Across Environments**: Containerizing the app ensures that it runs consistently across different environments (development, testing, staging, and production). By packaging the application and its dependencies into a single Docker image, you eliminate the "it works on my machine" problem, as the container behaves the same regardless of where it is deployed.

**2. Simplified Dependency Management**: Docker containers bundle the application with all its dependencies, libraries, and runtime, making it easier to manage and deploy. This encapsulation prevents issues related to missing dependencies or version mismatches, ensuring that the correct versions of libraries and tools are always used.

**3. Scalability**: Containerization makes scaling the application straightforward. Containers can be easily replicated and distributed across multiple servers or orchestrated using tools like Kubernetes. This allows the app to handle increased load efficiently by simply running more instances of the containers.

**4. Isolation and Security**: Docker containers provide process isolation, which enhances security by keeping the application isolated from the host system and other containers. This reduces the risk of conflicts between applications and minimizes the attack surface.

**5. Streamlined CI/CD Pipeline**: By using Docker, the app can be integrated into a CI/CD pipeline more easily. Containers can be built, tested, and deployed automatically, ensuring that every code change is validated in an environment identical to production.

#### How the App is Containerized
The application is containerized using Docker by creating a `Dockerfile` that defines the environment and dependencies for the app, and a `docker-compose.yml` file that manages multi-container configurations, enabling seamless deployment and scaling.
- Data is stored in 'Javascript Object' format. No Relational Database is used.

## Getting Started

### Prerequisites
Before you begin, ensure you have the following software and tools installed on your machine:
- **Node.js**: Version 14.x or higher. Download Node.js
- **npm or yarn**: Node.js package managers. npm comes bundled with Node.js. Install npm or Install yarn
- **Node.js**: Version 14.x or higher. [Download Node.js](https://nodejs.org/en/download/package-manager)
- **npm or yarn**: Node.js package managers. npm comes bundled with Node.js.
- **Only if you are using Docker**
- **Docker**: Docker must be installed and running on your system. Install Docker
- **Docker Compose** : Comes bundled with Docker Desktop, or can be installed separately. Install Docker Compose [title](https://www.example.com)
- **Docker**: Docker must be installed and running on your system. [Install Docker](https://docs.docker.com/engine/install/)
- **Docker Compose** : Comes bundled with Docker Desktop, or can be installed separately. [Install Docker Compose](https://docs.docker.com/compose/install/)


### Usage Instructions
Expand Down Expand Up @@ -151,7 +134,7 @@ Docker allows you to containerize your application, simplifying deployment acros
- The simplest step is to use docker compose. To use docker compose, follow only **step 1**.
- Alternatively, you can first build and then run the container. To do this, follow **step 2 & 3**.

#### 1. Using Docker Compose**
#### 1. Using Docker Compose

To containerize your application, start by using docker compose. From the root directory of your project, use the following command to build the image:

Expand Down Expand Up @@ -182,7 +165,7 @@ To stop and remove the container, use the following command:
docker compose down portfolio-dev
```

**2. Build Docker Images**
#### 2. Build Docker Images

To containerize your application, start by building Docker images based on your environment. From the root directory of your project, use the following commands to build the image:

Expand All @@ -201,7 +184,7 @@ In these commands:
- -t tags the image with a name (portfolio-dev or portfolio-prod).


**3. Run Docker Containers**
#### 3. Run Docker Containers

After building the images, you can run the containers using the docker run command.

Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ services:
- "6002:6002"
environment:
NODE_ENV: production
restart: always
restart: always

0 comments on commit 7c764ab

Please sign in to comment.