Connect php container + Apache container + mysql container without Docker-Compose

Asked

Viewed 426 times

-1

I have access to a Linux server and I don’t have access to the internet. I only have Docker without Docker-Compose installed, and I have 3 images already loaded in Docker (apache, php and mysql). Each image is running in a container. How can I integrate the 3 containers to run web applications?

  • What exactly do you need apache for? Only with php can you create a localhost..

  • 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 ?

  • 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 browser localhost:8000

1 answer

0

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.

Browser other questions tagged

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