[Docker] 처음하는 사람도 쉽게 Docker file 만들고 build 해보기

2020. 5. 9. 12:39개발/Docker

728x90

최근에 docker로 image를 만들 일이 있어서 해보고 있는 중이다.

docker에 대한 기초적인 것도 모르기 때문에 경험 위주로 하였으니, 다른 글도 참고 바람.

build

  •  options
    • --no-cache : 이전에 했던 캐쉬를 제거
    • -t : tag 달기
    • -f : Docker file name
docker build --no-cache -t test/test:latest -t test/test:1.0.0 -f ./Dockerfile

 

DockerFile은 anaconda를 만드는 것을 복붙 해왔다.

FROM ubuntu:18.04

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH /opt/conda/bin:$PATH

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 \
    git mercurial subversion

RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh -O ~/anaconda.sh && \
    /bin/bash ~/anaconda.sh -b -p /opt/conda && \
    rm ~/anaconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate base" >> ~/.bashrc

RUN apt-get install -y curl grep sed dpkg && \
    TINI_VERSION=`curl https://github.com/krallin/tini/releases/latest | grep -o "/v.*\"" | sed 's:^..\(.*\).$:\1:'` && \
    curl -L "https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini_${TINI_VERSION}.deb" > tini.deb && \
    dpkg -i tini.deb && \
    rm tini.deb && \
    apt-get clean

ENTRYPOINT [ "/usr/bin/tini", "--" ]
CMD [ "/bin/bash" ]

 

이제 여기다가 R 4.0을 추가해보자.

아래처럼 R 부분에 대해서 RUN을 해서 추가해주면 된다.

RUN , ENV 같은 것들이 있는데 이것들에 대한 설명은 다른 글에서 잘 정리를 해줬으니 참고하시면 될 것 같다.

자주 쓰는 Dockerfile instruction들

FROM ubuntu:18.04

ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH /opt/conda/bin:$PATH

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 \
    git mercurial subversion \
    sudo wget vim net-tools \
    gnupg gnupg2 gnupg1 \
    gdebi-core libssl1.0.0 libssl-dev \

RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh -O ~/anaconda.sh && \
    /bin/bash ~/anaconda.sh -b -p /opt/conda && \
    rm ~/anaconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate base" >> ~/.bashrc

########### add
## R 4.0 install
RUN echo "deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/" | tee -a /etc/apt/sources.list
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
RUN apt-get update
RUN apt-get install -y r-base
RUN apt-get install -y gdebi-core libssl1.0.0 libssl-dev
## rstudio install
RUN wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.2.5042-amd64.deb
RUN sudo gdebi --n rstudio-server-1.2.5042-amd64.deb
########### add

RUN apt-get install -y curl grep sed dpkg && \
    TINI_VERSION=`curl https://github.com/krallin/tini/releases/latest | grep -o "/v.*\"" | sed 's:^..\(.*\).$:\1:'` && \
    curl -L "https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini_${TINI_VERSION}.deb" > tini.deb && \
    dpkg -i tini.deb && \
    rm tini.deb && \
    apt-get clean



ENTRYPOINT [ "/usr/bin/tini", "--" ]
CMD [ "/bin/bash" ]

이제 Dockerfile을 만들었으니, build를 해보자

 

## 기존에 했던 것을 지우고 처음부터 시작하고 싶은 경우
docker build --no-cache -t test/test:latest -t test/test:1.0.0 -f ./Dockerfile
## 오류가 나서 Dockerfile 수정 후 계속 진행하고 싶은 경우
docker build -t test/test:latest -t test/test:1.0.0 -f ./Dockerfile

 

build가 안전회 image가 된다는 것이다. 

그래서 docker run에서 cmd를 치기 위해서는 아래와 같이 치면 된다.

docker run --rm -it test/test:1.0.0 /bin/bash

 

 

 

https://stackoverflow.com/questions/35594987/how-to-force-docker-for-a-clean-build-of-an-image

 

How to force Docker for a clean build of an image

I have build a Docker image from a Docker file using the below command. $ docker build -t u12_core -f u12_core . When I am trying to rebuild it with the same command, it's using the build cache l...

stackoverflow.com

docker build option site

 

docker build

Description Build an image from a Dockerfile Usage docker build [OPTIONS] PATH | URL | - Extended description The docker build command builds Docker images from a Dockerfile and a...

docs.docker.com

https://hub.docker.com/r/continuumio/anaconda3/dockerfile

 

Docker Hub

 

hub.docker.com

 

728x90