您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
docker开发环境搭建(docker环境下创建laravel)
发布时间:2024-10-26 17:45:15编辑:雪饮阅读()
-
Laravel & Docker
We want it to be as easy as possible to get started with Laravel regardless of your preferred operating system. So, there are a variety of options for developing and running a Laravel project on your local machine. While you may wish to explore these options at a later time, Laravel provides Sail, a built-in solution for running your Laravel project using Docker.
Docker is a tool for running applications and services in small, light-weight "containers" which do not interfere with your local machine's installed software or configuration. This means you don't have to worry about configuring or setting up complicated development tools such as web servers and databases on your local machine. To get started, you only need to install Docker Desktop.
Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker configuration. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.
Already a Docker expert? Don't worry! Everything about Sail can be customized using the docker-compose.yml file included with Laravel.
我们希望尽可能容易地开始使用Laravel,而不管你喜欢什么操作系统。因此,在本地机器上开发和运行Laravel项目有多种选择。虽然你可能希望稍后探索这些选项,但Laravel提供了Sail,这是一个使用Docker运行Laravel项目的内置解决方案。
Docker是一种在小型、轻量级的“容器”中运行应用程序和服务的工具,不会干扰本地计算机安装的软件或配置。这意味着您不必担心在本地计算机上配置或设置复杂的开发工具,如web服务器和数据库。要开始使用,您只需要安装Docker Desktop。
Laravel Sail是一个轻量级的命令行界面,用于与Laravel的默认Docker配置进行交互。Sail为使用PHP、MySQL和Redis构建Laravel应用程序提供了一个很好的起点,而不需要之前的Docker经验。
已经是Docker专家了?别担心!关于Sail的一切都可以使用Laravel附带的docker-compose.yml文件进行自定义。
Getting Started On Linux
If you're developing on Linux and Docker Compose is already installed, you can use a simple terminal command to create a new Laravel project. For example, to create a new Laravel application in a directory named "example-app", you may run the following command in your terminal:
curl -s https://laravel.build/example-app | bash
Of course, you can change "example-app" in this URL to anything you like - just make sure the application name only contains alpha-numeric characters, dashes, and underscores. The Laravel application's directory will be created within the directory you execute the command from.
Sail installation may take several minutes while Sail's application containers are built on your local machine.
After the project has been created, you can navigate to the application directory and start Laravel Sail. Laravel Sail provides a simple command-line interface for interacting with Laravel's default Docker configuration:
cd example-app
./vendor/bin/sail up
Once the application's Docker containers have been started, you can access the application in your web browser at: http://localhost.
To continue learning more about Laravel Sail, review its complete documentation.
如果你在Linux上开发,并且已经安装了Docker Compose,你可以使用一个简单的终端命令来创建一个新的Laravel项目。例如,要在名为“example-app”的目录中创建一个新的Laravel应用程序,您可以在终端中运行以下命令:
curl -s https://laravel.build/example-app | bash
当然,您可以将此URL中的“示例应用程序”更改为任何您喜欢的名称——只需确保应用程序名称仅包含字母数字字符、破折号和下划线。Laravel应用程序的目录将在您执行命令的目录中创建。
Sail安装可能需要几分钟,而Sail的应用程序容器是在您的本地计算机上构建的。
创建项目后,您可以导航到应用程序目录并启动Laravel Sail。Laravel Sail提供了一个简单的命令行界面,用于与Laravel的默认Docker配置进行交互:
cd example-app
./vendor/bin/sail up
启动应用程序的Docker容器后,您可以在web浏览器中访问该应用程序:http://localhost.
要继续了解更多关于Laravel Sail的信息,请查看其完整文档。
实践-干掉默认的80服务端口
基于以上,我们是选择了linux来进行docker环境搭建。
我们可以看到最终是访问localhost,即80端口。
所以我们首先要确保我们的80端口不被占用
fuser -k 80/tcp
[root@localhost ~]# curl -i localhost
curl: (7) Failed connect to localhost:80; Connection refused
实践-基于宝塔的docker-compose
按他上面的要求,就是说你要有docker-compose的安装。
那么如果你有安装宝塔,至少我当前宝塔9.2.0里面是有docker-compose的
但你如果按照laravel的说明去执行会出现如下错误
[root@localhost ~]# curl -s https://laravel.build/example-app | bash
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": dial tcp 31.13.85.34:443: i/o timeout.
See 'docker run --help'.
bash: line 17: cd: example-app: No such file or directory
bash: line 23: ./vendor/bin/sail: No such file or directory
bash: line 24: ./vendor/bin/sail: No such file or directory
Get started with: cd example-app && ./vendor/bin/sail up
则这里我们可以尝试访问
https://laravel.build/example-app
会发现页面输出
docker info > /dev/null 2>&1
# Ensure that Docker is running...
if [ $? -ne 0 ]; then
echo "Docker is not running."
exit 1
fi
docker run --rm \
--pull=always \
-v "$(pwd)":/opt \
-w /opt \
laravelsail/php83-composer:latest \
bash -c "laravel new example-app --no-interaction && cd example-app && php ./artisan sail:install --with=mysql,redis,meilisearch,mailpit,selenium "
cd example-app
# Allow build with no additional services..
if [ "mysql redis meilisearch mailpit selenium" == "none" ]; then
./vendor/bin/sail build
else
./vendor/bin/sail pull mysql redis meilisearch mailpit selenium
./vendor/bin/sail build
fi
CYAN='\033[0;36m'
LIGHT_CYAN='\033[1;36m'
BOLD='\033[1m'
NC='\033[0m'
echo ""
if sudo -n true 2>/dev/null; then
sudo chown -R $USER: .
echo -e "${BOLD}Get started with:${NC} cd example-app && ./vendor/bin/sail up"
else
echo -e "${BOLD}Please provide your password so we can make some final adjustments to your application's permissions.${NC}"
echo ""
sudo chown -R $USER: .
echo ""
echo -e "${BOLD}Thank you! We hope you build something incredible. Dive in with:${NC} cd example-app && ./vendor/bin/sail up"
fi
可以看到这里有个关键点
laravelsail/php83-composer:latest
可以用多个不同的镜像源去测试
最后我发现
docker pull 05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com/laravelsail/php83-composer:latest
这个源05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com可以
那你试试单独先执行这个呗。
然后接下来再执行
curl -s https://laravel.build/example-app | bash
就没有问题了。这里看似没有问题,但没有输出,可能是脚本内部还有问题。
那么所以你需要将刚才宝塔哪里的加速url修改为上面我们测试可以用的这个即https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com。
再次运行上面的脚本(你可以先排查下,如果之前有创建的未完整的容器就删除以及创建的未完整的example-app目录也删除)
这次的依赖就更多了,当然除了laravelsail/php83-composer:latest是不用重复拉取了。
但是即便这样,有时候curl -s https://laravel.build/example-app | bash第一次执行没有任何输出,第二次就又可以了,怀疑是第一次的时候https://laravel.build/example-app没有请求成功,所以没有执行后面的bash吧。
还有另一点就是这里最后执行的安装的是laravel11,也就是最新版的。真6,我看的是laravel9的,结果给我安装laravel11.
经过漫长的安装,这里还有一个错误
#0 1513.1 run `npm fund` for details
#0 1817.9 npm error code EIDLETIMEOUT
#0 1817.9 npm error errno EIDLETIMEOUT
#0 1817.9 npm error Invalid response body while trying to fetch https://registry.npmjs.org/pnpm: Idle timeout reached for host `registry.npmjs.org:443`
#0 1817.9 npm error A complete log of this run can be found in: /root/.npm/_logs/2024-10-26T08_31_26_522Z-debug-0.log
这个错误先不管吧。
继续
cd example-app
./vendor/bin/sail up
又是漫长的等待。
Error response from daemon: driver failed programming external connectivity on endpoint example-app-redis-1 (e8637e4955ba796ea881cdfd7363da400eea80ef872ac7e011f2ea3461d59d20): Error starting userland proxy: listen tcp4 0.0.0.0:6379: bind: address already in use
这个错误就好解决了。
fuser -k 6379/tcp
再次尝试
Attaching to example-app-laravel.test-1, example-app-mailpit-1, example-app-meilisearch-1, example-app-mysql-1, example-app-redis-1, example-app-selenium-1
Error response from daemon: driver failed programming external connectivity on endpoint example-app-mysql-1 (e541171c683230cd6b2694f8625faa8723495f36d6a53ceb5c31cfdb59497b23): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use
这个也好解决
fuser -k 3306/tcp
不对,这个好像还不行,干不掉,我这里应该是宝塔那边做了守护进程。所以直接去宝塔把宝塔的mysql服务停掉。
那么按照他文档的意思,我们这里再次运行./vendor/bin/sail up
后可以访问我们虚拟机的ip地址如:
http://192.168.217.130/
就可以看到界面了。
不过界面上又报了一个权限错误
file_put_contents(/var/www/html/storage/framework/views/bae129cef9e600352d1c88ca55b5c61c.php): Failed to open stream: Permission denied
我的处理方式是
chmod -R 777 ./example-app/
再次刷新页面又出现了第二个错误呀
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.sessions' doesn't exist (Connection: mysql, SQL: select * from `sessions` where `id` = 02WQALHDlO0v3Mu4lCOjMdxkNUIg9WdIGFA3Q2sy limit 1)
由于是laravel 11,据说执行如下数据库迁移即可。
[root@localhost example-app]# ./vendor/bin/sail artisan migrate
最后再次刷新页面终于正常的出来了。
本期词汇:
possible 可能的,可能做到的
developing 形成(观点)(develop 的现在分词)
regardless 不管怎样,无论如何
关键字词:docker,开发环境,laravel
相关文章
- 开启注册登陆功能(解决laravel安装Breeze后没有注册、
- laravel环境变量的原理、优势及项目配置
- docker-compose一键构建(尽量使用official官方镜像部署
- docker-compose一键构建(卷及具名卷)lnmp
- docker-compose一键构建(使用子网)lnmp
- docker-compose一键构建(非直接使用docker官方hub中的p
- docker一键构建lnmp(非docker-compose)
- Dockerfile构建lnmp(是docker而非docker-compose)(主机
- 解决docker单镜像实现多服务后-it阻塞问题
- 尚硅谷Docker核心技术学习笔记