Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
In this tutorial, you'll learn how to create a Docker container running PHP, a popular server-side scripting language widely used for web development. We'll use the official PHP image provided by the Docker community.
Prerequisites:
Pull the official PHP image:
To pull the official PHP image with Apache, run the following command:
docker pull php:apache
This will download the latest version of the PHP image with Apache web server to your system.
Create a sample PHP application:
First, create a new directory for your PHP application:
mkdir php-app cd php-app
Next, create a new file named index.php
in the php-app
directory with the following content:
<?php echo 'Hello World!'; ?>
This is a simple PHP script that prints "Hello World!" when executed.
Create a new container with PHP and Apache:
To create a new Docker container running your PHP application with Apache, use the docker run
command. We'll map the container's port 80 (the default Apache HTTP port) to the host's port 8080, so you can access the application from your host machine. Additionally, we'll mount the local php-app
directory as a volume inside the container:
docker run --name php-container -p 8080:80 -v $(pwd):/var/www/html -d php:apache
This command creates a new container named php-container
, maps the container's port 80 to the host's port 8080, mounts the local php-app
directory as a volume at /var/www/html
inside the container, and runs the container in detached mode (-d
flag). The container will run the Apache web server with PHP enabled and serve the index.php
file.
Verify the PHP container is running:
To check if the PHP container is running, use the docker ps
command:
docker ps
You should see your php-container
listed as running.
Access the PHP application:
Open your browser and navigate to http://localhost:8080
. You should see the "Hello World!" message from your PHP application.
In this tutorial, we covered how to create a Docker container running a simple PHP application with the Apache web server, access the application from your host machine, and update the application while it's running. You can now use this containerized PHP environment for development, testing, or deployment purposes.
Running PHP in Docker Container:
docker run -d -p 80:80 --name my-php-container php:latest-apache
How to Create a Docker Image with PHP:
FROM php:latest-apache # Add custom configurations or install extensions
Docker-Compose PHP Installation:
version: '3' services: php: image: php:latest-apache ports: - "80:80"
PHP Dockerfile Example:
FROM php:latest-apache COPY php.ini /usr/local/etc/php/php.ini
Setting Up PHP in a Docker Environment:
version: '3' services: php: image: php:latest-apache ports: - "80:80" volumes: - ./php.ini:/usr/local/etc/php/php.ini
Configuring PHP in Docker-Compose:
version: '3' services: php: image: php:latest-apache ports: - "80:80" volumes: - ./php.ini:/usr/local/etc/php/php.ini - ./htdocs:/var/www/html
Docker PHP Image Customization:
FROM php:latest-apache COPY php.ini /usr/local/etc/php/php.ini
Docker PHP Volume Mounts:
version: '3' services: php: image: php:latest-apache volumes: - ./php.ini:/usr/local/etc/php/php.ini - ./htdocs:/var/www/html
Custom PHP Image Creation in Docker:
FROM php:latest-apache COPY php.ini /usr/local/etc/php/php.ini
Docker Swarm and PHP Service:
version: '3' services: php: image: php:latest-apache deploy: replicas: 3 # Other Swarm deployment configurations
Troubleshooting PHP in Docker:
docker logs <php_container_id_or_name>