andrew17
andrew17

Reputation: 925

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:

Upvotes: 6

Views: 13625

Answers (3)

lkceo
lkceo

Reputation: 11

FROM php:7.4-cli
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["php", "/usr/src/app/mail/contact_me.php"]

Here's a breakdown of the changes made:

Added WORKDIR instruction to set the working directory to /usr/src/app. This will be the base directory for subsequent instructions.

Modified the CMD instruction to provide the correct path to the PHP script. Assuming the contact_me.php file is located in the mail directory inside the image, we specify the full path as /usr/src/app/mail/contact_me.php.

Please note that you should ensure the directory structure and file locations match your project setup.

Upvotes: 1

Reqven
Reqven

Reputation: 1778

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

Upvotes: 22

Anatoliy77
Anatoliy77

Reputation: 1

Read there about Dockerfile (official documentation): Best practices for writing Dockerfiles

And may be you need another Docker image: Docker HUB ~> bitnami/php-fpme

Upvotes: -2

Related Questions