Tag Archives: Docker

Using Docker to localhost PHP and mysql

If we want to run mysql, too, not only php (as shown here), here’s one way of doing it.

It is a bit more complicated. One reason is that the official php apache image doesn’t come with mysql support. Another reason is that you need to find out the IP address of the mysql container.

This is for developing, not for shipping applications.

Running mysql

docker run --name mymysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=testdb -e MYSQL_USER=testuser -e MYSQL_PASSWORD=testpw -d mysql:8.0

This time I use names, in this case mymysql. It’s easier to set a name so that  we already know the name of the container we want to link to (instead of using a randomly assigned name).

my-secret-pw is the password for the mysql root user, set with MYSQL_ROOT_PASSWORD. In the same way set the following to whatever you like, if needed


You can check at the Docker Hub what mysql versions are available.

Running php

Start php as before, but use

--link mymysql:mysql

in this case mymysql because that’s the name I chose for my mysql.

We can’t use an unmodified image anymore (the official php apache image doesn’t come with mysql support), so we need a Dockerfile or we get a Class ‘mysqli’ not found error.

Create a Dockerfile with the following content

FROM php:7-apache
RUN docker-php-ext-install mysqli

go to folder with Dockerfile, then build, in this case I call is phpwsql

docker build -t phpwsql .

then run

docker run --name myphp70 --link mymysql:mysql -d -p 80:80 -v /pathtofolder/:/var/www/html/ phpwsql

Get the IP address of the mysql container

The mysql and the php containers will have different IP addresses, so localhost won’t work. To get the IP address of the mysql container type

docker inspect mymysql | grep IPAddress

which you can then use in your php code.


At the time of writing this blog post both mysql and php-apache are based on debian:jessie, so there’s not much overhead.


Using Docker to localhost PHP

Another reminder for myself (or my students), this time how to use Docker to localhost PHP.

Download Docker from docker.com

Localhost php

In terminal run

docker run -d -p 80:80 -v /pathtofolder/:/var/www/html/ php:7.1-apache

-d for detach, i.e. run in background
-p for publish, i.e. publish container’s port(s) to the host
-v for volume, i.e. bind mount a volume

pathtofolder example: /Users/memm/GitHub/project1/backend/

You can check at the Docker Hub what php versions are available, other than 7.1-apache. Specific versions are on the left, to the right the get less specific, so get get the latest one, but it might break your application.

See all containers


docker ps

to see details of running containers. They tend to have funny names, like gigantic_snyder if you use it as described (i.e. if no name was specified).


docker ps -a

to see all containers, not only running ones.

Remove all stopped containers with

docker rm $(docker ps -a -q)

Stop a container

To stop a container use

docker stop name

name example: gigantic_snyder

The PHP repository on Docker Hub is at https://hub.docker.com/_/php/