This document provides instructions for building and running the application using Docker. It covers both development and production environments.
- Prerequisites
- Docker Setup
- Development Environment
- Build the Docker Image
- Run the Development Container
- Production Environment
- Build the Production Image
- Run the Production Container
- Managing Containers
- Common Docker Commands
- Docker Compose
- Troubleshooting
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.
Before you begin, ensure that you have the following software installed:
- Docker (version
XX.X
or later) - Docker Compose (if using multi-container setup)
Check that Docker is installed by running:
docker --version
docker compose version # if applicable
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.
To build the Docker image for the development environment, run the following command from the root of the project:
docker build -f docker/Dockerfile.dev -t your-app-dev .
Once the image is built, you can run the development container with the following command:
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 port3000
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:
docker run -d -p 3000:3000 -v $(pwd):/app -v /app/node_modules --rm your-app-dev
To build the Docker image for production, run the following command:
docker build -f docker/Dockerfile.prod -t your-app-prod .
This command builds the production-ready image using the docker/Dockerfile.prod
configuration.
After building the production image, run it using:
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:
docker run -d -p 6002:6002 --rm your-app-prod
To stop a running container:
docker stop <container-id>
To remove a stopped container:
docker rm <container-id>
To list all running containers:
docker ps
Here are some additional commands you may find useful:
-
List Docker Images:
docker images
-
Remove a Docker Image:
docker rmi <image-id>
-
View Logs of a Running Container:
docker logs <container-id>
-
View the Running Processes in a Container:
docker exec -it <container-id> ps
If you have multiple services (like a database or additional backend services), you can use Docker Compose to manage them more easily.
To start your development environment with Docker Compose:
-
Ensure you have a
compose.yaml
file configured for development. -
Run the following command:
docker compose up --build
This command:
- Builds the images.
- Starts the containers.
- Automatically handles networking between services.
To stop and remove the containers:
docker compose down
If you have a compose.prod.yaml
for production, you can start the services with:
docker compose -f compose.prod.yaml up --build -d
-
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:
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.
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.
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 section or consult Docker's official documentation.