WSL 을 이용하여 Ubuntu 에 Docker 를 이용하여 배포 - 01

2023. 7. 24. 00:00ASPNET/Docker

반응형

윈도우스에서 docker desktop 없이 docker 를 사용하고 싶을 수 있다. 

wsl 설치 부터 docker 설치 까지 과정을 확인하려면 아래를 참고하자

https://yogingang.tistory.com/426

 

솔루션 파일이 있는 폴더에 Dockerfile을 만든다. 

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS runtime
RUN mkdir -p /home/dotnet/apiserver
WORKDIR /home/dotnet/apiserver
COPY publish/. ./
ENTRYPOINT ["dotnet", "SignalRTemplate.dll"]

 

SignalRTemplate project 를 열고 publish 폴더에 publish 하도록 설정한다.

Finish 한 후 

다시 publish 를 선택 하고  들어가서 Show all settings 를 선택하자

 

위와 같이 설정 하고 save 하고 나오자

그리고 최상단에 오른쪽에 Publish 를 누르자

기본적으로 윈도우에서는 폴더가 copy 가 가능하다 

 

윈도우 탐색기에서 왼쪽 탐색 트리에서 아래와 같은 것을 찾는다.

(저는 yogingang 로 user 이름을 정했습니다. 아마 root 일 것 같습니다. )

그리고 원하는 위치에 folder 를 만들고 복사해 넣자. folder 가 읽기 전용이면 변경해 주자

 

publish 에 추가 해야 할 것이 있다. 

우리는 https 를 위해서 자체 생성한 인증서를 사용하였다. 

해당 인증서도 포함해 주어야 한다. 

만약 https 로 하지 않는다면 빼도 된다. 

(인증서 생성이 어렵다면 아래 강좌를 확인하자)

https://yogingang.tistory.com/418

 

Dockerfile 이 access deny 같은 에러가 뜨면 아래와 같은 명령을 해보자

chmod 755 Dockerfile

그리고 Dockerfile 을 이용해서 image 를 생성해 보자

 docker build -t signalr-template -f Dockerfile .

image 가 만들어졌다면 이제 container 를 만들고 실행해 보자

각각의 명령이 있지만 한번에 해보겠다. 

docker run -it -p 5004:5004 --rm signalr-template

혹시 파일을 못찾는 오류등이 나오면

appsettings.json 등 에서 자신이 혹시  "/"  대신 "\"  을 사용했는지 확인하자

윈도우에서 사용했던 역슬래쉬가 linux 에서는 무시 될 것이다 확인하자.

 

이미지를 다시 생성해야 할 경우 다음 같이 이미지를 삭제하자.

docker rmi signalr-template

 

또한 appsettings.json 에서 kestrel 관련 설정을 했다면 listen ip 에 대한 주의가 필요하다

만약 아래와 같이 했다면 docker container 안에서만 실행이 된다. 

즉 localhost 이외에 어떤 주소에도 binding 이 되지 않는 것이다. 

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2AndHttp3"
    },
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "HttpsDefaultCert": {
        "Url": "https://localhost:5004"
      }
    },

그래서 다음과같이 수정해야 한다. 

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2AndHttp3"
    },
    "Endpoints": {
      "Http": {
        "Url": "http://*:5000"
      },
      "HttpsDefaultCert": {
        "Url": "https://*:5004"
      }
    },

이와 같이 수정한 후 signalr client 로 접속하면 접근이 될 것이다. 

그러나 이 상태에서도 swagger 로의 접근은 되지 않을 것이다. 

이유는 program.cs 의 다음 코드 때문이다. 

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

우리는 publish 를 통해서 deploy 했기 때문에 swagger 와 swaggerui 가 뜨지 않는다. 

만약 크게 상관이 없고 오히려 swagger 를 외부에 공유해야 하는 상황이라면 위 if 문에서 swagger 를 빼내자

이제 publsh 를 다시 하고 실행하면 swagger 화면을 확인 할 수 있다.

 

 

자 이번에는 build 자체를 docker 를 이용하여 진행하자

Dockerfile 을 만들자.

순서는 아래와 같을 것이다. 

 

1. dotnet sdk 이미지 를 다운받는다.

2. source 파일 copy 하여 docker image 의 특정 폴더에 저장하자

3. docker buld 를 진행 하자

4. aspnet runtime 이미지를 다운받자 

5. 실행하자!

Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.sln .
COPY SignalRTemplate/*.csproj ./SignalRTemplate/
COPY SignalRClient/*.csproj ./SignalRClient/
COPY SignalRTemplate/. ./SignalRTemplate/
COPY SignalRClient/. ./SignalRClient/
#RUN dotnet restore


WORKDIR /source/SignalRTemplate
RUN dotnet publish -r linux-x64 -c release -p:PublishReadyToRun=true -o /publish


FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime
WORKDIR /publish
COPY --from=build /publish ./
ENTRYPOINT ["dotnet", "SignalRTemplate.dll"]

이제 image 를 생성해 보자

 docker build -t signalr-template -f Dockerfile .

정상적으로 image 가 생성되었다면

다음을 실행하자 

docker run -it -p 5004:5004 --rm signalr-template

Swagger 가 실행되는 지 확인해보자

 

 

 

 

관련영상

https://youtu.be/5L5Aplgjlvc

반응형