Overview
The Adoptme API is fully containerized and can be deployed using Docker. This guide covers building the Docker image, pushing it to Docker Hub, and running the containerized application.Prerequisites
- Docker installed (Docker Desktop or Docker Engine)
- Docker Hub account (for pushing images)
- Node.js 20.11.0 or higher
- MongoDB connection URL
Dockerfile Configuration
The project uses the following Dockerfile:Key Configuration Details
- Base Image: Node.js 20.11.0
- Working Directory:
/app - Exposed Port: 3000
- Start Command:
npm start
Docker Ignore File
The.dockerignore file excludes sensitive files from the image:
Building and Deploying
Build the Docker Image
Navigate to the project directory and build the image:Replace Verify the image was created successfully in Docker Desktop or by running:
DOCKER_USERNAME with your Docker Hub username and serverxxxx with your desired image name.Real Example:Push to Docker Hub
Push your image to Docker Hub:Real Example:The image will be available at
https://hub.docker.com/r/DOCKER_USERNAME/serverxxxxPull the Image (Optional)
On the deployment server, pull the image from Docker Hub:Or download it directly from Docker Hub at:
https://hub.docker.com/r/fabil2025/serverentregafinalContainer Configuration
Required Environment Variables
The container requires the following environment variables to start successfully:| Variable | Description | Example |
|---|---|---|
PORT | Internal application port | 3000 |
MONGO_URL | MongoDB connection URL | mongodb://host:27017 |
DB_NAME | Database name | adoptme |
Docker Desktop Configuration
When running the container through Docker Desktop:- Click the Play button next to the image
- Configure the following settings:
- Container Name: Choose a descriptive name (e.g.,
adoptme-api) - Ports: Map an external port to internal port 3000 (e.g.,
8080:3000) - Environment Variables:
- Container Name: Choose a descriptive name (e.g.,
Port Mapping
The Dockerfile exposes port 3000. When running the container, map it to any available host port:Volumes (Optional)
For persistent logging or file storage, mount volumes:Testing the Deployment
Once the container is running, verify the deployment:Troubleshooting
Container Won’t Start
-
Check the container logs:
-
Verify environment variables are set correctly:
- Ensure MongoDB is accessible from the container
Connection Refused
- Verify port mapping with
docker ps - Check firewall rules on the host
- Ensure the application is listening on 0.0.0.0 inside the container
MongoDB Connection Errors
- Verify
MONGO_URLincludes the correct protocol (mongodb://ormongodb+srv://) - Check MongoDB authentication credentials
- Ensure MongoDB allows connections from the Docker network
For production deployments, consider using Docker Compose or Kubernetes for better orchestration and scaling capabilities.