Home Creating a Docker image with Ubuntu, NGINX and PHP
Post
Cancel

Creating a Docker image with Ubuntu, NGINX and PHP

Set up

  • Create a new directory and change into it
  • Create a new directory called ‘webroot’
  • Create a new directory called ‘config’
  • Create ‘webroot/phpinfo.php’
1
2
3
<?php
  phpinfo();
?>
  • Create a file called config/default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;


        location ~* \.php$ {
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }


}
  • Create a file called Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Pinning to Ubuntu 20.04. v22 has some qwearks right now.
FROM ubuntu:20.04

# Set timezone
ENV TZ=America/New_York
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Updating and Upgrading
RUN apt-get -y update
RUN apt-get -y upgrade

# Installs happen now
RUN apt-get -y install nginx
RUN apt-get -y install php php-cli php-fpm
RUN apt-get -y install htop nano
RUN apt-get -y autoremove

RUN sed -i 's/cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.4/fpm/php.ini

# Copy config files into container
COPY config/default /etc/nginx/sites-available/default
COPY webroot/* /var/www/html/

# Expose port
EXPOSE 8080/tcp

# Start services and hang out
ENTRYPOINT service php7.4-fpm start && service nginx start && bash

Build the Docker image

1
docker build --tag "nginx-php" .

Run the container

1
docker run -dit --name nginx-php -p 8080:80 nginx-php
This post is licensed under CC BY 4.0 by the author.