How to install Docker and Dockerize PHP Application

by Prasad Domala
0 comment

In this post I will show you how to install and configure Docker on Linux and Dockrize a sample PHP application. Please watch the video for detailed explanation and demo. I will be using Amazon EC2 instance as my Docker host for this demo.

Docker installation on Amazon Linux

Login to AWS EC2 instance or any other linux system you wish to use as a Docker host using SSH and execute below commands to install Docker

sudo yum update -y
sudo yum install -y docker
sudo service start docker
sudo usermod -a -G docker ec2-user
docker info 

PHP sample application setup

sudo yum install -y git
mkdir mydocker
cd mydocker
git clone https://github.com/pdomala/dockerize-php-sample.git 

Dockerfile

Create a file with name “Dockerfile” inside mydocker directory. Copy below contents to the Dockerfile and save it.

FROM ubuntu:16.04
MAINTAINER Prasad Domala <prasad.domala@gmail.com>

#Update Repository
RUN apt-get update -y

#Install Apache
RUN apt-get install -y apache2

#Install PHP Modules
RUN apt-get install -y php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-zip

#Copy Application Files
RUN rm -rf /var/www/html/*
ADD dockerize-php-sample /var/www/html

#Configure Apache (Optional)
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2

#Open port 80
EXPOSE 80

#Start Apache service
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"] 

Build & run Docker image

# Build docker image
docker build -t pdomala/dockerize-php-sample .

# Display Docker images
docker images

#Start Docker container based on new image
docker run -itd -p 80:80 pdomala/dockerize-php-sample

# Check container status
docker ps -a

# Application URL
http://dns-or-ip-of-docker-host/index.php 

Additional Docker commands

# Login to container shell
docker exec -it  bash

# Login to Docker Hub
docker login

# Push/pull image to/from Docker Hub
docker push pdomala/dockerize-php-sample
docker push pdomala/dockerize-php-sample

# Remove Docker image
docker rmi -f

# Remove Docker container
docker rm -f

# Remove all Docker containers
docker rm -f $(docker ps -aq) 

Leave a Comment