2 min read

docker构建容器

之前看过一个系列的docker视频,不过长久没有用,有点忘记了,重新构建一个简单的docker image进行说明。 教程:链接

  1. 首先新建一个project文件夹,然后创建一个Dockerfile文件,以rocker/rstudio为例
# Dockerfile
FROM rocker/rstudio:4.2.1 # 从dockerhub上面来继续build
RUN /rocker_scripts/install_tidyverse.sh
VOLUME \
    /mnt/  # 最好把docker的mnt文件暴露出来,这样可以和本地的volumn

其他一些在rocker上面有的装包的script

# rocker这个R studio images: https://github.com/rocker-org/rocker-versioned2
# rocker scripts (主要的包): https://github.com/rocker-org/rocker-versioned2/tree/master/scripts
# xcms: https://hub.docker.com/r/yufree/xcmsrocker
  1. cd到相关的目录下,创建image
docker image build -t rstudiotest .
  1. 然后run这个build好的docker image
docker run --rm -ti -e PASSWORD="123456" -p 8787:8787 rstudiotest
  1. 然后可以用浏览器登录到这个docker镜像 http://localhost:8787/,即可正常使用

  1. 关闭这个container
docker ps
docker stop 0ba6125a6a85
  1. 装一些其他的R包,这里先重新创建一个Dockerfile2,基于上面build好的image继续进行build 逻辑:在目录里创建了一个packages,然后构建的时候copy到docker中,然后用R运行这个脚本进行安装
  • packages里面的install.r内容:
#options(repos="https://mirrors.tongji.edu.cn/CRAN/")
options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))
install.packages("BiocManager")
BiocManager::install()
pkgs <- c('crayon', 'readxl', 'pbapply')

# pkgs <- c('dplyr', 'tidyr', 'pryr', 'knitr', 'igraph', 'Rdisop', 'xcms', 'CAMERA', 'Cairo', 'baseline', 'stringr', 'tibble', 'purrr', 'caret', 'rmarkdown', 'magrittr', 'randomForest', 'fastcluster', 'missForest', 'scatterplot3d', 'pheatmap', 'beeswarm', 'ellipse', 'pls', 'pROC', 'e1071', 'readr', 'readxl', 'signal', 'ggrepel', 'xml2', 'pbapply', 'devtools', 'opentimsr','ggpmisc','smoother','splus2R', 'gsubfn', 'nnls', 'ggsci', 'enviPat', 'ggplot2', 'ggpubr', 'fastmatch')
BiocManager::install(pkgs, ask=FALSE)
# install.packages('https://cran.r-project.org/src/contrib/Archive/plsdepot/plsdepot_0.1.17.tar.gz')
# install local packages
install.packages("/MetDNA2_1.0.09.tar.gz", repos=NULL)
  • Dockerfile2
# Dockerfile2
FROM rstudiotest
COPY ./packages/ /packages/
RUN \ 
    R --no-save -q -f /packages/install.r \
    && rm -rf /packages
  • build rstudiotest2 image
# -f 来指定用哪个Dockerfile
docker image build -f .\Dockerfile2 -t rstudiotest2 .
  • run docker container
docker run --rm -ti -e PASSWORD="123456" -p 8787:8787 rstudiotest
  1. run container时,将本地的01_MIDNet/pacakges文件夹与data文件夹绑定,用于数据处理
# 将本地01_MIDNet/pacakges文件夹与data文件夹绑定,用于数据处理
docker run --rm -ti -e PASSWORD="123456" -p 8787:8787 -v D:\04_vs_codes\docker\01_MIDNet\packages:/data rstudiotest
  1. 由于xcms里面有很多依赖的环境,这边直接使用如下的Dockerfile
FROM rocker/rstudio:4.2.1 AS rstudiotest
RUN /rocker_scripts/install_tidyverse.sh

FROM rstudiotest AS rstudio-zhiwei
COPY ./packages/ /packages/
RUN \ 
    apt update \
    && apt install -y cmake pandoc libcairo2-dev libxt-dev liblzma-dev libbz2-dev libxml2-dev libssl-dev libnetcdf-dev libcurl4-openssl-dev libxt6 libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev libglpk-dev libfribidi-dev libharfbuzz-dev \
    && apt clean 
VOLUME \
    /mnt/


FROM rstudiotest AS rstudio-zhiwei
COPY ./packages/ /packages/
RUN \ 
    apt update \
    && apt install -y cmake pandoc libcairo2-dev libxt-dev liblzma-dev libbz2-dev libxml2-dev libssl-dev libnetcdf-dev libcurl4-openssl-dev libxt6 libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev libglpk-dev libfribidi-dev libharfbuzz-dev \
    && apt clean 
VOLUME \
    /mnt/


FROM rstudio-zhiwei AS rstudiotest3
COPY ./packages/ /packages/
RUN \ 
    R --no-save -q -f /packages/install2.r
    # && rm -rf /packages
VOLUME \
    /mnt/