[英]How to clone a GitHub repo inside a directory mapped as a volume using docker-compose.yml?
我有一個docker-compose.yml
類似於:
version: '3'
services:
my_service:
image: ....
container_name: ...
volumes:
- "./data:/my_service/data"
command: >
....
After running docker-compose pull
, I run git clone git@github.com:.....
, on the host machine, to clone a private GitHub repo inside the shared directory (mapped as volume). 這工作正常,但我想知道是否可以使用 Docker 處理此手動步驟。
做一些研究,我發現了一些使用Dockerfile
的例子,但如果可能的話,我寧願只使用docker-compose.yml
。
實現此目的的正確方法是編寫一個自定義 docker 文件並通過 docker 組合來解決它。
這是一個例子:
docker-compose 文件片段*
version: '3'
services:
my_service:
build:
context:
dockerfile: my_service.dockerfile
# ... continue ...
my_service.dockerfile
FROM some_image:some_tag
RUN apt update -y && \
apt install -y git
RUN cd some_folder && \
git clone some_uri
這是一個示例,它在 nginx 映像上使用自定義command
來 git 在啟動服務器之前克隆一些存儲庫:
version: '3'
services:
nginx:
image: nginx:latest
volumes:
- "./data:/my_service/data"
command: ["bash", "-c", "apt-get update; apt-get install -y git; rm -rf /usr/share/nginx/html; git clone https://github.com/cloudacademy/static-website-example /usr/share/nginx/html; nginx -g \"daemon off;\";"]
ports:
- 8080:80
它使用帶有所有適當命令的單行字符串,您需要適應您的用例。 另外,請注意,根據您使用的圖像,您可能會也可能無法添加包。
這可能有助於快速解決問題,但請參閱@Antonio Petricca 答案以獲得更強大的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.