NGINX – Docker Development Environment (DDE Part 6)


If you have made it this far, nice job! Now, let’s create out NGINX container.

Let’s start by adding the following service in our docker-compose.yml file below the php-fpm service we created in the last post.

            context: ./docker/nginx
                PROJECT_NAME: ${PROJECT_NAME}
                WEBSITE_DIR: ${WEBSITE_DIR}
        restart: always
            - php-fpm
            - php-fpm
            - ./logs/nginx:/var/log/nginx
            - DOMAIN_NAME=${DOMAIN_NAME}
            - PHP_FPM_PORT=${PHP_FPM_PORT}
            - "${HTTP_PORT}:${HTTP_PORT}"
            - "${HTTPS_PORT}:${HTTPS_PORT}"

By now, this should make sense to you. Here, we want to have a link to the php-fpm container so we can use PHP on our web server and use the volumes from php-fpm so we have files that we can serve with the web server.

To get started, copy the Dockerfile,, and nginx.conf files from here into the docker/nginx directory. If you haven’t already, create a directory called conf.d in the docker/nginx directory and copy the files from here into it.


For the sake of sanity and brevity, we are only going to look at some key points in the Dockerfile.


In order to be able to use value that we have defined in the .env file, we need to assign those values to environment variables.

ARG variables are passed to the Dockerfile and are only available while the container is being built. Once the container is built, they are no longer available.

ENV variables are used once the container is built so can be used inside scripts that are run inside the container. But, they are unavailable while the container is being built.

COPY /usr/local/bin/
RUN chmod +x /usr/local/bin/

CMD ["/usr/local/bin/"]

The above does the following:

  1. Copies the script into the container.
  2. Makes sure that we can execute the script.
  3. Runs the script.

We are almost done! Let’s take a look at the file to make sure you know what’s going on.

if [ ! -d "/etc/nginx/sites-enabled" ]; then
    mkdir /etc/nginx/sites-enabled

This creates the sites-enabled directory if it doesn’t already exist.

if [ -n "$APPLICATION_TYPE" ]; then
    if [ "$APPLICATION_TYPE" = "drupal8" ]; then
        cp /etc/opt/docker-files/drupal8.conf /etc/nginx/sites-enabled/${PROJECT_NAME}.conf
        cp /etc/opt/docker-files/symfony.conf /etc/nginx/sites-enabled/${PROJECT_NAME}.conf

If the environment variable, $APPLICATION_TYPE, is defined, copy the appropriate configuration file into sites-enabled.

sed -i "s/#DOMAIN_NAME#/${DOMAIN_NAME}/g" /etc/nginx/sites-enabled/${PROJECT_NAME}.conf
sed -i "s/#PROJECT_NAME#/${PROJECT_NAME}/g" /etc/nginx/sites-enabled/${PROJECT_NAME}.conf
sed -i "s/#PHP_FPM_PORT#/${PHP_FPM_PORT}/g" /etc/nginx/sites-enabled/${PROJECT_NAME}.conf
sed -i "s/#WEBSITE_DIR#/${WEBSITE_DIR}/g" /etc/nginx/sites-enabled/${PROJECT_NAME}.conf

Using inline replacement, we replace the defined placeholders with the appropriate value.

exec nginx -g "daemon off;"

This starts the NGINX web server.

Now run the following to build our web server container:

$ docker-compose up --build -d

That’s it! You now have a docker-based development environment with PHP-FPM, MySQL, and NGINX containers. 

Name               Command                 State        Ports
mysqldb_1 mysqld    Up>3306/tcp
nginx_1      /usr/docker/bin/docker ...     Up>80/tcp, 
php-fpm_1    docker-php-entrypoint /usr     Up>22/tcp,

At this point, you could create a directory called web in the project directory and add a PHP page to it. You can then visit http://project.local in your browser and see the page.

Next, we will discuss some useful commands to get started using this environment.

Leave a Reply

Your email address will not be published. Required fields are marked *