Why docker?

When the computer was still a Window system, it often needed to build a set of environment based on VM services for better development (should be closer to the online environment), but now under the mysterious aura of Docker, it has been possible to build a local development environment with minimal resources, and at the same time, it can be better migrated to other places.

Front knowledge

  • Understand docker installation and use
  • Learn about the docker-compose command line
  • Understand laravel installation and use

This paper mainly uses Laradock for local docker configuration. Laradock is already integrated with the environment laravel needs to use, and with simple configuration changes, you can build an environment for development, which is a great recipe for development and management.

To clarify, in the Docker environment we need to run the Laravel project, which actually creates several containers:

  • Workspace development environment
  • Php-fpm (PHP support)
  • Nginx (Web Services)
  • Mysql (database)

These containers are generated based on laradock reprocessing. Please refer to the directory of Laradock, which contains Dockerfile and related configuration

For more information, go to the official Laradock documentation.

To prepare

Install docker first on Windows

Run the docker command after several restarts to test it.

Then in your project directory using the git pull level https://github.com/laradock/laradock.git this package.

D:/ WWW / - /laradock - /my-projectCopy the code

This prepares you for the next step.

configuration

The environment configured here is configured as the common environment

  • nginx
  • PHP 5.6 – FPM
  • Mysql 5.6

Next you need to modify the laradock/.env file

/www/laradock
 - .envCopy the code

Env file does not exist in the laradock directory, so we need to copy it from env-example.

# /www/laradock

$ cp env-example .envCopy the code

Modifying the PHP version

Env file, find PHP_VERSION and change PHP version to 56 (default 71, 71, 70, 56).

# /www/laradock/.env
### PHP Version

PHP_VERSION=56Copy the code

Modify the Mysql version and configuration

Change MYSQL version to 56 (default 8, optional 8, 5, 5.6, 5.5).

Other Settings need to be filled out according to your personal needs. In general, you need to modify MYSQL_USER, MYSQL_PASSWORD, and MYSQL_ROOT_PASSWORD to ensure links.

### MYSQL MYSQL_VERSION=5.6 # MYSQL_DATABASE MYSQL_USER=packy # MYSQL_PASSWORD MYSQL_USER=packy # MYSQL_USER=packy # MYSQL_PASSWORD MYSQL_PASSWORD=123456- # MYSQL_PORT access port (default: 3306) MYSQL_PORT=3306 # MYSQL_ROOT_PASSWORD Root password The default is root MYSQL_ROOT_PASSWORD=23333- MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.dCopy the code

About Mysql version selection

Mysql uses an official image, so you can use hub.docker.com to check which versions are included in the official image. How you choose depends on your needs.

Modify the nginx configuration

In general, you don’t need to change anything, just use the default. To configure the site, go to laradock/nginx/sites.

If you need to modify the port, enter
.envFile to find
NGINXModify.

/ WWW /laradock/. Env ### NGINX NGINX_HOST_HTTP_PORT=80 NGINX_HOST_HTTPS_PORT=443 # NGINX_HOST_LOG_PATH: /logs/nginx/ # NGINX_SITES_PATH =./logs/nginx/ # NGINX_SITES_PATH The default location is laradock/nginx/sites/ NGINX_SITES_PATH=./nginx/sites/ NGINX_PHP_UPSTREAM_CONTAINER=php-fpm NGINX_PHP_UPSTREAM_PORT=9000Copy the code

About Nginx configuration

Nginx configuration files are stored in laradock/nginx/sites. If you want to create a new website, copy the corresponding. Example file and rename it to. Note: Only.conf files are loaded under nginx.

Here I copy laravel.conf.example as an example and rename it to my-project.conf:

# laradock/nginx/sites/my-project.conf server { listen 80; listen [::]:80; # domain name, change to your domain name server_name my-project.com /var/ WWW / Root /var/www/my-project; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_pass php-upstream; fastcgi_index index.php; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } location /.well-known/acme-challenge/ { root /var/www/letsencrypt/; log_not_found off; } error_log /var/log/nginx/laravel_error.log; access_log /var/log/nginx/laravel_access.log; }Copy the code

At the same time, modify the hosts of the host (that is, the local machine of the Window)

# C: \ Windows \ System32 \ drivers \ etc \ hosts 127.0.0.1 at my-project.comCopy the code

Try to run

The preparations are almost complete at this point.

Run the following command to install and use the software. The pulling time is slow because it is from a foreign source.

docker-compose up -d nginx mysqlCopy the code

Enter docker ps to see how the container works. Everything is ok!!

Try visiting http://my-project.com to see the results. PHP files can be accessed normally.

Run laravel

Not to mention the workspace container, which serves as a workspace for various tools (including: PHP CLI, Composer, Git, Linuxbrew, Node, V8JS, Gulp, SQLite, xDebug, Envoy, Deployer, Vim, Yarn, etc.)

So how do you use these features?

First of all to enterworkspaceThe container

# /www/laradock
docker-compose exec workspace bashCopy the code

Composer for domestic source

After entering the container, the default is /var/www project directory. Because the composer uses a foreign source, it is slow, so you need to switch to a domestic source.

composer config -g repo.packagist composer https://packagist.phpcomposer.comCopy the code

Install laravel

Here we need to install Laravel in the my-project directory.

* Note: All contents in my-project need to be cleared in advance

Composer creat-project laravel/ Laravel my-project2 "5.2.*" && CD my-project && PHP Artisan Key: GenerateCopy the code

Configure and restart nginx

After the installation is complete, change the site directory address in laradock/nginx/sites/my-project.conf file slightly

server { listen 80; listen [::]:80; server_name my-project.com; Root /var/ WWW /my-project/public; index index.php index.html index.htm; . }Copy the code

Restart the nginx container

# /www/laradock
docker-compose restart nginxCopy the code

About Database Services

DB_HOST= mysql_host = mysql_host = mysql_host DB_PORT=3306 # database DB_DATABASE=default # user DB_USERNAME=packy # Password DB_PASSWORD=123456-Copy the code

Some questions:

Q: ERROR message ERROR: for laradock_mysQL_1 Cannot create container for service mysql: Drive sharing seems blocked by a firewall

A: First, suspend the defense process of your local antivirus program.


Q: ERROR message ERROR: for laradock_nginx_1 Cannot start service nginx: driver failed programming external connectivity on endpoint laradock_nginx_1 (6e4f4761d30f4cd80c44c6b0fddfbd4ef0324529099aace02bee6a6653ce453a): Error starting userland proxy: Bind for 0.0.0.0:443 failed: Port is already allocated

A: I suggest that you switch the port. I have tried to make it work normally as 1443, and it can only be handled in this way at present.


# .env
### NGINX

NGINX_HOST_HTTPS_PORT=1443Copy the code

Q: Why does the website directory have to start with /var/www?

A: The nginx container is accessed by the website. The /var/www directory is where the website directory is stored. Because the configuration mounts the local directory to the /var/www directory when the container is created, you can access the local code. This setting can be found in laradock/. Env.