Up:: Programming

Docker

Docker Files

  • multi-stage docker file:
FROM mcr.microsoft.com/dotnet/core/aspnetcore:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
 
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApplication1.csproj", ""]
RUN dotnet restore "./WebApplication1.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
 
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
 
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

Common Docker Commands

Container Management

  • List running containers: docker ps
  • List all containers: docker ps -a
  • Stop a container: docker stop <container_id>
  • Start a container: docker start <container_id>
  • Remove a container: docker rm <container_id>
  • View logs: docker logs -f <container_id>
  • Access container shell: docker exec -it <container_id> /bin/bash (or /bin/sh)

Image Management

  • List images: docker images
  • Build an image: docker build -t <tag_name> .
  • Remove an image: docker rmi <image_id>
  • Prune unused images: docker image prune

Docker Compose Commands

  • Start services (detached): docker-compose up -d
  • Stop services: docker-compose down
  • View logs: docker-compose logs -f
  • List services: docker-compose ps

Updating an Application (Docker Compose)

To update an application to the latest version of the images defined in your docker-compose.yml:

# Pull the latest images
docker-compose pull
 
# Recreate and restart the containers with the new images
docker-compose up -d

Alternatively, in newer versions of Docker Compose:

docker-compose up -d --pull always

📇 Additional Metadata