Hi c# corner Team,
I'd like an example of how to configure a .NET Core API with Docker and create a Dockerfile for both the API and the database. How can I set it up so that running the Dockerfile or Docker Compose starts both the application and the database? Could you help me with this configuration?


Jaimin ShethiyaRank #177 · 13.7K reputationPosted May 4, 2024, 12:33 AM EDT
Hello Jobin,
Please refer the below links.
https://code-maze.com/aspnetcore-app-dockerfiles/
https://www.twilio.com/en-us/blog/containerize-your-aspdotnet-core-application-and-sql-server-with-docker#:~:text=How%20to%20containerize%20your%20ASP.NET%20Core%20application%20and,for%20the%20Web%20API%20application%20...%20More%20items
https://dev.to/moe23/step-by-step-guide-on-utilising-docker-compose-with-asp-net-core-sql-server-2e54
https://medium.com/@saurabh.singh0829/how-to-create-docker-compose-dockerfile-for-net-core-application-59390475b924
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted May 5, 2024, 2:10 AM EDT
Please refer below thread for reference,
https://dev.to/gbengelebs/how-to-containerize-an-asp-netcore-api-and-mysql-with-docker-compose-1m5c
https://medium.com/@jaydeepvpatil225/containerization-of-the-net-core-7-web-api-using-docker-3abdd543f78a
Sample for setting up API and dockker,
Create a .NET Core API: dotnet new webapi -n MyApi
Create a Dockerfile for the API:
# Use the official .NET Core SDK image as a build environment FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env # Set the working directory in the container WORKDIR /app # Copy the project file and restore dependencies COPY *.csproj ./ RUN dotnet restore # Copy the remaining source code COPY . ./ # Build the application RUN dotnet publish -c Release -o out # Use the official ASP.NET Core runtime image FROM mcr.microsoft.com/dotnet/aspnet:5.0 # Set the working directory in the container WORKDIR /app # Copy the built application from the build environment COPY --from=build-env /app/out . # Expose port 80 to the outside world EXPOSE 80 # Define the command to run the application ENTRYPOINT ["dotnet", "MyApi.dll"]Build and run the Docker image: docker build -t myapi .
Run the Docker container: docker run -d -p 8080:80 --name myapi-container myapi
Dockerfile for building and running SQL Server:
# Use the official SQL Server 2019 image from Microsoft as a base image FROM mcr.microsoft.com/mssql/server:2019-latest AS build # Set environment variables ENV ACCEPT_EULA=Y ENV SA_PASSWORD=YourStrong!Passw0rd ENV MSSQL_PID=Express ENV MSSQL_TCP_PORT=1433 # Create a new directory for SQL Server database files RUN mkdir -p /var/opt/mssql/data # Copy the SQL Server setup script into the container COPY setup.sql /usr/src/app/setup.sql # Grant permissions to the SQL Server setup script RUN chmod +x /usr/src/app/setup.sql # Set the working directory to the directory containing the SQL Server startup script WORKDIR /usr/src/app # Run the SQL Server setup script on container startup CMD /bin/bash -c '/opt/mssql/bin/sqlservr & /usr/src/app/setup.sh'build and run the Docker image as follows:
docker build -t sql-server . docker run -d -p 1433:1433 --name sql-server-container sql-serverNaimish MakwanaRank #324 · 7.7K reputationPosted May 7, 2024, 3:08 AM EDT
Here’s a basic example of how you can set up a .NET Core API with Docker and Docker Compose. This example includes a Dockerfile for the API and a Docker Compose file to start both the API and a SQL Server database.
First, let’s create a Dockerfile for the .NET Core API. This file will be used to create a Docker image of your application.
# Dockerfile FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build WORKDIR /app # copy csproj and restore as distinct layers COPY *.csproj ./ RUN dotnet restore # copy everything else and build app COPY . ./ RUN dotnet publish -c Release -o out FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime WORKDIR /app COPY --from=build /app/out ./ ENTRYPOINT ["dotnet", "YourApp.dll"]Replace
YourApp.dllwith the name of your application’s DLL file.Next, let’s create a
docker-compose.ymlfile. This file will be used to start both the API and the database.# docker-compose.yml version: '3.4' services: db: image: "mcr.microsoft.com/mssql/server" environment: SA_PASSWORD: "Your_password123" ACCEPT_EULA: "Y" api: image: your-api-image-name build: context: . dockerfile: Dockerfile depends_on: - dbReplace
your-api-image-namewith the name of your Docker image, andYour_password123with your chosen password.With these files, you can use Docker Compose to start both the API and the database. Run the following command in the directory containing your
docker-compose.ymlfile:This command will start both the API and the database. The API will be able to connect to the database using the service name
dbas the hostname.Please note that this is a basic example and might need to be adjusted based on your specific needs. For example, you might need to adjust the Dockerfile to include any necessary environment variables or to handle multiple projects, and you might need to adjust the Docker Compose file to include any necessary ports or volumes. Also, remember to replace placeholders with your actual values.
Thanks