Docker/PHP
Appearance
< Docker
Dockerfile
[edit | edit source]# Configures an Apache PHP server and copies the current folder content
# to www root.
# Use the following commands to build and run the server.
# docker build -t php-server .
# docker run -d -p 8000:80 --name=php-server php-server
# Then open a web browser and connect to http://localhost:8000 .
# References:
# https://hub.docker.com/_/php
FROM php:7.4-apache
COPY . /var/www/html/
RUN chmod 755 /var/www/html/index.php
EXPOSE 80
index.php
[edit | edit source]<?php
// Displays 'Hello world!'
//
// References:
// https://repl.it/languages/php7
// https://www.php.net/manual/en/tutorial.firstpage.php
echo 'Hello world!';
?>
Try It
[edit | edit source]Online Free
[edit | edit source]- Use Play with Docker. Create an account and/or log in.
- Start an interactive session and add a new instance.
- In the terminal window, enter the following commands:
touch Dockerfile
touch index.php
- Use the
Editor
button to edit both files and save the contents above into each respective file. - Run the following commands:
docker build -t php-server .
docker run -d -p 8000:80 --name=php-server php-server
- In the top window, select the
8000
button to connect to the running server.
On Your Own System
[edit | edit source]- Install Docker Desktop or the Docker Engine.
- Save the files above into a new
Docker PHP
folder:Dockerfile
index.php
- At a command prompt, change to the
Docker PHP
folder and then run the following commands:docker build -t php-server .
docker run -d -p 8000:80 --name=php-server php-server
- Open a web browser to connect to the running server: