在尝试将r-shiny应用程序泊坞化时,Docker缓存失效使我遇到了问题。更改/app
中的文件似乎仅在docker-compose build
输出中启用缓存,这根本不反映文件完全不变的容器内部。在build命令的输出下面:
Building app
Step 1/13 : FROM rocker/shiny:latestBuilding app
Step 1/13 : FROM rocker/shiny:latest
---> f184f6aaa052
Step 2/13 : RUN apt-get update && apt-get install libssl-dev -y nano libxml2-dev libsodium-dev libmariadbclient-dev libhiredis-dev
---> Using cache
---> 03a28c35ec29
Step 3/13 : COPY install_packages.R /tmp/install_packages.R
---> Using cache
---> f0f98562976d
Step 4/13 : RUN Rscript /tmp/install_packages.R
---> Using cache
---> 1f66021a00f5
Step 5/13 : RUN R -e "install.packages('devtools', dependencies = TRUE, repos = 'http://cran.rstudio.com/')"
---> Using cache
---> c65054a0b033
Step 6/13 : RUN R -e "devtools::install_github('PaulC91/shinyauthr', dependencies = TRUE)"
---> Using cache
---> 29135386a24f
Step 7/13 : ARG CACHEPOP=1
---> Using cache
---> 2916da43a88f
Step 8/13 : ADD shiny-server.conf /etc/shiny-server/shiny-server.conf
---> Using cache
---> f1289d6aba53
Step 9/13 : ADD shiny-server.sh /usr/bin/shiny-server.sh
---> Using cache
---> 87f462480ada
Step 10/13 : COPY app /srv/shiny-server/app
---> d89ae8e7fa85
Step 11/13 : RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
---> Running in 27c53685d6ad
Removing intermediate container 27c53685d6ad
---> 6cb6baf130af
Step 12/13 : EXPOSE 3838
---> Running in 4425302562bb
Removing intermediate container 4425302562bb
---> e6c4fc565195
Step 13/13 : CMD ["usr/bin/shiny-server.sh"]
---> Running in 7723d0219648
Removing intermediate container 7723d0219648
---> 742d77a0a30e
Successfully built 742d77a0a30e
Successfully tagged redis-test:latest
---> f184f6aaa052
Step 2/13 : RUN apt-get update && apt-get install libssl-dev -y nano libxml2-dev libsodium-dev libmariadbclient-dev libhiredis-dev
---> Using cache
---> 03a28c35ec29
Step 3/13 : COPY install_packages.R /tmp/install_packages.R
---> Using cache
---> f0f98562976d
Step 4/13 : RUN Rscript /tmp/install_packages.R
---> Using cache
---> 1f66021a00f5
Step 5/13 : RUN R -e "install.packages('devtools', dependencies = TRUE, repos = 'http://cran.rstudio.com/')"
---> Using cache
---> c65054a0b033
Step 6/13 : RUN R -e "devtools::install_github('PaulC91/shinyauthr', dependencies = TRUE)"
---> Using cache
---> 29135386a24f
Step 7/13 : ARG CACHEPOP=1
---> Using cache
---> 2916da43a88f
Step 8/13 : ADD shiny-server.conf /etc/shiny-server/shiny-server.conf
---> Using cache
---> f1289d6aba53
Step 9/13 : ADD shiny-server.sh /usr/bin/shiny-server.sh
---> Using cache
---> 87f462480ada
Step 10/13 : COPY app /srv/shiny-server/app
---> d89ae8e7fa85
Step 11/13 : RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
---> Running in 27c53685d6ad
Removing intermediate container 27c53685d6ad
---> 6cb6baf130af
Step 12/13 : EXPOSE 3838
---> Running in 4425302562bb
Removing intermediate container 4425302562bbBuilding app
Step 1/13 : FROM rocker/shiny:latest
---> f184f6aaa052
Step 2/13 : RUN apt-get update && apt-get install libssl-dev -y nano libxml2-dev libsodium-dev libmariadbclient-dev libhiredis-dev
---> Using cache
---> 03a28c35ec29
Step 3/13 : COPY install_packages.R /tmp/install_packages.R
---> Using cache
---> f0f98562976d
Step 4/13 : RUN Rscript /tmp/install_packages.R
---> Using cache
---> 1f66021a00f5
Step 5/13 : RUN R -e "install.packages('devtools', dependencies = TRUE, repos = 'http://cran.rstudio.com/')"
---> Using cache
---> c65054a0b033
Step 6/13 : RUN R -e "devtools::install_github('PaulC91/shinyauthr', dependencies = TRUE)"
---> Using cache
---> 29135386a24f
Step 7/13 : ARG CACHEPOP=1
---> Using cache
---> 2916da43a88f
Step 8/13 : ADD shiny-server.conf /etc/shiny-server/shiny-server.conf
---> Using cache
---> f1289d6aba53
Step 9/13 : ADD shiny-server.sh /usr/bin/shiny-server.sh
---> Using cache
---> 87f462480ada
Step 10/13 : COPY app /srv/shiny-server/app
---> d89ae8e7fa85
Step 11/13 : RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
---> Running in 27c53685d6ad
Removing intermediate container 27c53685d6ad
---> 6cb6baf130afBuilding app
Step 1/13 : FROM rocker/shiny:latest
---> f184f6aaa052
Step 2/13 : RUN apt-get update && apt-get install libssl-dev -y nano libxml2-dev libsodium-dev libmariadbclient-dev libhiredis-dev
---> Using cache
---> 03a28c35ec29
Step 3/13 : COPY install_packages.R /tmp/install_packages.R
---> Using cache
---> f0f98562976d
Step 4/13 : RUN Rscript /tmp/install_packages.R
---> Using cache
---> 1f66021a00f5
Step 5/13 : RUN R -e "install.packages('devtools', dependencies = TRUE, repos = 'http://cran.rstudio.com/')"
---> Using cache
---> c65054a0b033
Step 6/13 : RUN R -e "devtools::install_github('PaulC91/shinyauthr', dependencies = TRUE)"
---> Using cache
---> 29135386a24f
Step 7/13 : ARG CACHEPOP=1
---> Using cache
---> 2916da43a88f
Step 8/13 : ADD shiny-server.conf /etc/shiny-server/shiny-server.conf
---> Using cache
---> f1289d6aba53
Step 9/13 : ADD shiny-server.sh /usr/bin/shiny-server.sh
---> Using cache
---> 87f462480ada
Step 10/13 : COPY app /srv/shiny-server/app
---> d89ae8e7fa85
Step 11/13 : RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
---> Running in 27c53685d6ad
Removing intermediate container 27c53685d6ad
---> 6cb6baf130af
Step 12/13 : EXPOSE 3838
---> Running in 4425302562bb
Removing intermediate container 4425302562bb
---> e6c4fc565195
Step 13/13 : CMD ["usr/bin/shiny-server.sh"]
---> Running in 7723d0219648
Removing intermediate container 7723d0219648
---> 742d77a0a30e
Successfully built 742d77a0a30e
Successfully tagged redis-test:latest
Step 12/13 : EXPOSE 3838
---> Running in 4425302562bb
Removing intermediate container 4425302562bb
---> e6c4fc565195
Step 13/13 : CMD ["usr/bin/shiny-server.sh"]
---> Running in 7723d0219648
Removing intermediate container 7723d0219648
---> 742d77a0a30e
Successfully built 742d77a0a30e
Successfully tagged redis-test:latest
---> e6c4fc565195
Step 13/13 : CMD ["usr/bin/shiny-server.sh"]
---> Running in 7723d0219648
Removing intermediate container 7723d0219648
---> 742d77a0a30e
Successfully built 742d77a0a30e
Successfully tagged redis-test:latest
如您所见,在步骤 10/13 中,缓存确实无效,但这并没有反映在容器中,在该容器中我仍然看到旧文件(未添加新文件)。 ADD
和COPY
产生相同的行为。
这是正常行为还是我没有看到任何东西?
出于彻底的考虑,文件夹结构和Dockerfile:
.
├── app
│ ├── global.R
│ ├── mock_users.R
│ ├── server.R
│ ├── test.txt
│ └── ui.R
├── docker-compose.yml
├── Dockerfile
├── install_packages.R
├── readme.md
├── redis_cache_test.Rproj
├── shiny-server.conf
└── shiny-server.sh
FROM rocker/shiny:latest
# install openssl
# libsodium for shinyauthr
RUN apt-get update && apt-get install libssl-dev -y \
nano \
libxml2-dev \
libsodium-dev \
libmariadbclient-dev \
libhiredis-dev
# install packages and remove downloaded files
COPY install_packages.R /tmp/install_packages.R
RUN Rscript /tmp/install_packages.R
# install shiny authr package
RUN R -e "install.packages('devtools', dependencies = TRUE, repos = 'http://cran.rstudio.com/')"
RUN R -e "devtools::install_github('PaulC91/shinyauthr', dependencies = TRUE)"
# copy config files
ARG CACHEPOP=1
ADD shiny-server.conf /etc/shiny-server/shiny-server.conf
ADD shiny-server.sh /usr/bin/shiny-server.sh
# copy app
COPY app /srv/shiny-server/app
## allow permissions
RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
# expose port
EXPOSE 3838
CMD ["usr/bin/shiny-server.sh"]
答案 0 :(得分:0)
正如您所观察到的那样,从构建输出中,缓存确实无效,并且您使用目录的新内容构建了一个新映像。但是,根据注释,然后使用卷/app:/srv/shiny-server
运行此容器,该卷将把对/srv/shiny-server
中的容器内此目录的访问映射到/app
上的主机。请注意,此路径是绝对路径,而不是相对路径,因此文件位于主机/app
上,而不是位于./app
的构建目录中。
根据您的用例,有各种解决方案。首先,在compose文件中,您可以访问相对路径,因此,如果要在本地进行测试(装入卷的典型原因),则通常需要使用./app:/srv/shiny-server
之类的相对路径来装入卷。结果是容器使用此目录的内容运行,而无需重建映像,这通常对于更快的开发周期是首选。
如果您不希望挂载卷,例如如果这是在生产中,并且不需要从容器外部访问文件,则最好从撰写文件中删除卷挂载。
最后,如果需要外部访问这些文件,但又希望通过映像的内容对其进行更新,则通常需要将卷的内容保存在安全的地方,并使用入口点脚本。在我的docker-base repo中,我使用save-volume
和load-volume
脚本执行此操作,一个脚本在映像构建期间运行,另一个脚本在运行容器时在入口点运行。