Briefly about what I'm trying to do: push frontend, backend and other services into separate repositories and clone the source code from git during image mounting instead of using the usual COPY . ./app.
Here is my Dockerfile with the problem area:
# Install git.
RUN apk update && apk add git
# Install composer.
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet \
&& rm -rf /root/.composer/cache
# Cloning a laravel project from a private repository in github.
RUN git clone https://${USERNAME}:${TOKEN}@github.com/${USERNAME}/api.laravelTest.com.git
# Select the folder with the cloned repository as the working directory.
WORKDIR /var/www/html/api.laravelTest.com
# Install dependencies with the `composer install` command.
RUN composer install
# In the nginx configuration, the root directive has the path: /app/public,
# so we clone the contents of the api.laravelTest.com folder into the app folder.
RUN cp -r /var/www/html/api.laravelTest.com/. /app
The screenshot shows that the repository was indeed cloned, and the dependencies were installed, but all this is in the /var/www/html folder, but what about RUN cp -r /var/www/html/api.laravelTest.com/. /app?
There were no errors during mounting, the whole process was completed successfully, the containers are working.
I go to the /app directory and do not see the laravel project files there, in other words, nothing was copied there from the /var/www/html/api.laravelTest.com folder and there were no errors during the execution of this command. Everything that is in this folder got there through volumes registered in docker-compose.yml.
Edited:
Containers for api in my docker-compose.yml:
api:
ports:
- "8002"
build:
context: api/nginx
dockerfile: Dockerfile
volumes:
- ./api:/app
depends_on:
- api-php-fpm
api-php-fpm:
build:
context: api
dockerfile: php-fpm/Dockerfile
volumes:
- ./api:/app
api-php-cli:
build:
context: api/php-cli
dockerfile: Dockerfile
volumes:
- ./api:/app
Api folder structure:
api
|
|--nginx
| |
| |-Dockerfile
| |-conf.d
| |
| |-default.conf
|
|--php-fpm
| |
| |-Dockerfile
|
|--php-cli
|
|-Dockerfile
I also tried to simplify the dockerfile as much as possible:
FROM nginx:1.21.3-alpine
# Copy nginx configuration.
COPY ./conf.d /etc/nginx/conf.d
# Install git.
RUN apk update && apk add git
# Cloning a laravel project from a private repository in github.
RUN git clone https://${USER}:${TOKEN}@github.com/${USER}/api.laravelTest.com.git
# Try copy to app - does not work.
RUN cp -r ./api.laravelTest.com/. ./app
# Try copy to other folder, in this case copy to /usr/share/nginx/html - it works!
RUN cp -r ./api.laravelTest.com/. ./usr/share/nginx/html
I also tried to immediately clone the files from the repository to the app folder:
# Trying to immediately clone into the app folder (also does not work).
WORKDIR /app
RUN git clone https://${USER}:${TOKEN}@github.com/${USER}/api.laravelTest.com.git .
At the moment, any attempt to copy files or clone the repository, in other words, changing the contents of the app folder from the dockerfile, has no effect.
But, if I enter the container using the command:
root@:~/# docker exec -it <container_id> /bin/sh
And I execute next command inside the container, that it works:
cp -r ./api.laravelTest.com/. ./app