开始在Docker容器中运行Laravel

本文介绍了如何在Docker容器中运行Laravel应用。首先,确保安装了Docker和Docker Compose作为先决条件。然后,创建一个Dockerfile以指导Laravel应用的构建,并在docker-compose.yml文件中定义Web和MySQL服务。通过这种方式,可以使用单个命令启动容器,实现数据库和应用的协同运行。最后,文章提到后续会探讨如何使用Docker和Laravel构建Todo应用以及如何扩展Laravel应用。
摘要由CSDN通过智能技术生成

Laravel and Docker are two very popular tools of choice when considering building for the web. Although both of them do very different things, they can both be combined to create amazing products.

在考虑构建Web时,Laravel和Docker是两个非常受欢迎的工具。 尽管他们俩做的事情截然不同,但可以将它们组合在一起创造出令人赞叹的产品。

For our use case, we will be running Laravel in a Docker container. This is going to be a simple demonstration on how to use both products to create real-life applications. Nothing heavy.

对于我们的用例, 我们将在Docker容器中运行Laravel 。 这将是关于如何使用这两种产品来创建实际应用程序的简单演示。 没什么

While this article is made to cater for everyone who will be reading it, the article assumes you already have a working basic knowledge of both Docker and Laravel.

尽管本文旨在迎合所有将要阅读的读者,但本文假定您已经具备DockerLaravel的基本知识。

先决条件 ( Prerequisites )

Before you start, you need to have some certain prerequisites in place:

在开始之前,您需要具备一些前提条件:

  • Local machine running the latest version of Docker, v1.13.1 at the time of writing this article.

    在撰写本文时,运行最新版本Docker v1.13.1的本地计算机。
  • Local machine running the latest version of Docker compose.

    运行最新版本Docker的本地计算机。
  • Server running the latest version of Docker, v1.13.1 at the time of writing this article.

    在撰写本文时,服务器正在运行最新版本的Docker v1.13.1。
  • Laravel Installer installed on your machine (optional but recommended).

    您的机器上安装Laravel安装程序(可选,但建议使用)。

入门 ( Getting Started )

To get started, let us create an instance of Laravel running inside a Docker container. We will be starting with a base image and customizing it to suit our applications needs.

首先,让我们创建一个在Docker容器中运行的Laravel实例。 我们将从基础映像开始,并对其进行自定义以适合我们的应用程序需求。

# Create the project directory and some subdirectories we will need later
$ mkdir -p acme/storage/mysql 
$ mkdir -p acme/storage/logs
$ mkdir -p acme/storage/app

# Go into the application directory and create a new laravel application
$ cd acme
$ laravel new src

Now that we have our Laravel application inside the appropriate directory, let us create a Dockerfile to instruct the Laravel application on how to build. Create a new Docker file at ./src/Dockerfile.

现在,我们在适当的目录中包含了Laravel应用程序,让我们创建一个Dockerfile来指导Laravel应用程序如何构建。 在./src/Dockerfile创建一个新的Docker文件。

For your convenience, I have created a base Docker image that works right out of the box for Laravel. creativitykills/nginx-php-server. You can see the source code for the Docker image right here.

为了方便起见,我创建了一个基本的Docker映像,该映像可直接用于Laravel。 creativekills / nginx-php-server 。 您可以在此处查看 Docker映像的源代码。

FROM creativitykills/nginx-php-server:latest
MAINTAINER Neo Ighodaro <neo@creativitykills.co>

The Dockerfile is where we instruct Docker on what to build and how to build it. In this file, we are saying: 'Hey Docker, build a container from the creativitykills/nginx-php-server image, the maintainer is Neo Ighodaro...'. For more information on dockerfiles and the options available to you, read the documentation, but this will do for now.

Dockerfile是我们指导Docker构建什么以及如何构建它的地方。 在这个文件中,我们说的是:“嘿,Docker,从creativekills / nginx-php-server镜像构建一个容器,维护者是Neo Ighodaro ...”。 有关dockerfile和可用选项的更多信息,请阅读文档 ,但这仅适用于此。

Finally, lets create a ./docker-compose.yml file. Make sure it is at the root of your project. We will add our first service which is the web service to the docker compose file.

最后,让我们创建一个./docker-compose.yml文件。 确保它是您项目的根目录。 我们将第一个服务即Web服务添加到docker compose文件中。

version: '2'
services:
    web:
        build: ./src
        container_name: web
        ports:
            - 8888:80
        volumes:
            - ./src:/var/www
            - ./storage/app:/var/www/storage/app
            - ./storage/logs:/var/www/storage/logs

Once we have defined the web service file, we can move on to the next thing which is creating a MySQL service. Open the docker-compose.yml and make sure you have the following.

定义了Web服务文件后,我们可以继续进行下一步,即创建MySQL服务。 打开docker-compose.yml并确保您具有以下内容。

version: '2'
services:
    mysql:
        image: mysql:5.7
        env_file:
            - ./mysql.env
        volumes:
            - ./storage/mysql:/var/lib/mysql
    web:
        build: ./web
    container_name: web
        ports:
            - 8888:80
        volumes:
            - ./web:/var/www
            - ./storage/app:/var/www/storage/app
            - ./storage/logs:/var/www/storage/logss

Docker allows you to bring up a container in many ways, you can manually bring up a running instance using docker run, but sometimes, you will want to bring up multiple containers with one single command and that's where docker-compose comes in.

Docker允许您以多种方式启动一个容器,您可以使用docker run手动启动一个正在运行的实例,但是有时,您将希望使用一个命令来启动多个容器,这就是docker-compose的来历。

The version is telling Docker compose what version of the docker-compose syntax tthe current yaml uses. services lists the services we would like to control with docker compose. In the mysql service, we want to build the mysql:5.7 image, load an environment file that contains the MySQL image configuration, and finally mount a volume to the MySQL image for persistent storage of the database data.

版本告诉Docker撰写当前yaml使用的docker-compose语法版本。 服务列出了我们要使用docker compose控制的服务。 在mysql服务中,我们要构建mysql:5.7映像 ,加载包含MySQL映像配置的环境文件,最后将卷安装到MySQL映像以持久存储数据库数据。

Protip: Mounting a volume to the container keeps it alive even after the container is killed. When you mount a folder on the host to the container, the folder is basically mapped to the docker container and can be accessed from either inside or outside the container.

提示:即使在容器被杀死后,在容器上安装一个卷也可以使其保持活动状态。 当您将主机上的文件夹安装到容器时,该文件夹基本上被映射到docker容器,并且可以从容器内部或外部进行访问。

In the web service, one different thing we did is we exposed and mapped a port 8888 (on the host) to 80 (inside the container). We also specified a container_name so we can easily reference the container when we want to.

Web服务中,我们所做的另一件事是我们将端口8888 (在主机上)公开并映射到80 (在容器内部)。 我们还指定了一个container_name,以便我们可以在需要时轻松引用该容器。

Now create a new file called ./mysql.env in the root directory, and in there customize these environment values. These will be set as your database credentials when the MySQL container is built.

现在,在根目录中创建一个名为./mysql.env的新文件,并在其中自定义这些环境值。 这些将在构建MySQL容器时设置为您的数据库凭据。

MYSQL_DATABASE=homestead
MYSQL_USER=homestead
MYSQL_PASSWORD=secret
MYSQL_ROOT_PASSWORD=SuperSecretRootPassword

Protip If you are using a git repository, you might want to gitignore the mysql.env file for security reasons.

Protip如果您使用的是git存储库,出于安全原因,可能需要gitignore mysql.env文件。

Once you are done, you need to edit your Laravel applications .env file. Update the database configuration to match whatever you set as your MySQL details above. Note that the DB_HOST value in your Laravel .env file is equal to the name of the mysql service in the docker-compose.yml; in this case its web.

完成后,您需要编辑Laravel应用程序.env文件。 更新数据库配置以匹配您在上面设置MySQL详细信息。 请注意,您的Laravel .env文件中的DB_HOST值等于docker-compose.yml mysql服务的名称; 在这种情况下是它的web

Now, we can bring up our containers mysql and web using a single command. cd to the root directory of your application and run the command docker-compose up -d. This should bring up all your services. You can visit the docker URL for your web container http://localhost:8888 and you should see the awesome Laravel welcome page.

现在,我们可以使用单个命令启动容器mysqlwebcd到应用程序的根目录,然后运行命令docker-compose up -d 。 这应该调出您所有的服务。 您可以访问web容器http:// localhost:8888的Docker URL,并且应该看到很棒的Laravel欢迎页面。

You can ssh into your web container and run the command php artisan migrate to run migrations on your database. To ssh into your container, run the command:

您可以使用ssh进入Web容器,然后运行php artisan migrate migration命令以在数据库上运行迁移。 要ssh到您的容器中,请运行以下命令:

$ docker exec -it web bash

结论 (Conclusion)

Docker makes it easy to run consistent disposable environments, and with a little push, it is easy to get up and running with Docker and Laravel. In future articles, I will cover how to create a simple Todo app using our new Laravel + Docker application and also how to scale your Laravel application using Docker compose.

借助Docker,可以轻松地运行一致的一次性环境,只需稍加推动即可轻松使用Docker和Laravel进行安装和运行。 在以后的文章中,我将介绍如何使用新的Laravel + Docker应用程序创建一个简单的Todo应用程序,以及如何使用Docker compose扩展Laravel应用程序。

翻译自: https://scotch.io/tutorials/get-started-running-laravel-in-a-docker-container

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值