0

I run PHP-apache as a Docker container and I need to use Rsync inside of a PHP script - in exec() function.

I can't make it work with the 'default' Rsync - I mean, the one installed in the OS. I'm getting sh: 1: rsync: not found. Same with pretty much any other command, except ssh, ls, id - these work ok.

So I installed Rsync inside of the Docker PHP container, here's my Dockerfile:

FROM php:7.4-apache
LABEL maintainer="dev@chialab.io"

# Download script to install PHP extensions and dependencies
ADD https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions /usr/local/bin/

RUN chmod uga+x /usr/local/bin/install-php-extensions && sync

RUN chmod go+x /usr/local/bin
RUN apt-get update
RUN apt-get -y install rsync

RUN DEBIAN_FRONTEND=noninteractive apt-get update -q \
    && DEBIAN_FRONTEND=noninteractive apt-get install -qq -y \
      curl \
      git \
      zip unzip \
    && install-php-extensions \
      mysqli \
      pdo_mysql \
      zip \
    && a2enmod rewrite

It builds fine, the PHP container runs ok, everything's good, BUT I still can't use Rsync in PHP's exec() function. Am I missing something?

Interesting enough, the container's rsync runs ok in terminal: $ docker exec php rsync ...

P.S. I'm still learning Docker, and I've been fighting with this nightmare for over a week now, and fixed a number of other issues along the way - and still no success. It took me a while before I fixed sh: 1: rsync: permission denied with RUN chmod go+x /usr/local/bin or made the rsync installed in container - it couldn't install at the beginning, but I fixed it by adding RUN apt-get update right before the RUN apt-get -y install rsync.

Sam Zadworny
  • 23
  • 3
  • 12
  • The problem isn't connected with docker. When you run `docker exec php rsync`, the rsync is run by `root` user wich have permissions, but when you run by PHP's exec() function, the rsync is run by `www-data` user. Maybe `www-data` user doesn't have permissions to execute the `rsync` binary or the folder wich you want to sync is not available for `www-data` – Artem Apr 21 '21 at 18:59
  • Yeah, I'm aware of that: root vs. www-data owner. I played with dirs/files ownership, even added www-data to sudoers file, but again - no success. For test purposes I run just `exec("rsync --version");` so nothing to do with folders to sync in this case. Thanks. – Sam Zadworny Apr 21 '21 at 19:10
  • `exec("rsync --version");` works? – Artem Apr 21 '21 at 19:26
  • No, I'm still getting `sh: 1: rsync: not found`. As I mentioned before, only few simple commands work (ls, id, etc.) but pretty much everything else outputs 'sh: 1: ......: not found'. – Sam Zadworny Apr 21 '21 at 19:36
  • What a response of a command `$ docker exec php whereis rsync` ? – Artem Apr 21 '21 at 19:46
  • `rsync: /usr/bin/rsync` - that's on my Mac. Tomorrow I can test it on Raspberry PI, but on RPI I get exactly the same problem: not found. – Sam Zadworny Apr 21 '21 at 19:52
  • Could you try running `exec("/usr/bin/rsync --version");` ? – Artem Apr 21 '21 at 19:58
  • Sounds like `$PATH` is not set in the environment, and/or it does not include the path where rsync is installed to. The simplest solution would be to use the full path to the rsync binary. – Sammitch Apr 21 '21 at 20:01
  • Wow, the `exec("/usr/bin/rsync --version");` works! I tried the full path before too, but that's with the 'default' rsync installed in the OS, not docker. I missed it somehow, and didn't test the full path after installed rsync in the docker container! Let me play a bit more and try to actually transfer files... – Sam Zadworny Apr 21 '21 at 20:36
  • Rsync works now. I've tested it locally as I'm having trouble with ssh now when trying to download from remote server: `Load key "id_rsa": Operation not permitted`. I'm running the following command: `exec('rsync -avze "ssh -i id_rsa" "user@111.111.111.111:public_html/tocopy/" localdir')` – Sam Zadworny Apr 21 '21 at 20:54
  • Ok, the SSH issue solved. All works as expected! A BIG thank you to @Artem! – Sam Zadworny Apr 22 '21 at 09:33

1 Answers1

-2

Hi To sync your local files, you need to use the following instructions, which are referenced by the official docker community.

  1. You need to choose which sync you want to set up, single or double, in the additional docker-sync.yml file. https://docker-sync.readthedocs.io/en/latest/

or

  1. Just use docker-compose.yml with volumes: param.
    https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes
  • Thanks for that, but I want to transfer from/to a remote server. So it's a combination of rsync+ssh. I made ssh work, but I can't make the rsync work. Sorry I didn't write that at the beginning. – Sam Zadworny Apr 21 '21 at 19:42