Docker – Linked PHP and MySQL Containers


I’ve been wanting to try out Docker for some time now and have never had a good enough reason to down-tools and give it a good go. It’s actually a lot simpler to set things up than I expected – but it took a while to figure everything out. There are lots of guides online but none that were explaining exactly what I needed. This is here so I can remind myself of the steps needed to get to this point.

Below is a list of the commands and info needed to get Docker working – with one Docker for a MySQL database, and another for Apache/PHP.

Im porting an existing LAMP application over to Docker, so this guide requires that you have a working web-application’s code and data. You will need a backup of the data, and you will need to have PHP code that has no external sym-links. Since we will ‘share’ a folder with Docker, Docker will not be able to see anything outside of that shared folder.

You can get the latest version of Docker from

Docker set up Guide:

  • Install Docker (download latest from their website).
  • If you are running this on Linux, restart your Terminal, otherwise Open a docker terminal (not the regular terminal).
  • If you are not using Linux, you should be informed of the Docker host IP address. Make a note of it.
  • On Linux, the services will run on localhost so if you have conflicting services (ie. you are already running Apache or MySQL) then you need to stop them, modify their ports, or modify the ports we expose for the new services. This can be done in the command line when you start it up.
  • I need one Docker with Apache and PHP and another with MySQL.
  • To get the Apache/PHP Docker execute:
docker pull nimmis/apache-php5
  • Then to run that Docker – you execute this command:
docker run -tid -p 80:80 nimmis/apache-php5
  • Then to check it is running you can do this: docker ps
  • If you open your browser and load up the Docker host IP (localhost on Linux) you should now see the default Apache page.
  • We need to make some changes to this, so get the CONTAINER ID using the docker ps command and then run:
docker stop {container_id}
  • If I want to share a folder (eg. a vhost containing PHP code) I can use the -v argument to specify a folder to share. In this case, I want to share an existing PHP project as the /var/www/html folder (Apache’s default webroot) on the Docker.
docker run -tid -p 80:80 -v ~/Code/www/2016-02-full:/var/www/html nimmis/apache-php5
  • To get the database working, I took a DB dump of some cetasql data – and uploaded it to a public area on a website. This is simply so I can download it from the docker via curl later on.
  • Make sure you stop the PHP Docker again now:
docker stop {container_id}
  • For now – lets remove it completely using:
docker rm {container_id}
  • Now we need to get a MySQL docker – and that is done with:
docker pull mysql/mysql-server:5.6
  •  This time we are going to give it a name using the --name argument. Here I am calling it “mysqlserver”. You can also see I have set an environment variable using the-e argument. This is then used to set up the MySQL root user’s password. I presume this is done via scripts on the Docker (I am still figuring this out).
docker run -p 3306:3306 --name mysqlserver -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server:5.6
  • Now we need to start the PHP server back up, but we need to allow it access to the MySQL server.
  • This is the new command for starting the PHP docker. I have named this Docker “appserver”.
docker run -tid -p 80:80 -v ~/Code/2016-02-full:/var/www/html --name appserver --link mysqlserver:mysqldb nimmis/apache-php5 
  • The --link section is what allows this docker to talk to other dockers specified (eg. mysqlserver), otherwise they are all kept separate.
  • If both of these Dockers are up and running, you can find out the internal IP address of each of them by running this command. This gets the IP for mysqlserver.
docker inspect mysqlserver | grep IPAddress
  • In the results you will see the IP.
  • Once you have that, you can simply set up your site’s database configuration so that it points to the MySQL database.
  • Once done you can log in to the MySQL docker’s terminal – using this command:
docker exec -i -t mysqlserver bash
  • Then from within the bash terminal you can download the mysql backup file using curl {url} > {filename}.
  • Since this is a brand new database server you might need to create the database too. Log in to mysql to do that and issue the create database your-db-name command
  • Once working, you need to import the data dump using regular MySQL commands. eg:
mysql -u root -p your-db-name < db.sql

You should now be up and running with 2 Docker containers. One running Apache/PHP and the other running MySQL. Visit the host IP address in your browser to see your site!

If you’re running on a Mac or Windows, you can halt your Docker VM by running this command on your workstation (exit out of the Docker if you are still in there!):

docker-machine stop

Good luck with this. Any issues – let me know so I can correct the guide.