Home > Enterprise >  how to copy files and folders from docker context to images?
how to copy files and folders from docker context to images?

Time:06-22

I have a rocker/tidyverse:4.2.0 image which I'm using to create an image for myself. I need folders and files, but it's not showing up in the home folder. What am I doing wrong?

FROM rocker/tidyverse:4.2.0

RUN mkdir -p $HOME/rstudio/R_scripts
WORKDIR $HOME/rstudio/R_scripts
COPY ./R_scripts/* $HOME/rstudio/R_scripts/
COPY ./R_scripts/.Rprofile $HOME/rstudio/.Rprofile
RUN ls -l $HOME/rstudio

And this is how I run it.

docker run -it --rm -d -p 8787:8787 -e PASSWORD=rstudio --name rstudio-server -v /mnt/c/Users/test/sources:/home/rstudio/repos --net=host rstudio-server:4.2.0

When I check in the home folder, I can't find the folders I copied. R_scripts folder is in the same folder which contains Dockerfile

CodePudding user response:

Docker images tend to not have "users" or "home directories" in a way you might think about them in a typical Linux system. This also means environment variables like $HOME often just aren't defined.

This means that when you try to set the current container directory

WORKDIR $HOME/rstudio/R_scripts

since $HOME is empty, the files just end up in a /rstudio directory in the root of the container filesystem. (And this might be okay!)

Style-wise, it's worth remembering that the right-hand side of COPY can be a relative path relative to the current WORKDIR, and that WORKDIR and COPY will create directories if they don't already exist. This means you don't usually need to RUN mkdir, and you don't usually need to repeat the full container path. Here I might write

FROM rocker/tidyverse:4.2.0

WORKDIR /rstudio/R_scripts      # without $HOME, creates the directory
COPY ./R_scripts/* ./           # ./ is WORKDIR
COPY ./R_scripts/.Rprofile ../  # ../ is WORKDIR's parent
# RUN ls -l /rstudio            # invisible using BuildKit backend by default
  • Related