1

I'm just starting to get my head around docker and want to use it for a project.

I have followed https://docs.docker.com/docker-for-mac/#explore-the-application-and-run-examples and have NGINX running fine and can see the NGINX landing page.

Do I need to install php-fpm and mySQL within my container since my container is only NGINX at this stage?

How do I configure my project on a custom domain e.g. project.dev. Do I need to edit an entry in /etc/hosts for 127.0.0.1 project.dev and then listen for that URL in an NGINX config?

Lastly do I need a dockerfile? I already have my container up and my understanding is a dockerfile is only for defining your container?

An example of a dockerfile for NGINX, PHP and mySQL would be helpful to look at as well.

Thanks

1
  • I already have PHP and MySQL install on my mac so can this be shared by the docker container? Commented Jul 3, 2017 at 2:14

1 Answer 1

3

No, this guide just show using nginx container in docker. But I see the container don't have php installed. And you cannot install php-fpm inside this container.

So, if you want to use nginx, php, and MySQL using docker you should pull:

  1. Container which run Nginx + PHP-FPM (I recommend this image https://hub.docker.com/r/richarvey/nginx-php-fpm/)
  2. Container run MySQL (https://hub.docker.com/_/mysql/)

Download images

docker pull richarvey/nginx-php-fpm
docker pull mysql:5.6

Run MySQL Instance. Name it mysql56, and expose using port 3360

docker run -tid -p 3360:3306 --name mysql56 -e MYSQL_ROOT_PASSWORD=123456 -v /root/docker/mysql56/data/mysql:/var/lib/mysql  -d mysql:5.6

Run Nginx PHP+FPM instance. Link it to MySQL Instance, and name it project-dev

docker run -tid --name project-dev --link mysql56:mysql -v $(pwd):/var/www/html -p 8888:80 richarvey/nginx-php-fpm:latest

Run docker ps -a to see the running containers.

To make nginx can be accessed with address project.dev, just map it on /etc/hosts. Then access it on web browser http://project.dev:8888

Note:

  • -v /root/docker/mysql56/data/mysql:/var/lib/mysql it mean I have /root/docker/mysql56/data/mysql on my mac, and map it to /var/lib/mysql in mysql56 container. So all mysql data will be backup on my local data, and will not lose when I remove the container.
  • -v $(pwd):/var/www/html mean your current directory will be mapped to container. So, whatever you write in this directory will be exist on /var/www/html container.
  • I use port 8888 to avoid conflict with existing web server, you can change it as you want
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! Will give it a go
Is /root/docker/mysql56/data/mysql your default mysql install? Do I need to install mysql on my machine? I currently have it install via MAMP but I assume it isn't a good idea to map that version of MySQL?
oh no, I just create it manually to make data persitent in my local. So when container destroy, data will be keep on my local. You can custom the path as you want. And you don't need to install mysql on your local. I'm not sure, nginx php+fpm container able to connect with mysql on your local. So, just use docker container which run mysql like I create in guide above.
No, just not use -v /root/docker/mysql56/data/mysql:/var/lib/mysql option
FYI, to make connection from your PHP script to MySQL, please use mysql as the hostname. Because we use mysql as the name when link nginx php+fpm container (--link mysql56:mysql )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.