et al/Docker&git

[Docker] Dockerfile 작성 및 이미지로 build 하는 법

코딩상륙작전 2024. 1. 12. 21:04

* background(문제 상황)

우분투 20.04 LTS를 사용중이고, 

로컬에서 mmdetection을 돌려보기 위해서 dockerfile을 찾던 중에 dockerfile을 작성하는 법과 이미지로 빌드하는 법을 기록해둔다.

우선 환경에 맞는 pytorch와 cuda, cudnn을 알아야 한다.

환경세팅은 이전에 작성한 글을 참고하길 바람.

2023.10.19 - [et al/Docker&git] - [CS] ubuntu 20.04.6 [LTS]에 NVIDIA - DRIVER 설치

2023.10.19 - [et al/Docker&git] - [CS] 4060ti 16GB - cuda & pytorch & tensorflow & cuDNN 호환 버전 확인

2023.10.19 - [et al/Docker&git] - [Docker] 도커에 cuda & pytorch & tensorflow & cuDNN 설치

2023.10.22 - [et al/Docker&git] - [Docker] pytorch 딥러닝 환경 구축하기

 

* dockerfile 다운받기

mmdetection 공홈에는 dockerfile도 공유하고 있다. 

https://github.com/open-mmlab/mmdetection/blob/main/docker/Dockerfile

 

ARG PYTORCH="2.1.2"
ARG CUDA="11.8"
ARG CUDNN="8"

FROM pytorch/pytorch:${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-runtime

ENV TORCH_CUDA_ARCH_LIST="6.0 6.1 7.0 7.5 8.0 8.6+PTX" \
    TORCH_NVCC_FLAGS="-Xfatbin -compress-all" \
    CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" \
    FORCE_CUDA="1"

# Avoid Public GPG key error
# https://github.com/NVIDIA/nvidia-docker/issues/1631
#RUN rm /etc/apt/sources.list.d/cuda.list \
#    && rm /etc/apt/sources.list.d/nvidia-ml.list \
#    && apt-key del 7fa2af80 \
#    && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub \
#    && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/7fa2af80.pub

# (Optional, use Mirror to speed up downloads)
# RUN sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu\//http:\/\/mirrors.aliyun.com\/ubuntu\//g' /etc/apt/sources.list && \
#    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# Install the required packages
RUN apt-get update
#    && apt-get install -y ffmpeg libsm6 libxext6 git ninja-build libglib2.0-0 libsm6 libxrender-dev libxext6 \
RUN apt-get clean
#    && rm -rf /var/lib/apt/lists/*

# Install MMEngine and MMCV
RUN pip install -U openmim && \
    mim install "mmengine>=0.7.1" "mmcv>=2.0.0rc4"

# Install MMDetection
# RUN conda clean --all \
#    && git clone https://github.com/open-mmlab/mmdetection.git /mmdetection \
#    && cd /mmdetection \
#    && pip install -v -e

# for ImportError: libGL.so.1 
RUN apt-get update
RUN apt-get -y install libgl1-mesa-glx
WORKDIR /mmdetection

해당 파일을 다운받고 다음과 같이 고쳤다.

 

docker에서는 opencv import 에러가 나는데, 밑에서 RUN 두 줄은 에러를 해결하기 위해 추가한 것이고 아래 한 줄은 build후 나중에 적용하면 될 듯하다.

apt-get install libglib2.0-0

 

* Dockerfile 이미지로 빌드하기

중요한 건 Dockerfile의 이름이 중요하다.

Dockerfile의 이름은 꼭 Dockerfile 이어야 하고, 확장자도 있어선 안 된다. 처음에 Dockerfile.txt였어서 빌드가 안 되길래 이유를 찾는데 고생을 조금 했다.

 

https://mmdetection.readthedocs.io/en/latest/get_started.html

 

* wget 설치

apt-get install wget

.gz 파일 압축 해제 

tar -xf data.tar.gz

* 깃 설치

apt install git
git clone <깃허브 주소> 
# git remote add origin <깃허브 주소>