본문 바로가기

DOCKER

DOCKER c++로 프로그램 만들어서 windows 컨테이너 배포

728x90
반응형

사내 딥러닝 모델들의 서버가 윈도우라서, 윈도우 컨테이너에 대해서 알아보고 테스트하는 과정을 포스팅함.

C++로 hello world를 작성하고 (c++ 할줄몰라서 세팅 해멧음.. 그래도 내가 먼저 helloworld를 로컬에 뿌려보고 잘되야 도커파일을 작성하니까..)

도커로 윈도우 이미지로 배포하여 도커로 hello world를 출력되게

1. 도커데스크톱 설치 후 아래 문구 나오게끔 변경

2. 프로젝트 폴더 생성

3.hello.cpp 작성

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

 

4. 같은 경로에 Dockerfile 작성

# 베이스 이미지 설정
FROM mcr.microsoft.com/windows/servercore:ltsc2019


# Visual Studio Build Tools를 설치하여 C++ 컴파일러를 제공
RUN powershell -Command \
    Invoke-WebRequest -Uri https://aka.ms/vs/17/release/vs_buildtools.exe -OutFile vs_buildtools.exe ; \
    Start-Process vs_buildtools.exe -ArgumentList '--quiet --wait --norestart --nocache --installPath C:\BuildTools --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended' -NoNewWindow -Wait ; \
    Remove-Item -Force vs_buildtools.exe

# hello.cpp 파일을 컨테이너로 복사
COPY hello.cpp C:/hello.cpp

# C++ 프로그램을 컴파일
RUN "C:\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64 && cl C:/hello.cpp /Fe:C:/hello.exe

# 프로그램 실행
CMD ["C:\\hello.exe"]

 

5. 터미널에 접근하여 같은 경로로 cd맞춰주고 도커이미지 생성

docker build -t hello_world_cpp .

 

6.이미지 생성 확인 youjaehyun~~ 은 허브에 올린거라 무시하셔도 됨.

 

7. 다른 쉘켜서 이미지실행

* 다소 helloworld만 입력하는것도 빌드와 실행이 느린데,

아래 도커 파일처럼 멀티 스테이지 빌드를 사용하면 더빠르게 사용이 가능함.

# 베이스 이미지 설정
FROM mcr.microsoft.com/windows/servercore:ltsc2019 AS builder


# Visual Studio Build Tools를 설치하여 C++ 컴파일러를 제공
RUN powershell -Command \
    Invoke-WebRequest -Uri https://aka.ms/vs/17/release/vs_buildtools.exe -OutFile vs_buildtools.exe ; \
    Start-Process vs_buildtools.exe -ArgumentList '--quiet --wait --norestart --nocache --installPath C:\BuildTools --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended' -NoNewWindow -Wait ; \
    Remove-Item -Force vs_buildtools.exe

# hello.cpp 파일을 컨테이너로 복사
COPY hello.cpp C:/hello.cpp
# C++ 프로그램을 컴파일
RUN "C:\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64 && cl C:/hello.cpp /Fe:C:/hello.exe


# 런타임 단계
FROM mcr.microsoft.com/windows/nanoserver:ltsc2019
COPY --from=builder C:/hello.exe C:/hello.exe
CMD ["C:\\hello.exe"]
728x90
반응형