Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 4개 있습니다.)
(시리즈 글이 7개 있습니다.)
개발 환경 구성: 401. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성
; https://www.sysnet.pe.kr/2/0/11708

개발 환경 구성: 402. .NET Core 콘솔 응용 프로그램을 docker로 실행/디버깅하는 방법
; https://www.sysnet.pe.kr/2/0/11709

VS.NET IDE: 143. Visual Studio - ASP.NET Core Web Application의 "Enable Docker Support" 옵션으로 달라지는 점
; https://www.sysnet.pe.kr/2/0/12171

VS.NET IDE: 144. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/12197

VS.NET IDE: 185. Visual Studio - 원격 Docker container 내에 실행 중인 응용 프로그램에 대한 디버깅
; https://www.sysnet.pe.kr/2/0/13351

닷넷: 2156. .NET 7 이상의 콘솔 프로그램을 (dockerfile 없이) 로컬 docker에 배포하는 방법
; https://www.sysnet.pe.kr/2/0/13437

개발 환경 구성: 704. Visual Studio - .NET 8 프로젝트부터 dockerfile에 추가된 "USER app" 설정
; https://www.sysnet.pe.kr/2/0/13547




.NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성

Visual Studio에서 docker 지원은 ASP.NET Core Web Application인 경우에만 옵션을 제공하고 .NET Core Console 프로그램인 경우에는 그렇지 않습니다. 때로는 콘솔 프로그램도 docker로 테스트하고 싶을 때가 있는데, 이런 경우 당연히 수작업으로 설정하면 가능합니다. 방법은 다음의 글에 잘 나와 있습니다.

Running a .NET Core ConsoleApp in Docker 
; http://pvlerick.github.io/2017/02/running-dotnetcore-consoleapp-in-docker

그런데, 저렇게 매번 명령행에서 치는 것도 때론 번거롭습니다. 이번에는 Visual Studio의 도움 없이 배포(publish) 시에 docker 이미지를 함께 만들어 보겠습니다. 어떻게 하는지 간단하게 구성해 볼까요? ^^

테스트를 위해 다음의 소스 코드를 가진 콘솔 프로젝트를 하나 만들고,

using System;

namespace netcore_app  // 이 글의 테스트 프로젝트 이름은 netcore_app
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

예전에 다뤘던 Task를 이용해,

MSBuild - 빌드 전/후, 배포 전/후 실행하고 싶은 Task 정의
; https://www.sysnet.pe.kr/2/0/11507

csproj 파일에 다음과 같은 Task를 추가할 수 있습니다.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <OutputType>Exe</OutputType>
    </PropertyGroup>

    <Target Name="MyAfterPublish" AfterTargets="Publish">
        <Message Importance="high" Text="Creating docker images: $(ProjectName)" />
        <Exec Command="docker build --build-arg CONFIGNAME=$(ConfigurationName) -t $(ProjectName.ToLower()) ." WorkingDirectory="$(ProjectDir)" />
    </Target>

</Project>

남은 작업은 docker build를 실행하므로 프로젝트에 다음의 내용으로 "Dockerfile"을 추가하는 것입니다.

FROM microsoft/dotnet:2.1-runtime AS base

FROM base AS final
ARG CONFIGNAME

WORKDIR /app
COPY /bin/${CONFIGNAME}/netcoreapp2.1/publish/  .

ENTRYPOINT ["dotnet", "netcore_app.dll"]

이제부터는 Visual Studio에서 해당 프로젝트에 대해 "Publish"를 할 때마다 프로젝트 이름의 docker 이미지가 생성되고, 명령행에서 다음과 같이 실행해 볼 수 있습니다.

c:\temp> docker run --rm -it netcore_app
Hello World!

(첨부 파일은 이 글의 테스트 프로젝트를 포함합니다.)

만약 docker 이미지 생성뿐만 아니라 docker 환경 내에서의 실행 및 디버깅까지 원한다면 다음의 글을 참고하시면 됩니다.

.NET Core 콘솔 응용 프로그램을 docker로 실행/디버깅하는 방법
; https://www.sysnet.pe.kr/2/0/11709




docker build를 테스트하다 보면 태그 이름이 "<none>"인 이미지들이 제법 생겨납니다.

c:\temp> docker images
REPOSITORY          TAG                      IMAGE ID            CREATED             SIZE
netcore_app         latest                   7bac719feebf        4 minutes ago       180MB
<none>              <none>                   19ae9e1302cd        20 minutes ago      180MB
...[생략]...

검색해 보면 다음과 같이 지울 수 있다고 하는데,

docker rmi $(docker images -f "dangling=true" -q)

실제로 해보면 윈도우 docker의 경우 오류가 발생합니다.

c:\temp> docker rmi $(docker images -f "dangling=true" -q)
unknown shorthand flag: 'q' in -q)
See 'docker rmi --help'.

재미있는 것은 단독으로 사용하는 경우에는 잘 실행이 됩니다.

c:\temp> docker images -f "dangling=true" -q
19ae9e1302cd

어쨌든, 오류가 발생하니 다른 명령어를 찾아봤는데 다행히 하나 있습니다. ^^

docker image prune
; https://docs.docker.com/engine/reference/commandline/image_prune/

따라서 다음과 같이 실행해 주면 됩니다.

docker image prune -f

이렇게 실행하면 Container로 만들어져 있지 않는 한 <none> 이름을 가진 이미지들이 전부 삭제됩니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 2/27/2020]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2018-12-07 02시11분
docker - 윈도우에서 실행 시 "unknown shorthand flag" 오류
; http://www.sysnet.pe.kr/2/0/11738
정성태
2023-03-03 09시25분
정성태

... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...
NoWriterDateCnt.TitleFile(s)
11821정성태2/20/201912088오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201915235오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201913905Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
11818정성태2/20/201912830VS.NET IDE: 131. Visual Studio 2019 Preview의 닷넷 프로젝트 빌드가 20초 이상 걸리는 경우 [2]
11817정성태2/17/20199988오류 유형: 514. WinDbg Preview 실행 오류 - Error : DbgX.dll : WindowsDebugger.WindowsDebuggerException: Could not load dbgeng.dll
11816정성태2/17/201912489Windows: 157. 윈도우 스토어 앱(Microsoft Store App)을 명령행에서 직접 실행하는 방법
11815정성태2/14/201911383오류 유형: 513. Visual Studio 2019 - VSIX 설치 시 "The extension cannot be installed to this product due to prerequisites that cannot be resolved." 오류 발생
11814정성태2/12/201910201오류 유형: 512. VM(가상 머신)의 NT 서비스들이 자동 시작되지 않는 문제
11813정성태2/12/201911806.NET Framework: 809. C# - ("Save File Dialog" 등의) 대화 창에 확장 속성을 보이는 방법
11812정성태2/11/20199631오류 유형: 511. Windows Server 2003 VM 부팅 후 로그인 시점에 0xC0000005 BSOD 발생
11811정성태2/11/201913418오류 유형: 510. 서버 운영체제에 NVIDIA GeForce Experience 실행 시 wlanapi.dll 누락 문제
11810정성태2/11/201911484.NET Framework: 808. .NET Profiler - GAC 모듈에서 GAC 비-등록 모듈을 참조하는 경우의 문제
11809정성태2/11/201912994.NET Framework: 807. ClrMD를 이용해 메모리 덤프 파일로부터 특정 인스턴스를 참조하고 있는 소유자 확인
11808정성태2/8/201914049디버깅 기술: 123. windbg - 닷넷 응용 프로그램의 메모리 누수 분석
11807정성태1/29/201912359Windows: 156. 가상 디스크의 용량을 복구 파티션으로 인해 늘리지 못하는 경우 [4]
11806정성태1/29/201912110디버깅 기술: 122. windbg - 덤프 파일로부터 PID와 환경 변수 등의 정보를 구하는 방법
11805정성태1/28/201913945.NET Framework: 806. C# - int []와 object []의 차이로 이해하는 제네릭의 필요성 [4]파일 다운로드1
11804정성태1/24/201911982Windows: 155. diskpart - remove letter 이후 재부팅 시 다시 드라이브 문자가 할당되는 경우
11803정성태1/10/201911488디버깅 기술: 121. windbg - 닷넷 Finalizer 스레드가 멈춰있는 현상
11802정성태1/7/201912873.NET Framework: 805. 두 개의 윈도우를 각각 실행하는 방법(Windows Forms, WPF)파일 다운로드1
11801정성태1/1/201913908개발 환경 구성: 427. Netsh의 네트워크 모니터링 기능 [3]
11800정성태12/28/201813193오류 유형: 509. WCF 호출 오류 메시지 - System.ServiceModel.CommunicationException: Internal Server Error
11799정성태12/19/201813992.NET Framework: 804. WPF(또는 WinForm)에서 UWP UI 구성 요소 사용하는 방법 [3]파일 다운로드1
11798정성태12/19/201813224개발 환경 구성: 426. vcpkg - "Building vcpkg.exe failed. Please ensure you have installed Visual Studio with the Desktop C++ workload and the Windows SDK for Desktop C++"
11797정성태12/19/201810582개발 환경 구성: 425. vcpkg - CMake Error: Problem with archive_write_header(): Can't create '' 빌드 오류
11796정성태12/19/201810230개발 환경 구성: 424. vcpkg - "File does not have expected hash" 오류를 무시하는 방법
... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...