Hello everyone,
Been a minute, there was a situation with one of the web applications I manage and I needed to add some new features.
The application is working fine on the server but when I pulled it using git on my local machine I could not view it because the environment is different. While PHP 7.4 is being used on the server, PHP 8.0 is loaded on my local machine and I was able to upload the database to my local machine.
The challenge was viewing it on my local machine, an alternative was to downgrade the PHP back to 7.4 but that was unnecessary as Docker can do the magic. Enough stories let us get into it as I can create the same environment and connect the application from my container to the database on my local machine.
Firstly, Docker needs to be loaded on your machine, you can find the URL here below, I would not go into that as it is pretty straight forward
https://docs.docker.com/desktop/install/mac-install/
Secondly, let us dockerize the app, let us say the name of the Laravel application folder is “Laravelapp”
create a file name docker-compose.yaml. this is where we will write our configurations. let your configuration look like this
so the containers are broken down to 5. firstly we called it the “server” which is the Nginx. a folder is created in the root foler “.dockerfiles” where all docker files will be kept and I created “nginx.dockerfile” for the Nginx here is how it looks like
The shows the image of the nginx:stable-apline, what is happening here is that we are copying the nginx.conf settings to the working dir and moving nginx.conf to default.conf this is because we wanted to rename it to default.conf. we now changed the workdir to /var/www/html and copied all the files into it. a folder is created called .nginx where .nginx.conf is located in the root folder and here is the content.
The image above is the content of the .nginx/nginx.conf where you have php:9000 if your container/services is not named php just replace it with the name you used.
Back to the docker-composer file for the Nginx service, I set the port to 9000:80 meaning the port is 9000 on the local machine and 80 in the container, I created some bind mount, to reflect the folders and also the file in .nginx in the container. And the depends_on which include php and mysql means that it must first run those containers first before it.
The PHP service is straight forward and it is pointing to the dockerfile php.dockerfile located in the .dockfiles folder. the content below
This will get the PHP image 7.4 since that is the environment we want, copy all the files from local machine to the working directory install the necessary dependencies, and give permission rights.
Back to the docker-composer file for the Mysql service, This will get the mysql image and we are going to link environment variable which is located in a folder I created called .envs, there is a mysql.env inside the content below
This will contain your mysql configurations make sure its the same with the one on your local machine because you are going to connect to it in a bit. Also in your .env file which is in the root directory of your laravel app update the following lines.
DB_CONNECTION=mysql
DB_HOST=host.docker.internal
DB_PORT=3306
DB_DATABASE=leave
DB_USERNAME=root
DB_PASSWORD=mypassword
make sure DB_HOST=host.docker.internal this is set as this will connect the container to your local machine but if you dont want it to be connected to your local machine use DB_HOST=mysql where mysql is the name of the service/container in your docker-compose file.
Back to the docker-composer file for the Composer service, Without composer our laravel application will not work so we have to install it in the container a docker file has been created for this in the .dockerfiles folder called composer.dockerfile here is the content.
FROM composer:latest
WORKDIR /var/www/html
ENTRYPOINT ["composer","--ignore-platform-reqs"]
An entry point was created here showing it should start with “compose” as you know we will need to run composer install to get all the dependencies. I also added an artisan service, but that would not be needed for this tutorial as we are connecting to the MySQL on our local machine.
After all these have been set, go to the terminal and type this
$ docker-compose run --rm -d composer install
This will install composer and dependecies
and after that
$ docker-compose up -d server
This will start the container name server and we have depends on mysql and php so they will start running before this.
you can do
$ docker ps
To check the containers running and go to your browser and type “http://localhost:9000/” to view your laravel app.
Hope it was quite straight forwards thanks fam
You must be logged in to post a comment.