I created a simple asp.net core app in Visual Studio 2019 and added docker support.
Dockerfile, .dockerignore, and docker-compose file are all created.
In a command prompt I navigate to the folder docker-compose.yml file is present and then run the command
docker-compose up
I see that the containers are created and port mappings happen so that I can browse the web app in the browser.
So when I run the following inspect command on the container
docker inspect --format="{{ .NetworkSettings.Ports}}" ContainerId
I get something like this
map[80/tcp:[{0.0.0.0 32782}]]
So now I can browse the app with http://localhost:32782/index.html
Next if I tear down the containers with
docker-compose down
the containers are stopped and deleted. Created image remains.
Now when I do a docker run against that image to start a container
docker run -it --rm ae39
a new container is created but I am not able to browse the app because there is no port mapping from container to host. I have to explicitly specify this when I use the run command. Only then I am able to browse the app running inside of the container form the host.
But when I use docker compose I dont have to specify the port mapping. Something magical happens and the ports mappings are created for me. Note that the docker-compose.yml file is plane vanilla and does not contain any port mappings. So also the Dockerfile. They are included below for reference.
My question is does docker compose automatically create port mappings? If so how? Of is that is to do something with Visual Studio 2019
version: '3.4'
services:
generator31:
image: ${DOCKER_REGISTRY-}generator31
build:
context: .
dockerfile: generator31/Dockerfile
And the dockerfile is here.
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["generator31/generator31.csproj", "generator31/"]
RUN dotnet restore "generator31/generator31.csproj"
COPY . .
WORKDIR "/src/generator31"
RUN dotnet build "generator31.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "generator31.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "generator31.dll"]