使用ggsave()和Docker生成png文件

z31licg0  于 2022-12-18  发布在  Docker
关注(0)|答案(1)|浏览(177)

作为Docker的第一次尝试,我正在尝试运行一些R代码从Docker容器生成PNG文件。我在Windows 11中使用WSL 2,并且只使用Docker CLI,而不是Docker桌面。

停靠文件

FROM r-base

# Modify the date at build time
ARG WHEN

# Execute R from the terminal
RUN R -e "options(repos =  \
   list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}')); \
   install.packages('ggplot2')"

# Create a path in the container
RUN mkdir -p /home/
RUN mkdir /home/code
RUN mkdir /home/results

# Copy the file to a path in the container
COPY ./Code/iris.R /home/code/iris.R

WORKDIR /home/code

# Run iris.R in the container
CMD ["Rscript", "iris.R"]

# Copy iris.png
COPY ./iris.png /home/results/iris.png

代码/虹膜R

library(ggplot2)

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                 color = Species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  xlab("Sepal Length") +
  ylab("Sepal Width") +
  scale_x_continuous(limits = c(4, 8), breaks = seq(4, 8, 0.5)) +
  scale_y_continuous(limits = c(2, 5), breaks = 2:5)

ggsave(filename = file.path(getwd(), "iris.png"), plot = p, device = "png")

在Docker CLI中复制问题

$ docker build --build-arg WHEN=2022-12-10 -t plot .
Sending build context to Docker daemon  4.608kB
Step 1/10 : FROM r-base
 ---> 3de1ef2039fb
Step 2/10 : ARG WHEN
 ---> Using cache
 ---> ff24cab2714d
Step 3/10 : RUN R -e "options(repos =     list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}'));    install.packages('ggplot2')"
 ---> Using cache
 ---> 67b96e10f028
Step 4/10 : RUN mkdir -p /home/
 ---> Using cache
 ---> 65e100a0ca92
Step 5/10 : RUN mkdir /home/code
 ---> Using cache
 ---> 08506b7b2d44
Step 6/10 : RUN mkdir /home/results
 ---> Using cache
 ---> 149966cff268
Step 7/10 : COPY ./Code/iris.R /home/code/iris.R
 ---> Using cache
 ---> 5bba4c93e928
Step 8/10 : WORKDIR /home/code
 ---> Using cache
 ---> 1b5a8b7ee36b
Step 9/10 : CMD ["Rscript", "iris.R"]
 ---> Using cache
 ---> 8c9cc56e90f4
Step 10/10 : COPY ./iris.png /home/results/iris.png
COPY failed: file not found in build context or excluded by .dockerignore: stat iris.png: file does not exist


我还尝试注解掉最后一行CMD和最后一行COPY,并自己运行Rscript iris.R

$ docker build --build-arg WHEN=2022-12-10 -t plot .
Sending build context to Docker daemon  4.608kB
Step 1/8 : FROM r-base
 ---> 3de1ef2039fb
Step 2/8 : ARG WHEN
 ---> Using cache
 ---> ff24cab2714d
Step 3/8 : RUN R -e "options(repos =     list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}'));    install.packages('ggplot2')"
 ---> Using cache
 ---> 67b96e10f028
Step 4/8 : RUN mkdir -p /home/
 ---> Using cache
 ---> 65e100a0ca92
Step 5/8 : RUN mkdir /home/code
 ---> Using cache
 ---> 08506b7b2d44
Step 6/8 : RUN mkdir /home/results
 ---> Using cache
 ---> 149966cff268
Step 7/8 : COPY ./Code/iris.R /home/code/iris.R
 ---> Using cache
 ---> 5bba4c93e928
Step 8/8 : WORKDIR /home/code
 ---> Using cache
 ---> 1b5a8b7ee36b
Successfully built 1b5a8b7ee36b
Successfully tagged plot:latest
$ docker run -d plot tail -f /dev/null
dd2395f02f9793f1f4fca53eab904c33f38e9ee0dd3b14bcbdd56cd96693e150
$ docker exec -it dd2395f02f97 bash
root@dd2395f02f97:/home/code# ls
iris.R
root@dd2395f02f97:/home/code# Rscript iris.R
Saving 7 x 7 in image
`geom_smooth()` using formula = 'y ~ x'
root@dd2395f02f97:/home/code# ls
iris.png  iris.R

并且 * 在这里 * 它出现了,但是当我尝试自动化这个过程时,它没有出现,如上所述。
我也试过添加--no-cache,但这并没有解决这个问题。我不清楚这个问题是ggsave()的还是Docker的。

hivapdat

hivapdat1#

多亏了Flick先生的评论,我想出了解决问题的办法。
这里要理解的关键是输出需要存储在容器之外的本地驱动器上,理解MakeFile中任何语句引用的当前目录.都将引用挂载目录也很重要。

停靠文件

FROM r-base

# Set an environment variable
ENV MAINDIR /home

# Modify the date at build time
ARG WHEN

# Execute R from the terminal
RUN R -e "options(repos =  \
   list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}')); \
   install.packages('ggplot2')"

# Create a path in the container
RUN mkdir -p ${MAINDIR}

# Copy the file to a path in the container
COPY iris.R ${MAINDIR}

WORKDIR ${MAINDIR}

# Run iris.R in the container
CMD ["Rscript", "iris.R"]

值得注意的是,COPY iris.R语句将在将要挂载的目录中查找iris.R

[您想要输出的位置]/iris.R

library(ggplot2)

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                 color = Species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  xlab("Sepal Length") +
  ylab("Sepal Width") +
  scale_x_continuous(limits = c(4, 8), breaks = seq(4, 8, 0.5)) +
  scale_y_continuous(limits = c(2, 5), breaks = 2:5)

ggsave(filename = "iris.png", plot = p, device = "png")

坞站CLI

$ docker build --build-arg WHEN=2022-12-10 -t plot .
Sending build context to Docker daemon  70.14kB
Step 1/8 : FROM r-base
 ---> 3de1ef2039fb
Step 2/8 : ENV MAINDIR /home
 ---> Using cache
 ---> b8b3616ad975
Step 3/8 : ARG WHEN
 ---> Using cache
 ---> f5604ee4de70
Step 4/8 : RUN R -e "options(repos =     list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}'));    install.packages('ggplot2')"
 ---> Using cache
 ---> 8930de4d0a49
Step 5/8 : RUN mkdir -p ${MAINDIR}
 ---> Using cache
 ---> 5aa70002ed21
Step 6/8 : COPY /Code/iris.R ${MAINDIR}
 ---> 989fdac672b7
Step 7/8 : WORKDIR ${MAINDIR}
 ---> Running in 708bb8d8b42d
Removing intermediate container 708bb8d8b42d
 ---> 5edaa2e558a1
Step 8/8 : CMD ["Rscript", "iris.R"]
 ---> Running in 2d800c986828
Removing intermediate container 2d800c986828
 ---> dc23f821908a
Successfully built dc23f821908a
Successfully tagged plot:latest


现在是关键步骤:

$ docker run -v [where you want the output]:/home/ plot
Saving 7 x 7 in image
`geom_smooth()` using formula = 'y ~ x'

在该目录中执行ls会产生

$ ls
iris.R  iris.png

这可以根据需要用更复杂的文件夹结构来概括。

相关问题