[miscellaneous] docker usage

기타 유용한 Docker 명령어에 대해 작성합니다.

Make Dockerfile

  • Get docker image
    • docker pull nvidia/cuda:11.6.1-devel-ubuntu20.04
  • Dockerfile
    • 반드시 파일의 제목은 Dockerfile 이어야 한다. (확장자는 없음.)

Example Dockerfile

# 기존 이미지를 기반으로 사용
# Use nvidia/cuda version matches your server
FROM nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04

# 필요한 패키지 설치
# Install ubuntu apt packages. Do not remove default packages.
RUN apt-get update
# opencv-python error
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install\
    libgl1\
    libgl1-mesa-glx \ 
    libglib2.0-0 -y && \
    rm -rf /var/lib/apt/lists/*
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    wget \
    apt-utils \
    build-essential \
    ca-certificates \
    curl \
    git \
    htop \
    sudo \
    vim \
    python3-dev \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Miniconda 설치
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /miniconda.sh
RUN bash /miniconda.sh -b -p /miniconda
ENV PATH="/miniconda/bin:${PATH}"

# Conda를 이용해 Python3 설치 및 Python 패키지 설치
RUN conda install -y python=3.9 && \
    pip3 --no-cache-dir install --upgrade \
    pip \
    setuptools \
    ipython \
    ipdb \
    matplotlib \
    pandas \
    scipy \
    torch \
    jupyter \
    torchvision \
    torchtext \
    torchsummary \
    slacker \
    tqdm

# # 사용자 설정
# ARG UNAME
# ARG UID
# ARG GID

# # Ensure that the group and user are created successfully
# RUN if [ -z "$UNAME" ] || [ -z "$UID" ] || [ -z "$GID" ]; then echo "UNAME, UID, and GID arguments are required" && exit 1; fi && \
#     addgroup --gid ${GID} ${UNAME} && \
#     useradd -m -u ${UID} -g ${GID} -s /bin/bash ${UNAME} && \
#     adduser ${UNAME} sudo

# USER ${UNAME}
# WORKDIR /home/${UNAME}

CMD [ "/bin/bash" ]
Build Log
joonhyung@devbox:~/dockers/joonh_cu116$ docker build -t joonh_cu116 .
[+] Building 260.0s (11/11) FINISHED                                                                                                                                                    
 => [internal] load .dockerignore                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                    0.0s
 => [internal] load build definition from Dockerfile                                                                                                                               0.0s
 => => transferring dockerfile: 1.72kB                                                                                                                                             0.0s
 => [internal] load metadata for docker.io/nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04                                                                                             0.0s
 => [1/7] FROM docker.io/nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04                                                                                                               0.0s
 => CACHED [2/7] RUN apt-get update                                                                                                                                                0.0s
 => [3/7] RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install    libgl1    libgl1-mesa-glx     libglib2.0-0 -y &&     rm -rf /var/lib/apt/lists/*                19.2s
 => [4/7] RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y     wget     apt-utils     build-essential     ca-certificates     curl     git     htop     s  24.1s
 => [5/7] RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /miniconda.sh                                                                          3.0s
 => [6/7] RUN bash /miniconda.sh -b -p /miniconda                                                                                                                                  6.4s
 => [7/7] RUN conda install -y python=3.9 &&     pip3 --no-cache-dir install --upgrade     pip     setuptools     ipython     ipdb     matplotlib     pandas     scipy     torc  177.4s
 => exporting to image                                                                                                                                                            29.8s
 => => exporting layers                                                                                                                                                           29.8s
 => => writing image sha256:48e3f2be75424e71be48c75d5472c90a9bf5ac9c94586bd723127d5bed67714b                                                                                       0.0s 
 => => naming to docker.io/library/joonh_cu116

Build docker image

docker build -t [이미지 이름:이미지 버전] [Dockerfile의 경로]
docker build -t joonh_cu116 .
docker build --build-arg UNAME=joonhyung-lee --build-arg UID=1001 --build-arg GID=1001 -t joonh_cu116 .

Get built docker images

docker images
>>> joonh_cu116    latest    7c1bae5ab933   33 seconds ago   9.56GB

Start Docker Container

docker run -v [로컬_경로]:[컨테이너_경로] -it --gpus all [이미지_이름]:[태그] /bin/bash
docker run -v /home/joonhyung/python/:/home/root/python -it --gpus all joonh_cu116:latest /bin/bash

Commit and Push Docker Container

get information about container

joonhyung@devbox:~/dockers/joonh_cu116$ docker ps
CONTAINER ID   IMAGE                              COMMAND                  CREATED             STATUS             PORTS                                       NAMES
79b602e29923   c2e91d98d3ed                       "/opt/nvidia/nvidia_…"   About an hour ago   Up About an hour                                               magical_neumann

Commit

docker commit CONTAINER IMAGE_NAME
joonhyung@devbox:~/dockers/joonh_cu116$ docker commit 79b602e29923 joonh_cu116:v00
sha256:f50aabee25697dc96bc80f76fd231c74d1fbfe77cb47698a4ec89e0b84c5ba81
  • docker images로 image가 적절하게 생성되었는지 확인 가능.
  joonhyung@devbox:~/dockers/joonh_cu116$ docker images
  REPOSITORY                                                  TAG                               IMAGE ID       CREATED          SIZE
  joonh_cu116                                                 v00                               f50aabee2569   14 seconds ago   31GB

Build Dockerfile

  • Add lines
    • FROM joonh_cu116:v00
      # Install ubuntu apt packages.
      RUN sudo apt update && sudo apt install -y <ubuntu-packages>
      
  • Build New version
    • docker build -t joonh_cu116:v01 .
      

Login Docker server

  joonhyung@devbox:~/dockers/joonh_cu116_v00$ docker login -u joonhyunglee
  Password: 
  WARNING! Your password will be stored unencrypted in /home/joonhyung/.docker/config.json.
  Configure a credential helper to remove this warning. See
  https://docs.docker.com/engine/reference/commandline/login/#credentials-store

  Login Succeeded

push on Docker

joonhyung@devbox:~/dockers/joonh_cu116_v00$ docker push joonhyunglee/joonh_cu116:v01



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • [miscellaneous] pyenv usage
  • [구현] AprilTag Pose Estimation
  • [구현] MuJoCo 기초 사용법
  • [paper-review] Dream2Real: Zero-Shot 3D Object Rearrangement with Vision-Language Models
  • [study] Vector Quantization