Introduction
In this final lesson, we will validate the ASP.NET Core API and SQL Server application built throughout the course using a realistic operational workflow. We will first verify that the services are running, that the API can reach the database, and that the main CRUD operations produce the expected results. We will then use docker ps, docker logs, docker inspect, docker exec, and docker network inspect to diagnose common container problems.
In the final section, we will tag the application image with a meaningful version, publish it to a registry, deploy it with a production Compose file, and return to the previous image when a new release causes a problem. The goal is not simply to run a container, but to operate a containerized application in a testable, observable, and controlled way.
A successful HTTP response alone is not enough. Confirm that the request reached the expected SQL Server by considering the HTTP response, application logs, and database state together.
1. End-to-end test plan
In our example project, the Compose services are named api and sqlserver. The API container is exposed on host port 8080, while SQL Server listens on port 1433 inside the Compose network. The API connection string uses sqlserver as the host name because services in a Compose network can reach one another by service name.
docker compose -f compose.production.yml ps
Confirm that both services are running, or healthy when health checks are configured. Then call a basic health endpoint:
curl -i http://localhost:8080/health
The expected result is an HTTP 200 response indicating that the application is healthy. A basic endpoint may only prove that the process is running. If the health check also verifies the database, SQL Server connectivity is validated in the same step.
Testing API and database operations
Now run an end-to-end scenario using the sample product endpoints. Start by reading the existing records:
curl -i http://localhost:8080/api/products
Create a new product:
curl -i -X POST http://localhost:8080/api/products \ -H "Content-Type: application/json" \ -d '{"name":"Docker Notebook","price":29.90}'
If the API returns an identifier, use it to read the record again. Then try an update and a delete:
curl -i -X PUT http://localhost:8080/api/products/1 \ -H "Content-Type: application/json" \ -d '{"name":"Docker Notebook Pro","price":39.90}' curl -i -X DELETE http://localhost:8080/api/products/1 curl -i http://localhost:8080/api/products/1
The final request should return 404 after the delete operation. This workflow verifies that the HTTP layer, Entity Framework Core, and SQL Server work together.
2. Automated integration tests
Manual curl checks are useful, but automated tests should repeat the same scenarios for every release. ASP.NET Core integration tests can use WebApplicationFactory<Program> to start the application in a test environment. These tests are different from testing the actual production container, but they provide a fast way to validate endpoint behavior.
Add Microsoft.AspNetCore.Mvc.Testing and an xUnit test package to the test project. To make the application's Program class visible to the test project, add:
public partial class Program { }
A simple test can look like this:
public class ProductApiTests : IClassFixture<WebApplicationFactory<Program>> { private readonly HttpClient _client; public ProductApiTests(WebApplicationFactory<Program> factory) { _client = factory.CreateClient(); } [Fact] public async Task GetProducts_ReturnsSuccess() { var response = await _client.GetAsync("/api/products"); response.EnsureSuccessStatusCode(); Assert.Contains("application/json", response.Content.Headers.ContentType?.MediaType); } }
Run the tests with:
dotnet test
If you want to validate the behavior of the real SQL Server container, start the test environment with Compose and run a smoke test that sends HTTP requests to the API. This validates the image, environment variables, network, migrations, and database together. Use a separate database name or volume so that test data cannot be written to production.
3. A systematic troubleshooting approach
When a request fails, do not execute random commands. Move through the system in layers: container state, logs, configuration, network, and connectivity from inside the container.
Inspecting container status and logs
docker compose -f compose.production.yml ps docker ps -a docker compose -f compose.production.yml logs --tail=100 api docker compose -f compose.production.yml logs --tail=100 sqlserver
docker ps -a also shows stopped containers. If the API exits immediately, common causes include an incorrect environment variable, a missing file, or a migration failure during startup. In SQL Server logs, look for messages related to license acceptance, memory, passwords, or the startup period.
Follow the API logs live with:
docker compose -f compose.production.yml logs -f api
Do not print the complete connection string to application logs. If a password is exposed in a log, anyone with access to the registry or server logs may be able to read it.
Checking container configuration
docker inspect catalog-api docker inspect -f '{{json .NetworkSettings.Networks}}' catalog-api docker inspect -f '{{json .Config.Env}}' catalog-api
Use docker inspect to check the image name, port mappings, environment variables, volumes, and network membership. In production, a dedicated secret-management solution should be preferred. During troubleshooting, however, it is still important to verify that the expected environment variable names were supplied.
Entering a container
docker exec -it catalog-api sh
Minimal runtime images may not include bash, so try sh first. Inside the container, inspect files and processes:
pwd ls -la printenv | sort ps
The SQL Server image may not include a SQL client. In that case, verify connectivity from the API container using a suitable temporary debug container or an application-level connection test. To check DNS resolution:
getent hosts sqlserver
If this command succeeds, the Compose service name resolves correctly. If the connection still fails, verify that the API is not using the wrong port or credentials. Container-to-container communication should use sqlserver,1433, not host-oriented localhost:1433.
Inspecting the network
docker network ls docker network inspect catalog_default
The API and SQL Server should appear in the same network. The network name can vary according to the project directory. If one service is attached to a custom network, confirm that the other service is attached to the same network as well.
4. Tagging and publishing an image
The latest tag is convenient, but it does not clearly identify which code was deployed. Prefer immutable version or commit-based tags:
docker build -t ghcr.io/acme/catalog-api:1.0.0 . docker tag ghcr.io/acme/catalog-api:1.0.0 ghcr.io/acme/catalog-api:stable
Log in to the registry and push the image:
docker login ghcr.io docker push ghcr.io/acme/catalog-api:1.0.0 docker push ghcr.io/acme/catalog-api:stable
The production server also needs suitable authorization to pull from the registry:
docker pull ghcr.io/acme/catalog-api:1.0.0
Never place registry credentials in source code, a Compose file, or a Dockerfile. Use the secret mechanism provided by your CI/CD platform and follow the registry's least-privilege principle.
5. Deployment with production Compose
In the production file, the API image comes from a registry; the server does not build the application. SQL Server data is preserved with a named volume:
services: api: image: ghcr.io/acme/catalog-api:1.0.0 container_name: catalog-api ports: - "8080:8080" environment: ASPNETCORE_ENVIRONMENT: Production ConnectionStrings__Default: Server=sqlserver,1433;Database=CatalogDb;User Id=sa;Password=${SA_PASSWORD};TrustServerCertificate=True depends_on: sqlserver: condition: service_healthy sqlserver: image: mcr.microsoft.com/mssql/server:2022-latest container_name: catalog-sql environment: ACCEPT_EULA: "Y" MSSQL_SA_PASSWORD: ${SA_PASSWORD} volumes: - sql-data:/var/opt/mssql healthcheck: test: ["CMD-SHELL", "(/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P \"$${MSSQL_SA_PASSWORD}\" -C -Q \"SELECT 1\" || exit 1)"] interval: 10s timeout: 5s retries: 12 volumes: sql-data:
The real production password must not be stored in an example file. Use a protected .env file or a secret-management mechanism on the server. Deploy with:
docker compose -f compose.production.yml pull docker compose -f compose.production.yml up -d docker compose -f compose.production.yml ps
Running pull before the update fetches images in advance and reduces surprises during the restart. Automatic migrations can introduce data-loss or locking risks, so plan the migration strategy separately.
6. Updating and rolling back
Build the new version with a 1.1.0 tag and push it to the registry. Change only the API image tag in the Compose file:
image: ghcr.io/acme/catalog-api:1.1.0
Then run:
docker compose -f compose.production.yml pull api docker compose -f compose.production.yml up -d api docker compose -f compose.production.yml logs --tail=100 -f api
Check the health endpoint, basic GET and POST operations, application logs, and the expected migration state. If the release is faulty, return to the previous immutable tag:
image: ghcr.io/acme/catalog-api:1.0.0 docker compose -f compose.production.yml pull api docker compose -f compose.production.yml up -d api
Rollback is not always only an image change. If a new migration introduced a backward-incompatible schema change, the older application may not work with the new schema. Design database migrations to be backward-compatible and staged, and maintain a backup plan.
Common mistakes
- Using localhost inside the API: Inside a container, localhost refers to the API container, not SQL Server. Use sqlserver as the service host.
- Confusing ports: Container-to-container traffic uses the container port; browser access from the host uses the published host port.
- Using latest for every deployment: It becomes difficult to identify the previous image and roll back. Use version tags.
- Deleting the volume to solve a problem: docker compose down -v can delete database data. Review the impact before running it in production.
- Writing passwords to logs: Do not log the complete connection string or all secret values.
- Trusting the running status alone: A running container does not prove that the endpoint and database operations are correct. Run smoke tests.
Practice exercise
- Start the API and SQL Server with the production Compose file.
- Call the /health and product-list endpoints with curl.
- Create a product and verify that the record exists in SQL Server.
- While following the API logs, deliberately provide an incorrect connection string and diagnose the resulting error.
- Publish images with 1.0.0 and 1.1.0 tags. Update Compose to 1.1.0; if the smoke test fails, return to 1.0.0.
Conclusion
In this lesson, we completed the lifecycle of a containerized application. We tested API and database operations end to end, examined container state, logs, configuration, and network connectivity, and then published a versioned image to a registry. Finally, we deployed with production Compose and practiced a controlled update and rollback workflow.
More important than memorizing Docker commands is learning to divide a problem into layers: Is the process running? Is the expected image being used? Are the environment values correct? Are the services on the same network? Is the database ready? Does a real HTTP request succeed? This approach provides a reliable foundation from local development through production operations.
Sources
- https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing
- https://docs.docker.com/reference/cli/docker/compose
- https://docs.docker.com/compose/how-tos/networking/
- https://learn.microsoft.com/en-us/dotnet/core/containers/sdk-publish
- https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images