简体   繁体   English

在docker compose构建期间运行git clone和rsync bash脚本

[英]Running a git clone and rsync bash script during a docker compose build

docker-compose: 泊坞窗-撰写:

version: '3'
services:
  php:
    build:
      context: ./
      dockerfile: ./Dockerfile
    volumes:
      - ./wordpress:/var/www/html:rw
    restart: always

Dockerfile: Dockerfile:

FROM php:7-fpm-alpine

RUN apk add --quiet --no-cache bash git rsync

COPY ./scripts/php/install-wordpress.sh /install-wordpress.sh
RUN chmod +x /install-wordpress.sh \
  && sh /install-wordpress.sh

install-wordpress.sh: install-wordpress.sh:

#!/bin/bash

set -ex

git clone --depth=1 --no-tags https://github.com/roots/bedrock.git /tmp/bedrock

rsync -rp /tmp/bedrock/ /var/www/html/

exec "$@"

When I run docker-compose build php ; 当我运行docker-compose build php ; docker-compose up -d ; docker-compose up -d ; then docker-compose exec php /bin/bash and ls in /var/www/html/ the directory is empty however bedrock has been cloned into tmp 然后docker-compose exec php /bin/bash/var/www/html/ls目录为空,但是bedrock已克隆到tmp

The Docker image should work as it supposed to work if you run it without docker-compose. 如果在没有docker-compose的情况下运行Docker映像,则该映像应该可以正常工作。 try to run 试着跑

 docker run --rm --name testc -it your_image bash -c "ls /var/www/html/"

This issue is with volumes: it hides everything from Docker image. 这个问题与volumes:它隐藏了Docker映像中的所有内容。 remove the 去除

volumes:
      - ./wordpress:/var/www/html:rw

And it should work fine. 它应该工作正常。

version: '3'
services:
  php:
    build:
      context: ./
      dockerfile: ./Dockerfile
      restart: always

update: 更新:

You can not view the files even if you mount the empty directory of the host, the reason is you cloned repo at build, not at run time. 即使挂载了主机的空目录,也无法查看文件,原因是您在构建而不是在运行时克隆了仓库。

To view files in host without exec in Docker you must clone at run time and you should be moved you script from to entry point. 要在Docker中没有exec的主机中查看文件,您必须在运行时进行克隆,并且应该将脚本从移至入口点。 you current script install-wordpress.sh is not entrypoint it's just like other RUN commands of the Dockerfile. 您当前的脚本install-wordpress.sh不是入口点,就像Dockerfile的其他RUN命令一样。

FROM php:7-fpm-alpine
RUN apk add --quiet --no-cache bash git rsync
COPY install-wordpress.sh /install-wordpress.sh
RUN chmod +x /install-wordpress.sh 
entrypoint ["/install-wordpress.sh"]
CMD ["php-fpm"]

entrypoint.sh entrypoint.sh

#!/bin/bash

set -ex

git clone --depth=1 --no-tags https://github.com/roots/bedrock.git /tmp/bedrock

rsync -rp /tmp/bedrock/ /var/www/html/

exec "$@"

so now if you run 所以现在如果你跑步

docker run --rm --name testc -v $PWD/:/var/www/html/ -it your_image 

It should work fine and you will able to see files wordpress files also clone files and folder as well. 它应该可以正常工作,您将能够看到文件wordpress文件以及克隆文件和文件夹。

version: '3'
services:
  php:
    build:
      context: ./
      dockerfile: ./Dockerfile
    volumes:
      - ./wordpress:/var/www/html:rw
    restart: always

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM