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:
Base Stage : Sets up the runtime environment
Build Stage : Compiles the application
Publish Stage : Publishes the release build
Final Stage : Creates the production image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY [ "." , "./" ]
RUN dotnet restore "./CurrencyExchange/CurrencyExchange.API.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "./CurrencyExchange/CurrencyExchange.API.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./CurrencyExchange/CurrencyExchange.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT [ "dotnet" , "CurrencyExchange.API.dll" ]
Building the Docker Image
Navigate to the source directory
Build the Docker image
docker build -t currencyexchangeback .
This command builds the image using the Dockerfile in the current directory and tags it as currencyexchangeback.
Verify the image
docker images | grep currencyexchangeback
Running with Docker Compose
The docker-compose.yaml file defines the services needed to run the application, including the PostgreSQL database.
Docker Compose Configuration
services :
postgres :
image : postgres:17
container_name : currencies-bd
environment :
POSTGRES_DB : "currencies"
POSTGRES_USER : "alexpg"
POSTGRES_PASSWORD : ${password}
PGDATA : "/var/lib/postgresql/data/pgdata"
ports :
- "5432:5432"
volumes :
- ../2. Init Database:/docker-entrypoint-initdb.d
- currencies:/var/lib/postgresql/data
volumes :
currencies :
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
Set the database password
Create a .env file in the same directory as docker-compose.yaml: password = your_secure_password
Make sure to use a strong password and never commit the .env file to version control.
Start the PostgreSQL database
docker-compose up -d postgres
This starts only the PostgreSQL service in detached mode.
Run the API container
docker run -d \
--name currencies-backend \
--network host \
-p 8080:8080 \
currencyexchangeback
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:
Service Container Port Host Port Description API 8080 8080 HTTP endpoint for the API API 8081 8081 HTTPS endpoint (optional) PostgreSQL 5432 5432 Database connection port
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:
volumes :
- ../2. Init Database:/docker-entrypoint-initdb.d
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:
networks :
currencies-network :
driver : bridge
services :
nginx :
image : nginx:stable-alpine
container_name : currencies-proxy
volumes :
- ./nginx.conf:/etc/nginx/nginx.conf
ports :
- "80:3000"
restart : always
networks :
- currencies-network
depends_on :
- app
front :
image : currencyexchangefront
container_name : currencies-frontend
expose :
- "4200"
restart : always
networks :
- currencies-network
depends_on :
- app
app :
image : currencyexchangeback
container_name : currencies-backend
expose :
- "8080"
networks :
- currencies-network
depends_on :
- postgres
postgres :
# ... existing postgres configuration ...
networks :
- currencies-network
Then start all services:
Stopping and Removing Services
Stop Services
Stop and Remove
Remove with Volumes
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
API Logs
Database Logs
Follow Logs
docker logs currencies-backend
Common Issues
If port 8080 or 5432 is already in use, modify the port mappings in docker-compose.yaml or the docker run command: ports :
- "5433:5432" # Use host port 5433 instead
Database connection refused
Ensure the PostgreSQL container is running and healthy: docker ps
docker logs currencies-bd
Check that the connection string in appsettings.json matches the database configuration.
Container exits immediately