简体   繁体   中英

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. 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. 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. 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. So I wanted to make our deployment Flow more streamlined and automatic with BitBucket pipelines. 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.

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
  • on this VM I install 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.
  • ?? Do I need to do extra stuff here like copying of folders with code or is that done by bitbucket??

Now the problem I encounter is whilst creating this "docker container" for my situation. 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. 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.

I've logged into the CentOS VM and performed the following commands:

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:

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:

<!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.

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.

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. 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? 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

  • 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. Build a Docker image starting from Apache image, which then has a step to PULL the code from the git repository inside the container. 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)

  • 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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