execute Docker/Docker-Compose commands on external folder

Asked

Viewed 201 times

1

I created a simple LAMP server using Docker/Docker-Compose, I was able to initialize the containers pointing to an external folder to which the Docker files are, the problem is that I need to run some commands, such as Composer, in the folder of each project and as the file docker-compose.yml is in an external folder that is not possible, as I can run commands with external folders?

exemplifying:

Docker files (Dockerfile and Docker-Compose.yml):

/home/thiago/docker/lamp

folders of several different projects:

/home/thiago/www/ (várias pastas, exemplo: projeto1, projeto2, projeto3)

example of using the Docker (in the project folder):

docker exec -it CONTAINER composer install

I tried to add the --workdir /home/Thiago/www/project1 flag/

but the following error is returned:

OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "chdir to cwd (\"/home/thiago/www/projeto1\") set in config.json failed: no such file or directory": unknown

Dockerfile

FROM php:7.4-apache

RUN a2enmod rewrite
RUN docker-php-ext-install mysqli pdo pdo_mysql

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u 1000 -d /home/sammy sammy
RUN mkdir -p /home/sammy/.composer && \
    chown -R sammy:sammy /home/sammy

Docker-Compose.yml

version: "3.7"
services:
    www:
        build: .
        ports: 
            - "80:80"
        volumes:
            - $HOME/www/:/var/www/html/
        links:
            - mysql
        networks:
            - default
    mysql:
        image: mysql:8.0
        ports: 
            - "3306:3306"
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_PASSWORD: root
            MYSQL_ROOT_PASSWORD: root 
        volumes:
            - ./dump:/docker-entrypoint-initdb.d
            - ./conf:/etc/mysql/conf.d
            - persistent:/var/lib/mysql
        networks:
            - default
volumes:
    persistent:
  • Are you trying to use a Docker environment for all projects? By my understanding the idea of Docker (even more so with Docker-Compose) is to have a "containerized" environment for each project. To use a shared environment you can better use apache and mysql system.

1 answer

0

You can use the command of the Docker Composer, so every time before starting it runs a command

Remembering that the command you want to run needs a bash so I am calling instead the /bin/bash, but it can be another quanquer like the binary sh /bin/sh

For the service to be running you need a Mon application in the case here is the php server, if you just leave the composer install Docker would run the command and once finished running would stop

version: "3.7"
services:
    www:
        build: .
        ports: 
            - "80:80"
        command:  /bin/bash -c "composer install &&  php server"
        volumes:
            - $HOME/www/:/var/www/html/
        links:
            - mysql
        networks:
            - default
    mysql:
        image: mysql:8.0
        ports: 
            - "3306:3306"
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_PASSWORD: root
            MYSQL_ROOT_PASSWORD: root 
        volumes:
            - ./dump:/docker-entrypoint-initdb.d
            - ./conf:/etc/mysql/conf.d
            - persistent:/var/lib/mysql
        networks:
            - default
volumes:
    persistent:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.