Mac下安装LaraDock

Mac下安装LaraDock

[TOC]

mac下安装docker

下载地址:https://store.docker.com/editions/community/docker-ce-desktop-mac

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
docker version 

Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:21:31 2018
OS/Arch: darwin/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:29:02 2018
OS/Arch: linux/amd64
Experimental: true

设置阿里云镜像加速

  1. dev.aliyun.com 注册帐号

注意:点击右上角的管理中心,而不是控制台,点控制台就跑阿里云的后台了。

  1. 点管理中心,进入后台,如果第一次,则会提示开通服务及设置密码,点确认即可,然后根据提示操作即可

3.按提示进行操作即可

  1. 最终效果图

  1. 验证
1
2
3
4
5
6
7
8
9
docker info 

... 略

Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://ac4aiz13.mirror.aliyuncs.com/
Live Restore Enabled: false

下载 laradock

官方文档https://laradock.io

1
2
3
4
5
6
7
8
9
10
git clone https://github.com/laradock/laradock.git

cd laradock

cp env-example .env

vi .env

# 代码目录
APP_CODE_PATH_HOST=../php-workspace/

这里解释下目录结构

1
2
3
4
5
6
laradock #laradock的目录

php-workspace # 存放代码的目录
project1 #项目1
project2 #项目2
...

其它的项目结构,可以参考官方文档:https://laradock.io/getting-started/,这里提供了单个项目的结构,及多个项目的结构

1
2
3
cd laradock

docker-compose up -d nginx mysql redis workspace

配置虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cd /laradock/nginx/sites

cp laravel.conf.example site1.test.com.conf

vi site1.test.com.conf

server {

listen 80;
listen [::]:80;

server_name site1.test.com; #1. 设置下域名
root /var/www/site1.test.com/public; #2. 修改为相应的目录
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;
#fixes timeouts
fastcgi_read_timeout 600;
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;
}
  • 修改文件中的1,2两点即可,
  • 设置host 127.0.0.1 site1.test.com

相关的一些解释

  • 关于nginx配置文件中的项目目录/var/www/site1.test.com/public;,我们在刚开始的时候,设置过.env文件中的APP_CODE_PATH_HOST=../php-workspace/,那就意味着我们本机的php-workspace是挂载在docker容器里的 /var/www目录中的,而/var/www目录是nginxweb目录,下面我们来查看验证下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
docker ps 
...略

docker inspect bfb904a6957f #这个id为nginx的容器id

...
"Mounts": [ #这里就是挂载信息
{
"Type": "bind",
"Source": "/Users/lvxinxin/php-workspace",
"Destination": "/var/www",
"Mode": "cached",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/Users/lvxinxin/laradock/nginx/sites",
"Destination": "/etc/nginx/sites-available",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/Users/lvxinxin/laradock/logs/nginx",
"Destination": "/var/log/nginx",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
...

数据库配置

1
2
3
4
5
6
7
8
# 打开larvel项目的.env文件,注意这个是laravel项目里的.env,不是laradock里的.env文件

DB_CONNECTION=mysql
DB_HOST=mysql #注意这个即可,这个会自动连接docker里的mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root

连接其它主机的话,就把host设置为远程的ip就可以,和以前不用docker的方式一样

运行命令

1
2
3
4
5
6
7
8
9
10
docker-compose exec worksapce bash

php -v

#这是docker里面的php版本

PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr 5 2018 08:53:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.4-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

错误列表

Version ‘node’ not found - try nvm ls-remote to browse available versions.

解决方法:

1
2
3
4
# 参考: https://github.com/laradock/laradock/issues/1564
#打开 .env文件

WORKSPACE_NODE_VERSION=v8.12.0 #指定一个node版本即可

关于mysql版本的问题

建议使用5.7版本,可以在.env文件里的 MYSQL_VERSION=5.7,进行设置,否则默认为最新的8.0,之所以换成5.7是因为如果换成最新的,我们在外部,比如使用navicat or Sequel Pro 远程连接docker 容器中的mysql时,会报一个错误,导致无法远程连接mysql

关于mysql状态为 Exit2

1
2
3
4
5
6
7
8
9
lvxinxin@lvxinxindeMacBook-Pro:~/laradock$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1 dockerd-entrypoint.sh Up 2375/tcp
laradock_mysql_1 docker-entrypoint.sh mysqld Exit 2
laradock_nginx_1 nginx Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1 docker-php-entrypoint php-fpm Up 9000/tcp
laradock_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
laradock_workspace_1 /sbin/my_init Up 0.0.0.0:2222->22/tcp

查看mysql日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
docker-compose logs mysql

mysql_1 | key_buffer_size=8388608
mysql_1 | read_buffer_size=131072
mysql_1 | max_used_connections=0
mysql_1 | max_threads=151
mysql_1 | thread_count=0
mysql_1 | connection_count=0
mysql_1 | It is possible that mysqld could use up to
mysql_1 | key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 68195 K bytes of memory
mysql_1 | Hope that's ok; if not, decrease some variables in the equation.
mysql_1 |
mysql_1 | Thread pointer: 0x0
mysql_1 | Attempting backtrace. You can use the following information to find out
mysql_1 | where mysqld died. If you see no messages after this, something went
mysql_1 | terribly wrong...
mysql_1 | stack_bottom = 0 thread_stack 0x40000
mysql_1 | mysqld(my_print_stacktrace+0x2c)[0x55b9cfca7a6c]
mysql_1 | mysqld(handle_fatal_signal+0x479)[0x55b9cf5d3709]
mysql_1 | /lib/x86_64-linux-gnu/libpthread.so.0(+0x110c0)[0x7fb58501c0c0]
mysql_1 | /lib/x86_64-linux-gnu/libc.so.6(gsignal+0xcf)[0x7fb5837a8fff]
mysql_1 | /lib/x86_64-linux-gnu/libc.so.6(abort+0x16a)[0x7fb5837aa42a]
mysql_1 | mysqld(+0x628c7b)[0x55b9cf5a9c7b]
mysql_1 | mysqld(_ZN2ib5fatalD1Ev+0x12d)[0x55b9cfe7707d]
mysql_1 | mysqld(+0xfa2db1)[0x55b9cff23db1]
mysql_1 | mysqld(+0xfa33e8)[0x55b9cff243e8]
mysql_1 | mysqld(_Z6fil_ioRK9IORequestbRK9page_id_tRK11page_size_tmmPvS8_+0x2b0)[0x55b9cff2d510]
mysql_1 | mysqld(_Z13buf_read_pageRK9page_id_tRK11page_size_t+0xce)[0x55b9cfee264e]
mysql_1 | mysqld(_Z16buf_page_get_genRK9page_id_tRK11page_size_tmP11buf_block_tmPKcmP5mtr_tb+0x4aa)[0x55b9cfeb189a]
mysql_1 | mysqld(_Z31trx_rseg_get_n_undo_tablespacesPm+0x143)[0x55b9cfe55213]
mysql_1 | mysqld(+0x627def)[0x55b9cf5a8def]
mysql_1 | mysqld(_Z34innobase_start_or_create_for_mysqlv+0x2f3d)[0x55b9cfe220cd]
mysql_1 | mysqld(+0xd6d933)[0x55b9cfcee933]
mysql_1 | mysqld(_Z24ha_initialize_handlertonP13st_plugin_int+0x4f)[0x55b9cf61e3df]
mysql_1 | mysqld(+0xb143d6)[0x55b9cfa953d6]
mysql_1 | mysqld(_Z40plugin_register_builtin_and_init_core_sePiPPc+0x2f0)[0x55b9cfa985c0]
mysql_1 | mysqld(+0x64ae16)[0x55b9cf5cbe16]
mysql_1 | mysqld(_Z11mysqld_mainiPPc+0xc71)[0x55b9cf5cd9d1]
mysql_1 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fb5837962e1]
mysql_1 | mysqld(_start+0x2a)[0x55b9cf5c40ba]
mysql_1 | The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
mysql_1 | information that should help you find out what is causing the crash.

解决方法

我找到的第一个办法是说因为.env里的DATA_SAVE_PATH 没办法在windows下运行,而我的是mac系统,而且在.env里也没有找到这个常量。参考地址:https://github.com/laradock/laradock/issues/868#issuecomment-297675241

第二个办法是:删除现在的容器,重新build,然后再运行。不过貌似还是不行,后来说是需要把~/.laradock/data/mysql这个目录删除掉,然后再重新运行就ok了,我想可能是因为之前我装的是8.0,即使是修改了版本号,重新build,因为这个地方的原因,一直还是有问题。删除之后就可以解决了,参考地址https://github.com/laradock/laradock/issues/1478

然后使用127.0.0.1 root:root 3306 连接mysql即可

Failed to Setup IP tables: Unable to enable SKIP DNAT rule:

家里的电脑是用虚拟机装的centos 7,docker已经安装好了,晚上想学下iptables,而centos7默认的是firewall,于是就把centos7上的firewall改为iptables,但是d docker里使用docker-compose 进行安装的时候 ,一直提示Failed to Setup IP tables: Unable to enable SKIP DNAT rule:,后来查了下,重启下dokcer即可,因为之前是firewall,切换成iptables之后,docker也要重启下systemctl restart docker