search
HomeBackend DevelopmentPHP TutorialLaraDock -- Docker 版 Laravel Homestead:让你在Docker中轻松运行Laravel应用

什么是LaraDock

LaraDock 和Laravel Homestead 类似,只不过运行在Docker 而非Vagrant 中。LaraDock 可以让我们在数秒之内轻松在 Docker 中运行 Laravel 应用。

LaraDock 致力于让开发过程变得简单,包含了一个已经预包装好的Docker映像以便为我们提供一流的开发环境而不需要再手动安装PHP、Nginx、MySQL、Redis以及其他运行Laravel应用可能要用到的软件或工具。

Docker vs Vagrant

说到这里,首先我们来了解什么是Docker:Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机)、bare metal、OpenStack 集群和其他的基础应用平台。

接下来我们来讨论为什么使用Docker而不是Vagrant:从时间上来看,Vagrant提供虚拟机需要数分钟,而Docker只需数秒;此外,从体量上来看,Vagrant提供的是完整的虚拟机,而Docker提供的是轻量级的虚拟容器,这些虚拟容器共享同一个内核并且允许在独立进程中运行。

LaraDock内置软件

LaraDock内置的Docker映像包含的软件:

  • Nginx + PHP
  • MySQL
  • Redis
  • Beanstalked
  • Data Volume

你可以编辑这些映像: https://github.com/LaraDock/docker-images

系统要求

  • Laravel(安装)
  • Docker Toolbox( 下载 )
  • Git( 下载 )
  • Composer( 下载 )

使用教程

1)安装任意版本Laravel,或者使用已存在的Laravel项目

2)在Laravel项目根目录下克隆LaraDock到 docker 目录:

git clone https://github.com/LaraDock/laradock.git docker

3)安装完Docker Toolbox后,将其安装目录放到系统路径,然后查看Docker IP地址:

  • 如果你使用的是Linux:IP地址默认是 127.0.0.1 ,因为容器可以直接在本地运行
  • 如果你使用的是Mac或Windows并且使用的是 docker-machine :输入 docker-machine ip {vm-name-here} 命令查看(默认IP是 192.168.99.100 )
  • 如果你使用的是Mac或Windows并且使用的是 boot2docker :输入 boot2docker ip 命令查看

这里我们使用的是Windows环境并使用 docker-machine ,查看IP地址方式如下:

可见IP地址为 192.168.99.100 。

4)在 hosts 文件中映射Docker IP到 laravel.dev :

xxx.xxx.xxx.xxx laravel.dev

不要忘了将 xxx.xxx.xxx.xxx 替换成你的Docker IP地址,这里我的IP地址是192.168.99.100。

5)在上述第二步新创建的 docker 目录中,打开 docker-compose.yml 文件将 xxx.xxx.xxx.xxx 替换成你的Docker IP地址。

6)打开Laravel项目的 .env 文件,将 DB_HOST 、 REDIS_HOST 设置为 laravel.dev :

DB_HOST=laravel.devREDIS_HOST=laravel.dev

如果在 .env 中没有找到变量 REDIS_HOST ,到数据库配置文件 config/database.php 中将 127.0.0.1 替换成 laravel.dev :

'redis' => [    'cluster' => false,    'default' => [        'host' => 'laravel.dev',        'port' => 6379,        'database' => 0,    ],],

如果你想要使用Redis作为缓存/Session驱动,打开 .env 文件将 CACHE_DRIVER 和 SESSION_DRIVER 设置为 redis :

CACHE_DRIVER=redisSESSION_DRIVER=redis

7)最后在 docker 目录下使用如下命令运行容器:

docker-compose up

如果你想要在后台运行容器可以使用 docker-compose up -d 命令:

注意:第一次运行上述命令时会花费5分钟左右(取决于你的网速)下载映像到本地。

8)在浏览器中访问 http://laravel.dev 。

更多使用介绍可参考官方文档: http://laradock.github.io/laradock/

GitHub地址

LaraDock: https://github.com/LaraDock/laradock

Docker-Images: https://github.com/LaraDock/docker-images

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.