In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use Docker to build a Laravel development environment", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Docker to build a Laravel development environment.
Step 1: get the source package of laravel
Because we don't have composer installed on our computers, we can't use composer to create laravel projects. Here, I use curl to download the latest laravel source package directly from github. You can also use wget or git clone to get the source package.
Curl-l-o https://github.com/laravel/laravel/archive/v5.5.0.tar.gz / & & tar-zxvf v5.5.0.tar.gz / & & rm v5.5.0.tar.gz
The above command decompresses the source code package after curl downloads the source code package, deletes the source code package v5.5.0.tar.gz after decompression, and you will see a laravel-5.5.0 project directory after execution.
Step 2: add docker-compose.yml
Create a docker-compose.yml file in the project.
The compose project is the official open source project of docker, which is responsible for the rapid orchestration of docker container clusters. We know that using a dockerfile template file makes it easy for users to define a separate application container. Here we will use four containers to put php, mysql and nginx in four different containers, and associate the four application containers together to form a project through compose.
The beginning of the orchestration file is as follows:
Version: '2'services: # our services will go here
In the orchestration file, each container is called a service, and all the services (that is, containers) used in the entire application are defined under services.
App service
The container of the app service will execute the code in our project.
App: build: context:. / dockerfile: app.dockerfile working_dir: / var/www volumes: -. /: / var/www environment:-"db_port=3306"-"db_host=database"
Notes:
We use the app.dockerfile image file to build our app container, in which we will image the configuration of the php module used in the project, and install additional npm to build the front-end code.
Working_dir: / var/www sets the working directory to / var/www. In the container, the project code will be placed under the / var/www directory, including commands executed using docker exec app will also have / var/www as the current working directory.
Volumes is the mount path setting of the data volume in the container. Here, we define only one data volume and attach the host project directory to the / var/www in the container, so that the changes we make to the project code on the local computer will be synchronized to the container immediately, and vice versa. The changes made to the code in the container will also be fed back to the local computer project in time.
Environment sets the name of the environment variable. Here we set db_port and db_host so that there is no need to modify the values of these two items in the .env file in the project. Of course, any environment variable that you need to set separately in the development environment can be written here. The dotenv used by laravel to read the configuration will check whether the system has the specified environment variable setting, and if so, it will not read the .env file.
Now we need to create the app.dockerfile file mentioned in the build section above, which is as follows:
From php:7.1.22-fpm# update packagesrun apt-get update# install php and composer dependenciesrun apt-get install-qq git curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev# clear out the local repository of retrieved package files# run apt-get clean# install needed extensions# here you can install any other extension that you need during the test and deployment processrun apt-get clean Docker-php-ext-install pdo pdo_mysql mcrypt zip gd pcntl opcache bcmath# installs composer to easily manage your php dependencies.run curl-- silent-- show-error https://getcomposer.org/installer | php- install-dir=/usr/local/bin-- filename=composer# install noderun apt-get update & &\ apt-get install- y-- no-install-recommends gnupg & &\ curl-sl https://deb.nodesource.com/setup_10.x | bash-& & Apt-get update & &\ apt-get install- y-- no-install-recommends nodejs & &\ npm config set registry https://registry.npm.taobao.org-- global & &\ npm install--global gulp-clicmd php-fpm
Notes:
I first put npm and composer into an app container, because they often need to be executed during development. If you publish to a production environment, you usually use a separate composer to build the project code instead of putting it in a container running the application. One of the core ideas of the container is to keep it single, so that containers with the same role can be quickly added.
Web service
Next, we need to configure a web server to use, and we name the container web in the orchestration file
Web: build: context:. / dockerfile: web.dockerfile working_dir: / var/www volumes_from:-app ports:-8080
Notes:
Volumes_from is used to reuse the data volume paths defined in the app service
Port 8080 of the local computer is mapped to port 80 of the web container through ports, so that we do not need to set up the hosts file in the development environment, but can access the service directly through ip plus port.
The web server uses nginx, so we need to use a nginx image to build this container. Before that, we need to set the vhost used in the project based on the nginx image, so we need a web.dockerfile file, which is defined as follows:
From nginx:1.10add vhost.conf / etc/nginx/conf.d/default.conf
According to the definition of the image file, we copied the vhost.conf in the project to the / etc/nginx/conf.d/default.conf of the container, so that the basic nginx configuration is configured, as defined in vhost.conf:
Server {listen 80; index index.php index.html; root / var/www/public; location / {try_files $uri / index.php?$args;} location ~\ .php$ {fastcgi_split_path_info ^ (. +\ .php) (/. +) $; fastcgi_pass app:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param script_filename $document_root$fastcgi_script_name; fastcgi_param path_info $fastcgi_path_info }}
Notes:
Because it is a development environment, we only make the simplest configuration and do not consider tuning.
Fastcgi_pass app:9000; nginx passes the request for php to port 9000 of the app service through fastcgi. Docker-compose automatically connects the container services defined in services, and each service uses service names to reference each other.
Mysql service
Next, we will configure the mysql service. Unlike the above two services, in the container of php-fpm and nginx, the files of our local computer can be synchronized into the container for container access, which allows us to quickly get feedback on the changes made to the files in the container to speed up our development process. But in the database container, we want the files created in the container to be persistent (when the default container is destroyed, the files created in the container will also be destroyed). We can achieve the above function through the docker data volume, but this time we no longer have to hang the files from the local computer to the data volume, the docker client will manage the specific storage location of the created data volume on the local computer.
Here are the settings for the database service in the orchestration file
Version: '2'services: database: image: mysql:5.7 volumes:-dbdata:/var/lib/mysql environment:-"mysql_database=homestead"-"mysql_user=homestead"-"mysql_password=secret"-"mysql_root_password=secret" ports:-"33061 virtual 3306" volumes: dbdata:
Notes:
At the bottom of the file, we create a data volume named dbdata with the volumes command (the colon after dbdata is intentionally written, which is a syntax limitation of the yml file, so don't worry too much about it)
After defining the data volume, we use the format of: above to notify docker and hang the dbdata data volume on the / var/lib/mysql directory in the container
Set in environments are the four necessary parameters required for mysql's docker image.
In the ports port mapping, we map port 33061 of the local computer to port 3306 of the container so that we can connect to the mysql in the docker through the database tool on the computer.
Choreograph all services together
Here is the complete docker-compose.yml file. By orchestrating the file, we associate three application containers together to form the server side of the project.
Version: '2'services: # the application app: build: context:. / dockerfile: app.dockerfile working_dir: / var/www volumes: -. /: / var/www environment:-"db_port=3306"-"db_host=database" # the web server web: build: context:. / dockerfile: web.dockerfile working_dir: / var/www volumes_from:-app ports:-8080 80 # the Database database: image: mysql:5.6 volumes:-dbdata:/var/lib/mysql environment:-"mysql_database=homestead"-"mysql_user=homestead"-"mysql_password=secret"-"mysql_root_password=secret" ports:-"33061 dbdata 3306" volumes:
Start the service
After configuring the orchestration file and the specified docker image file according to the above steps, we can start the service with the following command, and then start the three services defined in the above file.
Docker-compose up-d
When starting for the first time, because the docker client needs to download the three images mentioned above and build the service, the startup speed will be slower. After the image is downloaded and the build is completed, the subsequent startup will be very fast.
Initialize the laravel project
After starting the service, we can initialize the laravel project. The steps are the same as those described in the official documentation, but need to be executed in the container of the launched app service:
Docker-compose exec app composer installdocker-compose exec app npm install / / execute the command docker-compose exec app cp. Env. Example. Envdocker-compose exec app php artisan key:generatedocker-compose exec app php artisan optimizedocker-compose exec app php artisan migrate-- seeddocker-compose exec app php artisan make:controller mycontroller if the front-end project is included
Notes:
Docker-compose exec sends the command to the specified container for execution
App is a service defined in docker-compose.yml, which is a container running php-fpm
Php artisan migrate is a command to be executed in the container
How to view the nginx log:
Docker ps found the container id of the nginx service
Docker exec-it
< contianer id >/ bin/bash enters the nginx container
For the specific path of the nginx log, please see the vhost.conf in the project.
After executing the above command, you can access the project through http://127.0.0.1:8080/.
At this point, I believe you have a deeper understanding of "how to build a Laravel development environment with Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.