Starting Nginx Dockerfile0:00
We've got the basics down for what it takes to create our own Docker image using a Dockerfile. Let's put that to use and start building one that can support a Laravel application, starting with Nginx. Let's open up our Dockerfile and remove everything that we have in here now. Our goal here is to create a container that's capable of running a web server, and we'll also process requests to the PHP container that we'll be adding in later. First we need to determine what base image we're going to build off of. Nginx has ones readily available in the Docker hub, and so we're going to use the latest stable alpine version of it.
Nginx has ones readily available in the Docker hub, and so we're going to use the latest stable alpine version of it. Alpine in this case means the image is based on Alpine Linux, a lightweight distro that works well with containers. So in our Dockerfile, from nginx:stable-alpine. After that, let's add a line to create the folder that our site source will be attached to. Run mkdir -p, and that's /var/www/html. We also need to pull in a config file to set up our site's root path, server name, and some other attributes.
Configuring Nginx User1:07
We also need to pull in a config file to set up our site's root path, server name, and some other attributes. For that we can use add, and copy a local file in nginx default.conf to the /etc/nginx/conf.d directory. And to wrap up this container image, we need to adjust what user the nginx process runs on. This is controlled through a pretty lengthy nginx config file. Instead of saving it locally to just change a single line, we can use the run keyword to do the work for us. Run sed, which stands for stream editor, and is a text transform utility in Linux, i, and
to do the work for us. Run sed, which stands for stream editor, and is a text transform utility in Linux, i, and then a string representing what we're replacing. This s/ is the substitute command, and expects a string to find, and a string to replace it with. I want to find the default user, www-data, and replace it with some different user, like laravel. We'll then make this user with run adduser -G, passing in the group to create or attach this user to, laravel, and then s/bin/sh, attaching it to the alpine linux shell, the -d flag, and then the user's name, laravel.
Creating Nginx Site Config2:15
this User to, Laravel, and then /bin/sh, attaching it to the alpine Linux shell, the -d flag, and then the user's name, laravel. Instead of hardcoding these values in, I can create some ENV values at the top of the Dockerfile to hold them. And then replace the hardcoded group and usernames with the ENV values like this. Actually, this directory should be /var/www/html/public, since that is what the directory will be for the root of our web server. Let's save this file and create our nginx configuration file. In an nginx directory, we'll create default.conf, and open it up. I'm going to paste in the standard boilerplate configuration, but the important thing is
Building and Running Image3:06
In an nginx directory, we'll create default.comp, and open it up. I'm going to paste in the standard boilerplate configuration, but the important thing is that we're going to change the root directory to /var/www/html/public, since that's what's expected from a Laravel application. In our terminal, we can run docker build --no-cache -t laravel-nginx ., followed by the current directory to build our Dockerfile into a usable image. And once it finishes up, let's run it. Enter docker run --rm -p 80:80 laravel-nginx. The container will remain running until we hit Ctrl-C, and we also get the error and access logs streamed to the output in front of us.
Mounting Source with Volumes3:52
The container will remain running until we hit ctrl-c, and we also get the error and access logs streamed to the output in front of us. Now opening up the browser, we can access this nginx container by heading to localhost. Uh oh, except it's giving a 403. Well the reason for that is that we don't have any website source code available for nginx to use in that public directory. We can change that though by using something called a volume. In docker, volumes are virtual mounted file systems that connect files and directories from the host machine docker is running on to ones in a specific container or containers. Let's stop this container with ctrl-c, and create a directory in our project called source.
from the host machine docker is running on to ones in a specific container or containers. Let's stop this container with ctrl-c, and create a directory in our project called source. Instead of that, we'll create a file called index.html, and open it up. In there, I'll just add a title that says hello from the nginx container. Going back to our terminal, and starting up our nginx container again, docker run --rm -p 8080, but this time with a -v flag. We can pass in a volume name, or full local path, and then a target directory into the container. For the local mount point, that's the full project directory plus source. So for me, users/andrew/sites/laraveldocker.tests/source, then a colon, and the path we want.
For the local mount point, that's the full project directory plus source. So for me, users, andrew, sites, laraveldocker.tests, source, then a colon, and the path we want to connect this to into the container, /var/www/html/public. Open the container name, laravel-nginx. And once the container is back up again, we can visit localhost, refresh, and we're seeing the heading that we applied in our index.html file, which means that data is flowing from our local machine into the container. The best part about volumes is that their data is modifiable in real time. We can open up the index.html file again, and add a few lines, and then save this file, go back to our browser, refresh, and see those changes happen immediately.
Recap and Next Steps5:55
We can open up the index.html file again, and add a few lines, and then save this file, go back to our browser, refresh, and see those changes happen immediately. This also works both ways. If any files in the volume are changed by software running in the container, you'll see those update in real time on the host machine. Alright we have our nginx Dockerfile ready to use. In the next video, we'll introduce Docker Compose and show off how it'll be used in the rest of this series.
