تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing Docker Compose0:00

OK, so we have a Dockerfile for an Nginx container, but that's not all we need to run a Laravel application. We also need containers for PHP and MySQL, and even for other things like Composer, NPM, or Redis. Managing all of those together is pretty difficult, which is why Docker Compose exists. Compose is a tool that's used to define and manage multi-container applications. It uses a YAML file to specify what containers we need, and then a variety of commands to build, run, and manage those containers that are all wrapped up in their own virtual network. It sounds a little complicated, but let's dive into it. In our terminal, the first thing that we need to do is create a file called docker-compose.yml.

Defining Nginx Service0:39

It sounds a little complicated, but let's dive into it. In our terminal, the first thing that we need to do is create a file called docker-compose.yml. Let's open it up. And at the top, we have to define a version for it. The current latest is 3.8, so that's what we'll set it to. Then we have to create a list of services. Each of these is a separate container, complete with their own image, exposed ports, and volumes. First, let's just start out with our Nginx service. Under the heading for it, we can specify what image that container is built off of. If we use the image tag, that pulls an image directly from the docker hub, like nginx-stable-alpine.

Under the heading for it, we can specify what image that container is built off of. If we use the image tag, that pulls an image directly from the docker hub, like nginx-stable-alpine. However, we built out a custom nginx image earlier, and we should be using that. So instead of image, we can use build. Under that, we specify a context, or the directory to look for a Dockerfile in. Ours is located in the same directory as this file, so we use the same directory. And then under that is the name of the Dockerfile to build it off of. Right now, that's just Dockerfile. But let's rename that to nginx.dockerfile in order to specify. Under the build heading, we can use the ports heading, and list out pairs of ports to connect.

But let's rename that to nginx.dockerfile in order to specify. Under the build heading, we can use the ports heading, and list out pairs of ports to connect from our local machine to the services container. Right now, that's just 80 and 80. Next a volumes heading, which also expects a list. This time of mount points from our local machine to the services container, like how we brought in the source HTML code in the last video. I'm going to bind the whole source directory in our project to /var/www/html. All right, that's it for this service. We have an image, ports, and volumes.

Running Compose Up2:44

All right, that's it for this service. We have an image, ports, and volumes. Let's spin this up and see how it looks. In our terminal, we just have to run docker-compose up. This will go through each of our service headings in the docker-compose file, pull down or build any images associated with each one, and provision the containers with our ports, volumes, and any other attributes. We can see that just like previously, our nginx container is up, and we're getting the output of the access and error logs streamed to our console. Let's bring this back down with Ctrl C, and open back up our docker-compose file to add

Adding Database Service3:18

output of the access and error logs streamed to our console. Let's bring this back down with Ctrl C, and open back up our docker-compose.yml file to add a new service. We have a web server, but we also need a database, like mysql. Instead of building from a custom Dockerfile, we'll use an image from the Docker Hub, mysql:5.7. If we want to be able to access this database while it's running with something, like a GUI program, we should expose its 3306 port to our system. Then we'll use an environment heading to specify some default values for our mysql user and default database. MYSQL_DATABASE set to laravel, MYSQL_USER also set to laravel,

user and default database. mysql_database set to Laravel, mysql_user also set to Laravel, mysql_password set to secret, mysql_root_password also secret, and there we go. Now we don't need any volumes for this service since we are not bringing any data from our local system over to it. But remember how I mentioned that volumes work both ways? Well in the case of our database, any data that's written to it will be wiped whenever the container is brought down. But we can create a volume that will also store that data on our local machine and allow

the container is brought down. But we can create a volume that will also store that data on our local machine and allow it to repopulate the database whenever the container is brought down and back up again. Create mysql directory in our project and bind it to the container's /var/lib/mysql directory. Let's save this file and create that directory. And we can spin both of these containers up just like we did earlier with docker compose up. It looks like I've run into a slight problem trying to pull this image on my local computer. I'm using the Apple M1 silicone which apparently doesn't play well with this mysql image. So instead let's go back to our docker-compose.yml file and change this mysql 5.7 instead to

Managing Services and Dependencies5:19

I'm using the Apple M1 silicone which apparently doesn't play well with this mysql image. So instead let's go back to our docker-compose file and change this mysql 5.7 instead to a drop-in replacement MariaDB 10.5. Going back to our terminal, let's try running docker compose up again. Alright now you can see that we have outputs for both nginx and mysql containers streamed to our console. And if we open up another terminal window and run docker ps, we can see those containers are active and listed out here. We can also run docker compose ps and get a different formatted list. Only those containers associated with our docker-compose file as well as their current

We can also run docker compose ps and get a different formatted list. Only those containers associated with our docker-compose.yml file as well as their current status and any ports exposed. Sometimes though you might not want to bring up every container at once. Instead you can specify based on the service names in your docker-compose.yml file. For instance if I just wanted to start the nginx service, docker compose up nginx. Some of your containers might rely on other services needing to be up before they can be ran. For example, in our application, the database service should be running and available before the web server is started.

For example, in our application, the database service should be running and available before the web server is started. Opening up our docker-compose.yml file again, this is easy to specify. At the bottom of our nginx service, we'll add a new heading called depends_on. This expects a list of other service names. And these will be brought up before the nginx service is started. So we'll add mysql to this list. Now if we go back and run docker compose up nginx, we can see that the nginx service is running and the output is streamed to us. But if we open up a new window and run docker compose ps, then mysql service is brought.

is running and the output is streamed to us. But if we open up a new window and run docker compose ps, then mysql service is brought up as well running in the background. In the next video, we'll add a php container to our docker compose setup, rounding out the full LEMP stack needed to run Laravel.

Docker Compose

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟