NGINX – Docker Development Environment (DDE Part 6)
NGINX
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.
nginx: build: context: ./docker/nginx args: APPLICATION_TYPE: ${APPLICATION_TYPE} PROJECT_NAME: ${PROJECT_NAME} WEBSITE_DIR: ${WEBSITE_DIR} restart: always links: - php-fpm volumes_from: - php-fpm volumes: - ./logs/nginx:/var/log/nginx environment: - VIRTUAL_HOST=${DOMAIN_NAME} - 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, docker-entrypoint.sh, 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.
Dockerfile
For the sake of sanity and brevity, we are only going to look at some key points in the Dockerfile.
ENV APPLICATION_TYPE=${APPLICATION_TYPE} ENV PROJECT_NAME=${PROJECT_NAME} ENV WEBSITE_DIR=${WEBSITE_DIR}
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 docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh CMD ["/usr/local/bin/docker-entrypoint.sh"]
The above does the following:
- Copies the docker-entrypoint.sh script into the container.
- Makes sure that we can execute the script.
- Runs the script.
docker-entrypoint.sh
We are almost done! Let’s take a look at the docker-entrypoint.sh file to make sure you know what’s going on.
if [ ! -d "/etc/nginx/sites-enabled" ]; then mkdir /etc/nginx/sites-enabled fi
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 else cp /etc/opt/docker-files/symfony.conf /etc/nginx/sites-enabled/${PROJECT_NAME}.conf fi fi
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 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp nginx_1 /usr/docker/bin/docker ... Up 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp php-fpm_1 docker-php-entrypoint /usr Up 0.0.0.0:2222->22/tcp, 0.0.0.0:9000->9000/tcp, 0.0.0.0:9001->9001/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.