dockerdocker-composedockerfilewildcardln

Dockerfile symbolic link with wildcard (*) not working


I'm new to docker and I'm having problem creating a symbolic link with the following RUN command:

FROM php:7.3-apache

RUN ["/bin/bash", "-c", "ln -s /app/frontend/web/* /var/www/html"] && \
    ln -s /app/backend/web /var/www/html/admin

Here is the output when doing "ls -la /var/www/html" inside the container:

'*' -> '/app/frontend/web/*'
admin -> /app/backend/web

I'm expecting the following output:

css -> /app/frontend/web/css
index.php -> /app/frontend/web/index.php
admin -> /app/backend/web

I'm getting the correct output when creating the symlink directly in the container but for some reason the wildcard (*) does not work when building the image.

If I'm replacing the wildcard by a specific file, the symlink is creating correctly at build time. This is working but I'd like to avoid listing every files from the folder:

FROM php:7.3-apache

RUN ["/bin/bash", "-c", "ln -s /app/frontend/web/index.php /var/www/html"]

Anyone have a clue on how to do that, I've been searching for a while now and tried different syntax of that RUN command with no success.

EDIT: The symbolic link to my backend works as expected. I also should have noted that I'm using docker-compose and a volume containing my webapp is mounted to the /app folder.

Thanks


Solution

  • When * does not match any files, it is passed as is to your command:

    $ ls -l
    total 0
    -rw-r--r-- 1 leodag leodag 0 jun 17 03:29 a
    -rw-r--r-- 1 leodag leodag 0 jun 17 03:29 b
    -rw-r--r-- 1 leodag leodag 0 jun 17 03:29 c
    
    $ ls d*
    ls: cannot access 'd*': No such file or directory
    

    So your problem is that there is nothing inside /app/frontend/web/, since you still haven't copied the files - you're trying to run it right after the FROM. First you need to copy in your files, so that the glob matches your files. Or else the glob expressions are passed literally to ln, creating a file called *, pointing to a non-existant * file.

    FROM php:7.3-apache
    
    COPY myfrontend/ /app/frontend/
    
    RUN ["/bin/bash", "-c", "ln -s /app/frontend/web/* /var/www/html"]