Docker+PHP+Nginx: Part 1

This tutorial shows a way to set up Docker, PHP and Nginx, and creates files and writes log(writing permission in docker is a complex problem) in the php container.

It is split into two parts. Part 1 aims to show how to get the whole thing running, Part 2 will focus on two important points:

  • php-fpm's writing permission in docker container
  • fastcgi_param: 'SCRIPT_FILENAME' in nginx.conf

This tutorail's code is put at GitHub

The code works with:

  • macOS
  • Linux

Command to execute

  1. Clone the repository, and execute the following command inside the cloned repository's directory
  2. Open the terminal and check the directory and files permission: ls -l, and you should see the following:
    docker-php-nginx-3
    The above image shows that the files belong to user 'ec2-user' and group 'ec2-user'
  3. Check the belonged user's uid(user id) and gid(group id), which are number the kernel to recognize user and group(replace 'ec2-user' with the name shows in your terminal ): id ec2-user, and it should shows things like this:
    docker-php-nginx-4
    So the uid is 1000, gid is 1000
  4. Edit the .env.dev file, replace the 1000 with your uid and gid, you can also change "USER_NAME", "GROUP_NAME" to something you prefer(they don't have to be the same with the files' belonged username)
  5. Start containers: sudo docker-compose --env-file ./.env.dev up nginx -d --no-deps --build app. (Make sure you have docker, docker-compose installed)
    • --no-deps --build <service name>: make sure every time the app container start with new params in .env.dev file, otherwise the updated params in env file won't take effect
  6. Once you start the containers successfully, open "localhost:8000" in your browser, you should see the following page:
    docker-php-nginx-2
  7. Stop containers: sudo docker-compose down