Azure Kubernetes Service
Building and containerizing sample application
Containerizing the application
With your ASP.NET Core application image (kodekloudapp:v1
) built, you can now run and customize it without rebuilding. This guide shows how to:
- Launch the container in detached mode
- Map host ports to container ports
- Edit
appsettings.json
at runtime - Override settings using environment variables
1. Run the Container
Use docker run
to start your app in detached mode, mapping port 8080
on your host to port 80
in the container and naming it for easy management:
docker run -d \
-p 8080:80 \
--name corecloudapp \
kodekloudapp:v1
Verify it’s running:
docker ps
Your application should now be live at
http://localhost:8080
Note
Containers are ephemeral. Any file changes made inside will be lost if the container is removed.
2. Edit Configuration In-Place
ASP.NET Core reads appsettings.json
at startup via IConfiguration
. You can exec into the running container, modify the JSON file, and immediately see the changes—no image rebuild required.
- Open a shell inside the container:
docker exec -it corecloudapp /bin/bash
- Install
vim
(the base image is minimal):apt-get update && apt-get install -y vim
- Edit the settings file:
cd /app vim appsettings.json
- In
vim
, update the"Message"
property:{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "Message": "Hello World from VIM !!!" }
- Save and exit. Reload http://localhost:8080 in your browser to see the new message.
Warning
Editing configuration inside a running container is ideal for demos and debugging—but not for production. Prefer mounting configuration files or injecting environment variables instead.
3. Override with Environment Variables
ASP.NET Core’s configuration provider loads environment variables after JSON, allowing overrides without editing files.
- Stop and remove the current container:
docker rm -f corecloudapp
- Launch a new container with the
Message
environment variable:docker run -d \ -e Message="From Env" \ -p 8080:80 \ --name corecloudapp \ kodekloudapp:v1
- Refresh http://localhost:8080. The message should read From Env.
4. Cleanup
When you’re done, remove the container:
docker rm -f corecloudapp
Docker Command Reference
Command | Purpose |
---|---|
docker run -d -p … | Start a container in detached mode |
docker exec -it … | Open an interactive shell inside a container |
docker rm -f … | Force remove a running or stopped container |
Links and References
Watch Video
Watch video content