Skip to main content
The Currency Exchange API is containerized using Docker, making it easy to deploy and run in any environment.

Prerequisites

Before you begin, ensure you have the following installed:
  • Docker (version 20.x or higher)
  • Docker Compose (version 2.x or higher)

Docker Configuration

The API uses a multi-stage Dockerfile based on .NET 8.0 to optimize the image size and build process.

Dockerfile Overview

The Dockerfile consists of four stages:
  1. Base Stage: Sets up the runtime environment
  2. Build Stage: Compiles the application
  3. Publish Stage: Publishes the release build
  4. Final Stage: Creates the production image

Building the Docker Image

1

Navigate to the source directory

2

Build the Docker image

This command builds the image using the Dockerfile in the current directory and tags it as currencyexchangeback.
3

Verify the image

Running with Docker Compose

The docker-compose.yaml file defines the services needed to run the application, including the PostgreSQL database.

Docker Compose Configuration

The docker-compose file includes commented-out sections for nginx reverse proxy and frontend services. Uncomment these sections if you need to deploy the full stack.

Starting the Services

1

Set the database password

Create a .env file in the same directory as docker-compose.yaml:
Make sure to use a strong password and never commit the .env file to version control.
2

Start the PostgreSQL database

This starts only the PostgreSQL service in detached mode.
3

Run the API container

4

Verify the services are running

You should see both the currencies-bd and currencies-backend containers running.

Port Mappings

The following ports are exposed by the services:

Volumes

Database Volume

The PostgreSQL database uses a named volume currencies to persist data:
This ensures that your database data survives container restarts and removals.

Initialization Scripts

Database initialization scripts are mounted from the host:
Place your SQL initialization scripts in the ../2. Init Database directory, and they will be executed automatically when the database container starts for the first time.

Full Stack Deployment

To deploy the complete stack including the API, frontend, and nginx reverse proxy, uncomment the relevant sections in docker-compose.yaml:
Then start all services:

Stopping and Removing Services

Using docker-compose down -v will delete all data in the database volume. Only use this command if you want to completely reset the application.

Troubleshooting

Viewing Logs

Common Issues

If port 8080 or 5432 is already in use, modify the port mappings in docker-compose.yaml or the docker run command:
Ensure the PostgreSQL container is running and healthy:
Check that the connection string in appsettings.json matches the database configuration.
Check the container logs for errors:
Common causes include missing environment variables or database connection issues.