SagiK-Repository / Learn-Git-Lab-CICD

Git Lab에서 CICD를 활용하는 방법을 익힌다. (Local)
0 stars 0 forks source link

Window Docker Run #3

Closed SAgiKPJH closed 6 months ago

SAgiKPJH commented 6 months ago

참고 사이트 : https://docs.docker.com/language/dotnet/?uuid=03850096-754D-419A-9327-660A06270D02

SAgiKPJH commented 6 months ago

.Net 6.0 Window Docker 얻기

$ docker init

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml

Let's get started!

? What application platform does your project use?  [Use arrows to move, type to filter]
> ASP.NET - (detected) suitable for an ASP.NET application
  Go - suitable for a Go server application
  Python - suitable for a Python server application
  Node - suitable for a Node server application
  Rust - suitable for a Rust server application
  Other - general purpose starting point for containerizing your application
  Don't see something you need? Let us know!
  Quit

image

SAgiKPJH commented 6 months ago
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

################################################################################

# Learn about building .NET container images:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md

# Create a stage for building the application.
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
ARG TARGETARCH

COPY . /source

WORKDIR /source/AppData/Local/Microsoft/VisualStudio/BackupFiles/BuildTest

# Build the application.
# Leverage a cache mount to /root/.nuget/packages so that subsequent builds don't have to re-download packages.
# If TARGETARCH is "amd64", replace it with "x64" - "x64" is .NET's canonical name for this and "amd64" doesn't
#   work in .NET 6.0.
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
    dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

# If you need to enable globalization and time zones:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/enable-globalization.md
################################################################################
# Create a new stage for running the application that contains the minimal
# runtime dependencies for the application. This often uses a different base
# image from the build stage where the necessary files are copied from the build
# stage.
#
# The example below uses an aspnet alpine image as the foundation for running the app.
# It will also use whatever happens to be the most recent version of that tag when you
# build your Dockerfile. If reproducability is important, consider using a more specific
# version (e.g., aspnet:7.0.10-alpine-3.18),
# or SHA (e.g., mcr.microsoft.com/dotnet/aspnet@sha256:f3d99f54d504a21d38e4cc2f13ff47d67235efeeb85c109d3d1ff1808b38d034).
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS final
WORKDIR /app

# Copy everything needed to run the app from the "build" stage.
COPY --from=build /app .

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser
USER appuser

ENTRYPOINT ["dotnet", "~AutoRecover.BuildTests.dll"]
SAgiKPJH commented 6 months ago

Window .NetDocker 간소화

FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
ARG TARGETARCH

RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
    dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS final
WORKDIR /app

ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser
USER appuser

ENTRYPOINT ["dotnet", "~AutoRecover.BuildTests.dll"]
SAgiKPJH commented 6 months ago

WPF는 Window 환경 전용입니다. *(참조링크(1), 참조링크(2)) 따라서 Linux 환경에서 Window Server 실행은 불가합니다.

SAgiKPJH commented 6 months ago

시도한 dockerfile

# BackUp
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
ARG TARGETARCH

RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
    dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS final
WORKDIR /app

ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser
RUN echo "appuser:appuser" | chpasswd
# appuser에게 sudo 권한 부여
RUN echo "appuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# SSH 설치
USER root
RUN apk update && apk add --no-cache openssh-server
RUN ssh-keygen -A

# 폴더 권한 부여
RUN chown -R appuser:appuser /app
RUN chmod -R 755 /app

# SSH 서버 실행
CMD ["/usr/sbin/sshd", "-D"]

WORKDIR /app

ARG UID=10001 RUN adduser \ --disabled-password \ --gecos "" \ --home "/nonexistent" \ --shell "/sbin/nologin" \ --no-create-home \ --uid "${UID}" \ appuser RUN echo "appuser:appuser" | chpasswd

appuser에게 sudo 권한 부여

RUN echo "appuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

SSH 설치

USER root RUN apk update && apk add --no-cache openssh-server RUN ssh-keygen -A

폴더 권한 부여

RUN chown -R appuser:appuser /app RUN chmod -R 755 /app

SSH 서버 실행

CMD ["/usr/sbin/sshd", "-D"]