Well, looking at your problem, it seems to me that you don’t need the apache container, since with PHP you were able to instantiate a server as follows: php -S localhost:8000
. Starting from this idea, you only need the PHP and MYSQL container, let’s treat one at a time.
Container from MYSQL.
Here it is simple, you just need to instantiate the container normally, just do not forget to map the port with -p 3306:3306
, she will be useful.
Container from PHP
Your source code needs to be in here, and then you instantiate the server by command line, like I said above. Some points need to be noticed, for you to connect to the container in mysql, on the host vc need to point the IP of the MYSQL container and on the port, the port you set (in the case of above, would be the 3306), you can discover the container IP by giving a docker inspect <CONTAINER_ID>
. That should be enough to connect each other.
To make your life easier you can do some things, for example:
You can map a volume so you don’t have to keep rebuilding the image every time you change the code, at the instantiation of the PHP container you can send a flag by passing this parameter -v <SUA_PASTA_LOCAL>:<PASTA_DO_CONTAINER_COM_CODIGO>
Another tip is to use environment variables. With environment variables you will not need to keep changing the connection of the bank whenever the container IP changes, within php code you can capture these variables using getenv('exemplo')
. And when it comes to instantiating that container, you can fly and pass the value of that variable, like this, -e exemplo:valor_da_variavel_exemplo
.
What exactly do you need apache for? Only with php can you create a localhost..
– Matheus Barbosa
I always used apache and php in my web applications, I have no knowledge if with only php I can run an application. You need a web server, no ?
– Gabriel Lopes
By what I see, your case, only PHP and mysql solves. You have the source code ? If you have, do the experiment, install PHP and configure the path, go to the source code folder and send
php -S localhost:8000
this command means that you are creating a server on your localhost with port 8000, then just access the browserlocalhost:8000
– Matheus Barbosa