PHP

PHP and Mysql environment with Docker

Kartic1 min read

Create a Dockerfile in your PHP project

FROM php:8.2-cli

COPY . /usr/src/myapp

WORKDIR /usr/src/myapp

CMD [ "php", "./your-script.php" ]

Then, run the commands to build and run the Docker image:

$ docker build -t my-php-app .

$ docker run -it --rm --name my-running-app my-php-app

Run a single PHP script For many simple, single file projects, you may find it inconvenient to write a complete Dockerfile. In such cases, you can run a PHP script by using the PHP Docker image directly:

Create a Dockerfile with the code below:


FROM php:8.2-cli
COPY example_app/ /var/www/html
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
EXPOSE 80


Create a docker-compose.yml file with code below

version: "3.7"
services:
web:
build: .
volumes:
- ./example_app:/var/www/html
ports:
- "80:80"
db:
image: mysql
volumes:
- ./db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: example_app
ports:
- "3306:3306"
Share this articleLinkedIn X

Discussion (0)

Sign in to join the discussion.

Delete this comment?

This will also remove its replies.

Keep exploring

PHP1 min read

What is recursion in PHP

Recursion is a programming solution in which a function calls itself. It is used to solve a variety of problems, all of which have in common a structure that contains repetition. R...

Kartic · May 20, 2023
PHP1 min read

What is the difference between PHP's get & post method

​​​​​​​Get MethodGET requests can be cachedGET requests remain in the browser historyGET requests can be bookmarkedGET requests should never be used when dealing with sensitive dat...

Kartic · Oct 27, 2019
PHP1 min read

What is PHP?

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for develop...

Kartic · Oct 22, 2019

Follow the engineering notes.

Subscribe for technical articles from iTechOrbit.