Debugging with Docker Compose remote interpreter: db container unknown - php

I am currently trying to debug my tests in PhpStorm with a docker-container as remote interpreter. The problem I am facing is that, though I can connect to the remote interpreter and execute my code on it, the container seems to be unaware of the other containers from the compose file. Unlike normally he is unable to connect to the database, which runs in another container (host=mysql).
Does anybody had the same issue before and found a solution?

Are you setting up your remote interpreter as Docker or Docker Compose?
In PhpStorm go to Preferences -> PHP -> CLI Interpreter -> ... and Check if you have Docker Compose or Docker section right under the name of Interpreter. If it's not Docker Compose, add a new Interpreter and make sure to chose Docker Compose for this one

Related

Symfony 5 exception Could not find driver

I am making my first app with Symfony and when i setup my database (doesn't matter if it's postgresql or mysql).
I have an error "could not find driver" three time.
One in PDOConnection.php line 38.
Other in Exception.php line 18.
And last one in AbstractMySQLDriver.php line 128.
What i made : i check the php path i also check all the extensions. I check if i fill correctly .env and i check those folders.
PHP version : 7.4
And mysql in docker containers: mysql:5.7.
I really have no idea what to do now
UPDATE:
The problem (for me) was the command php bin/console doctrine:database:create
if you have the same problem try symfony doctrine:database:create
do you use .env file? I have same problem and after a day of disable/enable pdo without any effect, i found that symfony auto add a line to my .env file:
DATABASE_URL=postgresql://db_user:db_password#127.0.0.1:5432/db_name?serverVersion=11&charset=utf8
That make my database url wrong and error not found pdo fire because of postgresql not mysql
Apparently you use Docker, but in your question you mentioned Windows cmd. I suppose, you check PDO extension in your host system, not in container. You need to log into your Docker PHP container and check it there. In Windows Desktop version of Docker there is a button near each container to open command-line interface. You also can use command:
docker exec -it <here goes your container name>
To find out what your container name is, you can use command
docker ps
UPDATE:
To start your project not in Docker, bun on Windows-host itself, you can use very helpful mini-server, provided by Symfony. Run this command inside your project directory:
symfony serve
It will start local server, available by default at address https://127.0.0.1:8000/.
However, keep in mind, that if your project requires database, you will need to have MySQL Server for Windows installed on your host system too.

How do I setup my host machine's PHP interpreter to point to the container's PHP interpreter

I would like to configure PHPStorm so that I can run Artisan commands directly from my IDE's terminal. For example php artisan key:generate. However, when I do, I receive the error 'php' is not recognized as an internal or external command.
Background:
running Windows 10 as host machine
using Docker to run my webserver (Nginx) and PHP interpreter (7.2-fpm).
I followed this tutorial.
https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose.
One thing the tutorial didn't cover was how to set up my remote PHP CLI interpreter. For that, I followed https://www.jetbrains.com/help/phpstorm/configuring-remote-interpreters.html and can make a successful connection as it picks up the correct PHP version 7.2.21 as seen in the screenshot.
After this, I restarted my Windows 10 host machine, fired up the container, opened my IDE and tried to run php -v from the terminal but hit the error again.
I know it has something to do with my PATH environment variable being set incorrectly, or not at all but I am not sure how to point it to the container's PHP interpreter. Any help would be greatly appreciated.
EDIT
For what it is worth, I know that I can exec into the container and run PHP commands from there, but thought I might be able to do it directly from my host machine by pointing to the container's interpreter.
The terminal in PHPstorm is the same as any other terminal on your computer. It doesn't automatically connect to docker to run the php -v command. If you want to run php from PHPstorm in docker you can right click the file:
Choose run:
And select your created docker interpreter as your runtime:
If the fix button doesn't show it can be configured through:
settings > Languages & Frameworks > PHP
And then clicking on the 3 dots at the end of "CLI interpreter"

Docker, PhpStorm & Xdebug: Can't find source position error

I built an image that is based on a php:5.6-fpm-alpine image and I run a symfony-based application therefore I run both cli and web-based php scripts.
So I spawned a shell over my running container via:
docker exec -ti ^container_id^ /bin/sh
And over the shell I exported the following enviromental variables:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
export XDEBUG_CONFIG="idekey=PHPSTORM"
And the IDE has been setup as explained in the following links:
Can't connect PhpStorm with xdebug with Docker
How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04
But when I enable the Xdebug on phpstorm even it debugs normally I get the following error message:
Do you know why that happens.
That happend because on Servers section of the phpstorm does not have the very same name as defined in the PHP_IDE_CONFIG enviromental variable in order to solve that follow these steps:
Step 1:
Go to server settings by pressing Ctrl+Alt+S or by visiting File -> Settings from the menu.
Step 2:
Then on the open window go to Settings -> Languages & Framework -> Servers.
Step 3:
Set the Name as the one defined into:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
In order to work it should be 0.0.0.0:5092 as the image shows:

Debug Symfony CLI application with PhpStorm inside Docker container

My current setup involve PhpStorm IDE in which I have imported Symfony 3 projects which is basically CLI tool. On the host machine I don't have PHP installed so I'm running the application from Docker container which has PHP and Xdebug installed.
I don't have issues to debug web applications from Docker containers but with Symfony and this CLI tool it seems a little bit more tricky.
My question is how to properly set this up and debug it from PhpStorm? I tried to create a new debug configuration (PHP Remote Debug) but breakpoints are not trigged.
Suppossing you have followed into the instructions mentioned into the following links:
Can't connect PhpStorm with xdebug with Docker
How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04
Or similar questions
Then you need to follow theese steps:
Step1:
Get shell access to your container via running:
docker exec -ti ^container_id^ /bin/sh
Or if running a debian/ubuntu based one (or you installed manually bash):
docker exec -ti ^container_id^ /bin/bash
The ^container_id^ can be found via docker ps command it is the first column of the table. If running on a small window just pipe int into less -S resulting the command:
docker ps | less -S
Then export the following enviromental variables:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
export XDEBUG_CONFIG="idekey=PHPSTORM"
Please keep in mind to setup the correct value specified into Servers section as you see in the image:
It is important in order not to run into the problem specified in this question.
Then just enable debugger listentin into the phpstorm and spawn the cli as you do when you run a symfony application.

Docker + PHP - ERROR: Couldn't connect to Docker daemon?

I'm following this guide to set up a PHP development environment with Docker.
I have created a folder on my desktop docker-php and added a docker-compose.yml file into it, with this content:
nginx:
image: nginx:latest
ports:
- 80:80
On my terminal:
$ cd /home/my-username/Desktop/docker-php/
$ docker-compose up -d
I get this error:
ERROR: Couldn't connect to Docker daemon at
http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the
DOCKER_HOST environment variable.
I'm on Xubuntu 16.04.
Or perhaps I should put the folder in the specific location that is required by Docker? If so, which is it?
The most common reason for this error is that you ran 'docker-compose up' without sudo. As long as there is docker installed and is up and running, you are likely missing sudo in the docker command.
You could use native Docker
One option is to abandon docker-machine and use a native Docker setup on your system. Since you are on Linux (Xubuntu), this is an option for you. docker-machine is most often used by people who can't run Docker natively (Mac or Windows), and use it to install a Docker-capable VM and some local commands on their OS to talk to it.
You can find install docs for Docker on Linux here.
However, you already have docker-machine installed, so this may be the most disruptive option for you.
Your docker-machine may not be running
The error you are getting is saying the Docker client cannot talk to the server. One potential reason for this is that your docker-machine VM isn't running. You should verify it is running, and if not, start it.
To get a list of your docker-machines (may be one or more):
docker-machine ls
You will probably have one machine named default, but you may have more, depending on how you did your setup.
You can get the current status with:
docker-machine status <machine-name>
And you can use stop, start, restart to manage the docker-machine.
(More in the Docker Machine CLI reference.)
You need the proper environment set
docker-machine relies on environment variables to work properly. Because you may have multiple docker-machine setups, you have to tell the client which one to use.
To set the environment, you can get it from the docker-machine command.
docker-machine env <machine-name>
And you can automatically inject it into the environment (this may be a useful thing to put into your shell startup file).
eval "$(docker-machine env <machine-name>)"
You should end up with env vars similar to these:
DOCKER_HOST=tcp://192.168.99.101:2376
DOCKER_CERT_PATH=/Users/nathanleclaire/.docker/machines/.client
DOCKER_TLS_VERIFY=1
DOCKER_MACHINE_NAME=dev
Keep in mind you should use the eval form here, not just run the env command and paste the output into your shell setup; it may change on a docker-machine restart, etc, so you can't rely on an old setup to still work later.
If your docker-machine is running, and these env vars are set, your docker and docker-compose commands should work.
Solution:
sudo usermod -a -G docker USERNAME

Categories