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

  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
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
#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
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
For WEB: docker build -t web:v1 .
For Database: dokcer build -t db:v1 .
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
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
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
$docker-compose up -d
To check service up or not:
$docker-compose ps
To check service logs
$docker-compose logs -f <service_name>
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:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store