Docker Compose Tutorial: Run a Node.js App With Postgres

Written by

in

TL;DR: Create a docker-compose.yml file that defines services for your Node.js application and a PostgreSQL database instance. Run the command docker compose up to build and start both containers with automatic network configuration and data persistence.

Step 1: Prepare Your Project Structure

Ensure your Node.js application directory contains a Dockerfile and a package.json file. The Dockerfile should install dependencies and start the server. Create a new file named .env in the root directory to store sensitive variables like database passwords. This keeps credentials out of your version control and allows easy configuration changes without modifying code.

If you want to dig deeper, check out our guide on Longevity Clinics Boom: The Rise of Anti-Aging Care in Major.

Step 2: Define the Docker Compose File

Create a file named docker-compose.yml in your project root. This file orchestrates your containers. Define a service named app that builds from your local Dockerfile. Map port 3000 inside the container to port 3000 on your host machine. Define a second service named db using the official postgres:15 image. Set environment variables for POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB using variables from your .env file. Add a volumes section to the db service to persist data across container restarts by mapping a local directory or named volume to /var/lib/postgresql/data.

Step 3: Configure Network Dependencies

In the app service definition, add a depends_on key listing db. This ensures the database container starts before your application. Crucially, Docker Compose creates a default network for all services defined in the file. Inside the application code, use the service name db as the hostname for your database connection string, not localhost. For example, your connection string should look like postgresql://user:pass@db:5432/mydb. This internal DNS resolution allows containers to communicate securely without exposing ports to the host.

Step 4: Build and Run the Stack

Navigate to your project directory in the terminal. Execute the command docker compose up --build. The --build flag ensures your Node.js image is rebuilt if you have changed dependencies or code. Docker will pull the PostgreSQL image if it is not already present, create the necessary networks, and start both services. Use docker compose logs -f to monitor real-time output from both containers. To stop the stack, run docker compose down. Add the -v flag to this command if you want to remove persisted data volumes, but be careful as this deletes your database data permanently.

Best Practices for Production

Always use health checks in your docker-compose.yml to ensure the database is fully ready before the application attempts to connect. Use named volumes instead of bind mounts for better performance and portability. Regularly update your base images to patch security vulnerabilities. Keep your Dockerfile multi-stage to reduce the final image size, excluding development dependencies and source code from the production container.

FAQ

Q: Why can’t my app connect to the database?
A: Ensure you are using the service name db as the hostname in your connection string instead of localhost. Also, verify that the database container is running and healthy by checking the logs with docker compose logs db. If the app starts before the database is ready, implement a

Related Articles

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *