'How to run Docker container with website and php?
I have a landing page and one PHP file to send emails (feedback form). I want to test this form using Docker.
I've written this Dockerfile:
FROM php:7.4-cli
COPY . /usr/src/app
CMD [ "php", "/mail/contact_me.php"]
But it doesn't work for me.
I have the directory mail with the PHP file in the root of the project, but I'm still unsure if the Dockerfile is correct:
- Is it enough to inherit
FROM php:7.4-clior do I have to add nginx server to run the image? - What does the line
COPY . /usr/src/appexactly do? Is this correct?
Solution 1:[1]
A Dockerfile is used when you want to create a custom image.
FROM php:7.4-cli specifies the base image you want to customize.COPY . /usr/src/app copie the host current directory . into the container /usr/src/app.CMD [ "php", "/mail/contact_me.php"] specifies what command to run within the container.
In your case, I don't think a custom image is required.
As you need a webserver with PHP, you can use the php:7.4.3-apache image which comes with PHP7 and Apache webserver pre-installed. All you need to do is copy your app to your container, or use a volume. A volume is great because it actually mounts your host directory into your container, allowing you to edit your app from the host and see changes in real-time.
You can use a docker-compose.yml file for that.
version: "2"
services:
webserver:
image: php:7.4.3-apache
ports:
- "8181:80"
volumes:
- ./app:/var/www/html
Assuming your application is located in an app folder on your host machine, this folder will get mounted at /var/html/html on your container. Here the 8181:80 will redirect the 8181 port on your host machine to the 80 port of your container, which is the http port.
Use this command to start your container:
docker-compose up -d
Your should see your landing page at http://localhost:8181
Solution 2:[2]
Read there about Dockerfile (official documentation): Best practices for writing Dockerfiles
And may be you need another Docker image: Docker HUB ~> bitnami/php-fpme
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reqven |
| Solution 2 |
