1. Dockerfile 생성
본격적으로 Dockerfile을 생성하기 전에 먼저 기본 이미지로 사용할 AWS Deep Learning Containers(DLCs) 딥러닝 컨테이너 이미지를 선택해야 합니다.
아래에서 원하는 이미지 URL을 가져올 수 있습니다.
GitHub - aws/deep-learning-containers: AWS Deep Learning Containers (DLCs) are a set of Docker images for training and serving m
AWS Deep Learning Containers (DLCs) are a set of Docker images for training and serving models in TensorFlow, TensorFlow 2, PyTorch, and MXNet. - GitHub - aws/deep-learning-containers: AWS Deep Lea...
github.com
지역 Region, 프레임워크 Framework(Tensorflow, PyTorch, etc.) 종류 및 버전, 용도(Train/Inference), OS, CUDA 버전, 컴퓨팅 환경(EC2, GPU/CPU), 파이썬 버전에 따라 원하는 이미지를 선택하면 됩니다.
아래 그림처럼 원하는 이미지 URL을 복사해서 나에게 맞는 리전으로만 바꿔서 사용합니다.
이를 바탕으로 Dockerfile을 작성하면 다음과 같습니다.
FROM 763104351884.dkr.ecr.ap-northeast-2.amazonaws.com/pytorch-training:1.12.1-gpu-py38-cu116-ubuntu20.04-ec2
WORKDIR /app
COPY ./ ./
# Install dependencies
RUN pip install -r requirements.txt --no-cache-dir
# 기타 원하는 명령어 적기
aws s3 cp s3://{Bucket_Name}/{File_Name} ./{New_File_Name}
# 컨테이너에서 실행하고자 하는 명령어 적기
ENTRYPOINT ["python", "main.py"]
2. Image Build와 ECR Push
이제 docker에 로그인을 하고, shell에서 docker build -t {이미지 이름} .
명령을 실행하면 이미지가 빌드됩니다.
중요한 건 로그인을 할 때에는 내 소유의 계정이 아니라, AWS DLCs의 계정을 입력해야 한다는 점입니다.
Building AWS Deep Learning Containers Custom Images - AWS Deep Learning Containers
Building AWS Deep Learning Containers Custom Images How to Build Custom Images We can easily customize both training and inference with Deep Learning Containers to add custom frameworks, libraries, and packages using Docker files. Training with TensorFlow
docs.aws.amazon.com
그런데 저는 Dockerfile 내부에 AWS S3 서비스에 접근하는 명령어가 추가로 있기 때문에, 제 개인 개정으로도 로그인이 필요합니다.
이럴 땐 고민 없이 2개 계정으로 모두 로그인하면 됩니다.
그런데 안타깝게도 이렇게 하면 Credentials 에러가 발생합니다. 도커가 S3에 대한 credential 정보를 찾을 수 없기 때문인 것으로 추측됩니다. (혹시 정확한 이유를 설명해주실 수 있는 분은 댓글 부탁 드리겠습니다! 🙏)
그래서 결국 S3에서 파일을 복사해오는 것은 Dockerfile에서는 빼고 shell(powershell)에서 직접 진행하기로 합니다. 대신에 이 모든 과정을 shell에서 직접 타이핑하는 것이 귀찮으니, shell script 파일을 작성해서 실행해 보았습니다.
아주 잘 작동합니다!
S3 파일 복사해오는 것부터 이미지 빌드, ECR 푸쉬까지 한 번에 가능한 최종 스크립트 파일 구성입니다.
# Copy model files
$bucket = '{Bucket_Name}'
aws s3 cp "s3://$bucket/{File_Name}" ./{New_File_Name}
# Login to ECR & Docker
$region = 'ap-northeast-2'
$aws_ami_url = "763104351884.dkr.ecr.$region.amazonaws.com"
$aws_my_url = "{AWS_User_ID}.dkr.ecr.$region.amazonaws.com"
aws ecr get-login-password --region $region | docker login --username AWS --password-stdin $aws_ami_url
aws ecr get-login-password --region $region | docker login --username AWS --password-stdin $aws_my_url
# Build and Push the image to AWS ECR
$repo = '{Repo_Name}'
docker build -t $repo .
docker tag "$repo`:latest" "$aws_my_url/$repo`:latest"
docker push "$aws_my_url/$repo`:latest"
# Delete Downloaded files (if needed)
Remove-Item {New_File_Name}