Skip to Content
CoursesLearn DockerDockerfiles with Port Forwarding

Using a Containerized PHP Server

View the Full Course Now 

Using a PHP Dev-Server inside a Container with a Dockerfile

To get started, let’s change the Dockerfile from the previous example to:

FROM php:7.2-cli EXPOSE 8000 RUN mkdir /myproject COPY index.php /myproject WORKDIR /myproject CMD ["php", "-S", "0.0.0.0:8000"]

Then run the command:

docker build -t myphpapp .
  • Will rebuild the image and tag it again as myphpapp
  • Will overwrite the old one
  • Will copy the index.php to a new directory /myproject
  • Will run php -S 0.0.0.0:8000 and serve the content of /myproject on port 8000
  • CMD will take an array and run a command. Every parameter must be in a separate array element, so: CMD ["command", "param1", "param2", ...]
docker run --name myphp-container -p 8080:8000 myphpapp
  • Will run spin up the image myphpapp which we generated

  • -p 8080:8000 Will forward port 8000 in the container to port 8080 on the host

  • http://localhost:8080  should bring up the hello world

End this with ctrl+c

  • to stop the container
  • Mind on Windows: The container is still running in the background.
docker rm -f myphp-container
  • To remove the container: cleanup
docker rmi myphpapp
  • Remove the myphpp image again
Last updated on