Docker/PHP

From Wikiversity
Jump to navigation Jump to search

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]

  1. Use Play with Docker. Create an account and/or log in.
  2. Start an interactive session and add a new instance.
  3. In the terminal window, enter the following commands:
    • touch Dockerfile
    • touch index.php
  4. Use the Editor button to edit both files and save the contents above into each respective file.
  5. Run the following commands:
    • docker build -t php-server .
    • docker run -d -p 8000:80 --name=php-server php-server
  6. In the top window, select the 8000 button to connect to the running server.

On Your Own System[edit | edit source]

  1. Install Docker Desktop or the Docker Engine.
  2. Save the files above into a new Docker PHP folder:
    • Dockerfile
    • index.php
  3. 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
  4. Open a web browser to connect to the running server:

See Also[edit | edit source]