Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: docker-compose copy files from container to host

I want to copy all files during the installation process in docker-compose.yml file.

If I run:

$ git clone https://github.com/laravel/laravel.git laravel-app
$ cd laravel-app
$ docker run --rm -v $(pwd):/app composer install

It will copy all new files from container to host during the installation process in the docker container.

So I will see new vendor folder and composer.lock file in my laravel-app directory after installation.


But if I setup volume in docker-compose.yml:

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

And then setup installation process in Dockerfile:

FROM php:7.4.4-fpm

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy existing application directory contents
COPY . /var/www

# Install composer.json dependencies
RUN composer install # <<-- !this one!

It will not copy vendor folder and composer.lock file back to my host.

Hot to make it happen?

like image 612
mr.boris Avatar asked Apr 16 '20 22:04

mr.boris


2 Answers

how about add composer docker image to your docker compose, and run command install

version: '3'
services:
    #PHP Service
    #your php image

    # composer
    composer:
      image: composer/composer
      volumes:
        - ./:/var/www
      command: install
like image 107
Aswita Hidayat Avatar answered Oct 25 '22 10:10

Aswita Hidayat


  • You copy all build context into your image /var/www

  • You run composer install from the image, so vendor and composer.lock only exist in the image, not on your host

  • You bind mount your current directory(which has composer.json but not vendor and composer.lock) to /var/www, so it replaces the image /var/www

You know your issue, the fix depends on what you want to do exactly, tell me in a comment.

Tip: You should use Docker multi-stage builds to install Composer into your image. It's much cleaner than the curl

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

COPY . /var/www/

RUN composer install --no-scripts --no-suggest --optimize-autoloader
like image 22
Shizzen83 Avatar answered Oct 25 '22 11:10

Shizzen83