简体   繁体   English

CentOS上具有php,mysql和apache的Docker映像

[英]Docker image on CentOS with php, mysql and apache

Right now we have a website running on php 5.6 at Azure on a CentOS 7 based operating system. 现在,我们有一个网站在基于CentOS 7的Azure上的php 5.6上运行。 Every time if we want to do a new code deploy we have to do it using ftp to our server and manually transfer files and folders. 每次我们要部署新代码时,都必须使用ftp到我们的服务器并手动传输文件和文件夹。 This is very error prone and takes us hours of deploying and debugging afterwards every single time. 这非常容易出错,此后每次都要花费我们数小时的部署和调试时间。 we develop on our localhost Windows machine using PHP with WAMP. 我们使用PHP和WAMP在本地Windows计算机上进行开发。 So there's already a discrepancy between our localhost environments and the production environment. 因此,我们的本地主机环境和生产环境之间已经存在差异。

I started reading a lot about docker lately and how it integrates with BitBucket pipelines. 最近,我开始阅读有关Docker及其与BitBucket管道如何集成的许多文章。 So I wanted to make our deployment Flow more streamlined and automatic with BitBucket pipelines. 因此,我想使用BitBucket管道使我们的部署流程更加简化和自动化。 Before I get to the technical stuff I have already tried, I want to make sure that I have the general picture of steps that need to be done correct. 在获得我已经尝试过的技术知识之前,我想确保我已掌握需要正确完成的步骤的一般情况。

What I want to achieve is a way for me and my colleague to write our code and push it to our BitBucket repository, from there on the pipeline picks it up, creates a docker container and deploys it (automatically) (is this a good idea, what about active users during a new container deploy?) to our website. 我想实现的是一种让我和我的同事编写代码并将其推送到我们的BitBucket存储库的方法,从那里从管道中将其拾取,创建一个Docker容器并(自动)部署(这是一个好主意) ,那么在新容器部署期间的活跃用户呢?)。

These are the steps I think that need to be done, please correct me where I am wrong: 这些是我认为需要完成的步骤,请在错误之处纠正我:

  • 1) I create a CentOS virtual machine using VirtualBox 1)我使用VirtualBox创建一个CentOS虚拟机
  • on this VM I install docker 在此VM上我安装docker
  • I create a dockerfile here where I use the php7.3-apache base image and I will install mysql on top of it as well. 我在这里创建一个dockerfile,在其中使用php7.3-apache基本映像,我还将在其之上安装mysql。
  • ?? ?? Do I need to do extra stuff here like copying of folders with code or is that done by bitbucket?? 我是否需要在这里做一些额外的事情,例如使用代码复制文件夹还是由bitbucket完成?

Now the problem I encounter is whilst creating this "docker container" for my situation. 现在我遇到的问题是为我的情况创建这个“ docker容器”。 I realize this is probably a very common use case for Docker, but I've read through thousand of tutorials and watches tons of videos, but I cannot find answers to my most basic questions and I end up being stuck and frustrated for days/weeks. 我意识到这可能是Docker的一个非常普遍的用例,但是我已经阅读了成千上万的教程并观看了无数的视频,但是我找不到最基本的问题的答案,最终我被困在几天或几周的沮丧中。 I've got a fully working website created in CodeIgniter, but for the sake of the question I just want to have a working version of the docker container containing PHP MySQL and Apache. 我已经在CodeIgniter中创建了一个可以正常运行的网站,但是出于这个问题,我只想拥有一个包含PHP MySQL和Apache的docker容器的工作版本。

I've logged into the CentOS VM and performed the following commands: 我已经登录到CentOS VM,并执行了以下命令:

mkdir dockertest
touch index.html (and i placed some text in here)
touch index.php (and i placed a basic echo "hello world" in here)
touch docker-compose.yml
mkdir .docker
cd .docker
touch Dockerfile
touch vhost.conf

Dockerfile looks like this: Dockerfile看起来像这样:

FROM php:7.3.0-apache-stretch

MAINTAINER Dennis

COPY . /srv/app
COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf

RUN chown -R www-data:www-data /srv/app && a2enmod rewrite

Then I'm able to build the image using 然后我可以使用创建图像

docker build --file .docker/Dockerfile -t docker-test .

Right now I can run the container with the following command: 现在,我可以使用以下命令运行容器:

docker run --rm -p 8080:80 docker-test

At this point I go to my CentOS VM and I try to do a curl localhost:8080 and I get the following HTML: 此时,我转到CentOS VM,尝试执行curl localhost:8080,并得到以下HTML:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access on this server. <br />
</p>
<hr>
<address>Apache/2.4.25 (Debian) Server at localhost Port 8080</address>
</body></html>

So I guess this means that the Apache server is running, but it does not see my index files anywhere. 所以我想这意味着Apache服务器正在运行,但是在任何地方都看不到我的索引文件。

I am massively overwhelmed by the amount of documentation and tutorials that are available for Docker, but they all seem to be too high level for me or none which targets CentOS 7, PHP, MySQL and Apache combined. 可用于Docker的文档和教程数量之多使我感到不知所措,但是对于我来说,它们似乎都太高了,或者对我而言,没有一个针对CentOS 7,PHP,MySQL和Apache。

A question that's also bugging me: The advantage of Docker is that it can be deployed to anywhere and the environment is the exact same. 这个问题也困扰着我:Docker的优势在于它可以部署到任何地方,并且环境完全相同。 This causes no problems like "it works on my localhost". 这不会引起任何问题,例如“它可以在我的本地主机上运行”。 But how does this exactly work, do me and my colleague need to develop our code INSIDE the docker container? 但是,这到底是如何工作的,我和我的同事需要在Docker容器内开发代码吗? How does this even work? 这怎么工作?

The process should be: 该过程应为:

  • develop: you and your colleages develop code, they push that to a version control system (git on bitbucket/github) -> the code is in one trusted repository 开发:您和您的同事开发代码,他们将其推送到版本控制系统(bitbucket / github上的git)->代码位于一个受信任的存储库中

  • build: you take this code and create a (or multiple) Docker image(s) with it: on the Apache server, you need the HTML, Javascript code. 构建:您需要使用此代码并使用它创建一个(或多个)Docker映像:在Apache服务器上,您需要HTML,Javascript代码。 Build a Docker image starting from Apache image, which then has a step to PULL the code from the git repository inside the container. 从Apache映像开始构建一个Docker映像,然后执行从容器内git仓库中提取代码的步骤。 That's your front end server. 那就是你的前端服务器。

For the DB part, you probably want another container, or even use a managed service that handles the migrations/updates for you, so you only need to worry about the data in the database. 对于数据库部分,您可能需要另一个容器,甚至使用托管服务来为您处理迁移/更新,因此您只需要担心数据库中的数据。 If you want to have you own container, make sure the data is in a VOLUME that is mounted in the container, but is otherwise stored on a local or network drive, (ie NOT inside the container which would get destroyed on any update) 如果您想拥有自己的容器,请确保数据位于容器中已安装的VOLUME中,但否则存储在本地或网络驱动器中(即不在容器内部,该容器在任何更新时都将被破坏)

  • deploy: pull the images from registry of choice, and make sure the containers are connected as needed (ie either on the same host and linked, or on different nodes that have access to each other through a private network) 部署:从选择的注册表中提取映像,并确保根据需要连接容器(即,在同一主机上并已链接,或者在通过专用网络相互访问的不同节点上)

Notes: 笔记:

Use Docker for Windows rather than virtual machine and installing Docker inside it. 在Windows而不是虚拟机上使用Docker,并在其中安装Docker。

The host doesn't matter, it's the base image in the container that matters whether you deploy on a Ubuntu, CentOS or CoreOS host, the Docker base image is what matters for you to install dependencies and make your code run. 主机无关紧要,无论您是在Ubuntu,CentOS还是CoreOS主机上部署,容器中的基础映像都很重要,而Docker基础映像对于安装依赖关系和使代码运行至关重要。

On the build phase, you probably don't want to pull from git inside the image if your project is a private repository, because you would need to have credentials inside the image to do that: rather you either pull the code from git outside the image, and ADD it to the image, or use another (private) container that has the git pull credentials to pull the code, do the build, and dump a build file that you can then ADD to a shippable container. 在构建阶段,如果您的项目是私有存储库,则可能不想从映像中的git中提取,因为您需要在映像中具有凭据才能这样做:相反,您可以从git中将代码从git中提取出来映像,并将其添加到映像,或使用另一个具有git pull凭据的(专用)容器来提取代码,进行构建并转储构建文件,然后可以将其添加到可运输的容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM