Deploy A simple and practical example for a PHP/MySQL in Docker Container

Vineet Kumar
3 min readJul 31, 2020

--

There is many way to deploy PHP code with MySQL in Docker Environment.We will start from scratch i.e. from without customize Images to Customize images with Docker compose. There are Following steps

  1. Basic Requirements: PHP code and MySQL dump file, You can download the code from https://github.com/vineetkumar03/php_mysql_demo.git
    Please read the instruction to run PHP code without Docker. DB connection needs Environment variable to run the code.
    To run in docker environment put all PHP code in src Directory.
    To Communication with web and db via two methods : 1. Link method(Link PHP container with MySQL container’s name) 2. With Same Network(Create a docker network and run both container same network)
  2. Run it From Basic Docker Commands using Base Images: Use Docker basic commands run PHP with MySQL using link method with name.
FOR DATABASE:docker run --name db -v register.sql:/docker-entrypoint-initdb.d/ -e  MYSQL_USER=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_ROOT_PASSWORD=tests -p 3306:3306  -d mariadb:latestFOR WEB:docker run -d --name phpcore -v ./src:/app/public --link db -p 8000:80  -e MYSQL_ROOT_PASSWORD=tests -e MYSQL_USERNAME=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_HOST=db  php:7.4-alpine

OR

Use Docker basic commands run PHP with MySQL using network method with same network name.

Create Network:
docker network create mynet
FOR DATABASE:
docker run --name db --net mynet -v register.sql:/docker-entrypoint-initdb.d/ -e MYSQL_USER=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_ROOT_PASSWORD=tests -p 3306:3306 -d mariadb:latest
FOR WEB:
docker run -d --net mynet -v ./src:/app/public -p 8000:80 -e MYSQL_ROOT_PASSWORD=tests -e MYSQL_USERNAME=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_HOST=db php:7.4-alpine

2. Run it Form Basic Docker commands Using customize Docker Image:
For creating customized docker images use Docker file for PHP web and MySQL database.
Docker images link is as below
https://github.com/vineetkumar03/php_mysql_dockerfiles
OR

#FOR DATABASE:
FROM mariadb:latest
LABEL maintainer="vineet kumar"
COPY *.sh /docker-entrypoint-initdb.d/
COPY *.sql /docker-entrypoint-initdb.d/
#Environment variable may store in .env file

FOR WEB:

From  vineetkumar03/php:7.4-alpine
LABEL maintainer="VINEET KUMAR"
ADD src /app/publicENV MYSQL_HOST localhost
ENV MYSQL_USERNAME tests
ENV MYSQL_PASSWORD tests
ENV MYSQL_DATABASE register
RUN chown -R www-data:www-data /app

Command to Create Image From Dokcerfile:

For WEB: docker build -t web:v1 .
For Database: dokcer build -t db:v1 .

3. Docker command run with Customize Image: Link with Database container’s name

For Database:
docker run --name db -e MYSQL_USER=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_ROOT_PASSWORD=tests -p 3306:3306 -d db:v1
For WEB:
docker run -d --name web --link db -p 8000:80 -e MYSQL_ROOT_PASSWORD=tests -e MYSQL_USERNAME=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_HOST=db web:v1

Connect with same network

Create Network:
docker network create mynet
For Database:
docker run --name db --net mynet -e MYSQL_USER=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_ROOT_PASSWORD=tests -p 3306:3306 -d db:v1
For WEB:
docker run -d --name web --net mynet -p 8000:80 -e MYSQL_ROOT_PASSWORD=tests -e MYSQL_USERNAME=tests -e MYSQL_PASSWORD=tests -e MYSQL_DATABASE=register -e MYSQL_HOST=db web:v1

3. Run with Docker compose file: Using the Compose command line tool you can create and start one or more containers for each dependency with a single command ( docker-compose up ).
A Docker file is a simple text file that contains the commands a user could call to assemble an image whereas Docker Compose is a tool for defining and running multi-container Docker applications. Docker Compose define the services that make up your app in docker-compose.
Similarly we can connect PHP with MySQL using Name method as well as Same network Methods. Docker compose file is as below
Connect With Service Name

version: "3.2"
services:
mysql:
image: db:v1
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=tests
- MYSQL_USER=tests
- MYSQL_PASSWORD=tests
- MYSQL_DATABASE=register
web:
image: web:v1
environment:
- MYSQL_ROOT_PASSWORD=tests
- MYSQL_USERNAME=tests
- MYSQL_PASSWORD=tests
- MYSQL_DATABASE=register
- MYSQL_HOST=mysql
ports:
- "8000:80"
volumes:
- ./src:/app/public

Command to run docker compose file:

$docker-compose up -d
To check service up or not:
$docker-compose ps
To check service logs
$docker-compose logs -f <service_name>

Advanced Docker compose with build parameter from base images is as below, or you can download or clone the code from https://github.com/vineetkumar03/php-mysql-advance-dokcer-baseimages.git

version: "3.2"
services:
web:
build: ./
image: webcompose:v1
container_name: web
ports:
- "8000:80"
environment:
- MYSQL_USERNAME=tests
- MYSQL_PASSWORD=tests
- MYSQL_DATABASE=register
- MYSQL_HOST=mysql
hostname: phpweb
volumes:
- ./src:/app
depends_on:
- mysql
networks:
- frontend
links:
- mysql
restart: on-failure
stdin_open: true
tty: true


mysql:
build: ./mysqldocker
image: dbcompose:v1
container_name: database
hostname: database
ports:
- "3306:3306"
restart: always
networks:
- frontend
environment:
- MYSQL_ROOT_PASSWORD=tests
- MYSQL_USER=tests
- MYSQL_PASSWORD=tests
- MYSQL_DATABASE=register
volumes:
- ./mysql:/var/lib/mysql
networks:
frontend:
backend:

Very first time run it by “$docker-compose up -d” it will create images but after that build it by “docker-compose up --build” command

4. Enjoy!!!!

--

--