What is Docker? Docker is a software platform that allows you to pack, distribute and run applications in autonomous units called containers. These containers include everything the application needs, such as code, execution and libraries, guaranteeing coherent execution in different environments.
How can it be useful? There are many possible uses of Docker containers. You can use them for tests for example. If you have a website that runs on PHP 8.1 and you want to make sure that the transition to PHP 8.3 does not break anything, you can create a container based on PHP 8.3 and fully test your application before making a change on the main website. Other options include the distribution of your application to your customers as a container or using containers for web hosting if your web hosting provider supports this option.
In this article, we will explain how you can pack your phprunner application as a Docker container.
Docker office installation
In the first step, you must install Docker Desktop on your local computer. Just download it from the official website and install all the default settings.
Once Docker Desktop installed, launch it. Two main sections with which we are going to work are Images And Containers.
An image Docker is a single reading model that contains everything you need to perform an application, including code, system tools and application libraries. A Docker container is an instance being executed in a docker image, think of an image like a plan and a container like the building built from this plan.
Project structure
Create a new file for our Docker project, that is to say C: \ Project \ Docker. Here is what the Default Docker project looks like. Src The folder must be created manually and this is where all of our PHP files will be stored. Fortunately, we don’t need to create the rest of the file manually, Docker Desktop can help us create them.
Docker Init
Start by opening a terminal (click on the terminal icon in the lower right corner). First, go to your project file, then run Docker Init order.
Docker will ask you a few questions about what you want to create. Select Apache web application + PHP. You can use high and bottom keyboard keys to change your selection. Press Entrance to make a choice.
For all other questions, leave the default options. Docker will create an image for you, depending on your selection. On Images Tab You will now see a new image. Docker will also tell you that now you should run Docker composed –Build Order to create and launch your container.
Dockerfile
This is how the initial Dockerfile Look at. I just deleted some additional comments for Brevity. Let’s try to create a container and execute it.
Build and first race
Without making any change to Dockerfile We return to the terminal and enforce the suggested construction command:
First build will take about a minute and consecutive will be a little faster. Dcker does the construction and starts our container. You can see the container status tab on the containers. You can see that our new container is operational.
This means that we can now open a web browser and enter the following URL: http: // localhost: 9000. This is what you are supposed to see:
Good news: our container is actually underway.
Bad news: a MySQLI extension is missing and we cannot connect to MySQL.
Installation of the MySQLI extension
The Google Quick search for “Docker PHP Install MySQLI” we point to the solution and we modify our Dockerfile In any text editor, adding lines 7 and 8.
Now we stop the container, get the Build command again and once finished, we update our browser window. It makes us even further, but we are not yet quite there.
Although this error is not very descriptive, we can guess that our application is not able to connect to MySQL. We use “Localhost” as a mysql server address which, in the case of the container, refers to the container itself. We must find a way to connect to MySQL on the host which is our local computer running Docktop. Another quick search for Google from “Docker Connect to the host from the container” we point to host.docker. internal that we must use instead of local host When connecting to MySQL.
We proceed to Phprunner, create a new server database connection on the ‘Directory’ screen and modify the address of MySQL Server as follows:
And one more test
1. Stop the container
2. Build the PhPrunner project (make sure the new connection is selected)
3. Copy all the files in the PHPRUNNER output folder to C: \ Projects \ DOCKER \ SRC
4. Execute Docker composed –Build Order once again.
5. Refresh the browser window
It works!
Additional notes. You can make your container connect to any database. It can be a database that is part of the same container (although it is not recommended), or a database executed in another container or any external database server. All you have to do is point out the database connection to your choice database.
Technology
Motivation
News
Pendidikan
Pendidikan
Download Anime