问题
well I was using wamp-server on windows 10 for a long time and that was fine but now I want to go for docker and use its benefits. problem is I want to use my docker PHP interpreter on my local machine so according to this question:
set PHP path from host to docker container
I've found a solution to use something like this:
docker exec -i php73_fpm php %*
in a php.bat
file, place it in a directory and add it to environment variables and that looks fine.
when i hit php -v
it shows me the version that i have on docker not locally so that should be fine.
until i tried to use composer. and as soon as i hit php composer
in a CMD window, it shows an error saying:
Could not open input file: C:\ProgramData\ComposerSetup\bin\composer.phar
this is because i changed php.exe
path to php.bat
i assume? and composer is expecting php.exe
path in environment variables?
how can i force composer to use my docker PHP container not the locally installed one?
EDIT: i will post my docker-compose.yml content here in case that might be needed:
version: '3'
networks:
web_server:
driver: bridge
services:
nginx:
image: nginx
container_name: nginx_server
ports:
- "80:80"
- "443:443"
depends_on:
- php
- couchbase
command: [nginx-debug, '-g', 'daemon off;']
restart: unless-stopped
tty: true
volumes:
- ./docker/nginx/logs:/var/log/nginx
- ./docker/nginx/sites:/etc/nginx/conf.d
- ./src:/usr/share/nginx/html
networks:
- web_server
php:
container_name: php73_fpm
build:
context: ./docker/php-fpm
dockerfile: Dockerfile
working_dir: /usr/share/nginx/html
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: php73-fpm
SERVICE_TAGS: dev
ports:
- "9000:9000"
environment:
- COUCHBASE_HOST=couchbase
- COUCHBASE_BUCKET_NAME=default
- COUCHBASE_BUCKET_PASSWORD=
volumes:
- ./src:/usr/share/nginx/html
networks:
- web_server
couchbase:
image: couchbase
container_name: couchbase
ports:
- 8091:8091
- 8092:8092
- 8093:8093
volumes:
- ./docker/couchbase:/opt/couchbase/var
restart: unless-stopped
tty: true
networks:
- web_server
回答1:
First of all this answer is inspired by @soju suggestion about installing composer on the container itself.
well turned out I should have forget my local composer and install it to my container. so i installed it using this line in my php containter's Dockerfile:
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
then i should have remove composer directories from environment variables, these directories are:
C:\Users\[windows-username-here]\AppData\Roaming\Composer\vendor\bin
C:\ProgramData\ComposerSetup\bin\composer.phar
create another composer.bat file and place it somewhere with this content:
docker exec -i php73_fpm composer %*
add file path to environment variables. and thats it.
now when i use composer require something
it will send my command to the php73_fpm
docker container and do the job. but there should be a volume for the folder that i would use in the docker-compose.yml
file so when it creates the composer.json
it would place it in that directory on local machine.
来源:https://stackoverflow.com/questions/59375780/docker-php-container-and-local-composer-issue