Build LAMP running environment tutorial with docker

  • 2020-05-17 07:03:47
  • OfStack

LAMP introduction

LAMP refers to the first letter of Linux (operating system), ApacheHTTP server, MySQL (sometimes MariaDB, database software) and PHP (sometimes Perl or Python), which is used to build web server. Although these open source programs themselves were not designed to work with several other programs, the combination became popular because they were free and open source (most Linux distributions *** * these software). When used by 1, they behave like a dynamic solution package.

Here's how to use docker to build a container containing lamp components:

pull 1 lamp mirror from the website

There is no mirror image of lamp marked OFFICIAL in the official warehouse, but the mirror image of "tutum" is very good, we can directly use the mirror image of pull to complete our operation.


"lang-bash">core@localhost ~/base $ docker pull tutum/lamp
Pulling repository tutum/lamp
b32789c7d66: Download complete
...

Start the lamp container by default


"lang-bash">core@localhost ~/base $ docker run "hljs-operator">-d -p : -p : tutum/lamp

 
ee00c97a5cdefb985baf826c16723f333aa5edddee4072a5107c724ad09f10d
core@localhost ~/base $ docker ps
CONTAINER ID    IMAGE               COMMAND        CREATED       STATUS       PORTS                     NAMES
ee00c97a5 "hljs-built_in">cd    tutum/lamp:latest         "/run.sh"        seconds ago    Up seconds    0.0.0.0:->/tcp, 0.0.0.0:->/tcp  lonely_davinci
e3c136d76b44    tutum/tomcat: "hljs-number">8.0          "hljs-string">"/run.sh"        minutes ago   Up minutes    "hljs-number">0.0. "hljs-number">0.0:->/tcp              tomcat001
fe9e65aaf58c    dl.dockerpool.com:/mysql: "hljs-number">5.7  "hljs-string">"/entrypoint.sh mysq  51 minutes ago   Up 51 minutes    3306/tcp                    db001,tomcat001/tomysql
core@localhost ~/base $ curl http://127.0.0.1:8080

Use curl to see that the default application has started


<html>
<head>
    <title>Hello world!</title>
    <style>
    body {
        background-color: white;
        text-align: center;
        padding: 50px;
        font-family: "Open Sans "hljs-string">","Helvetica Neue "hljs-string">",Helvetica,Arial,sans-serif;
    }

    #logo {
        margin-bottom: 40px;
    }
    </style>
</head>
<body>
    <img id="logo "hljs-string">" src="http://upload.server110.com/image/20141116/205R55010-0.png" />
    <h1>Hello world!</h1>
            <h2>MySQL Server version: 5.5.38-0ubuntu0.14.04.1</h2>
    </body>
</html>

Deploy your own PHP application

The default container starts one helloword application, and we can use dockerfile to create another image to deploy our own application. The syntax of dockerfile is described in a later section.


core@localhost ~ $ mkdir php
core@localhost ~ $ cd php/
core@localhost ~/php $ touch Dockerfile
core@localhost ~/php $ vi Dockerfile
core@localhost ~/php $ docker build -t dockerpool/my-lamp-app .

Dockerfile reads as follows:


FROM tutum/lamp:latest
RUN rm -fr /app && git clone https://github.com/username/customapp.git /app
# Here to replace  https://github.com/username/customapp.git  The address is your own project address 
EXPOSE 80 3306
CMD ["/run.sh"]

Start your container again and the deployment is complete


"lang-bash">core@localhost ~/php $ docker stop  ee
ee
core@localhost ~/php $ docker rm ee
ee
core@localhost ~/php $ docker run "hljs-operator">-d -p : -p : dockerpool/my-lamp-app

Use curl to see if your application has started correctly.


curl http://localhost/

Connect to the database in the php program to access the mysql database in the container

The mirrored mysql database has a default root user, and you can connect locally without a password, so it's easy to access from code.


"hljs-preprocessor"><?php
$mysql = "hljs-keyword">new mysqli( "hljs-string">"localhost", "hljs-string">"root");
echo "hljs-string">"MySQL Server info: ". "hljs-variable">$mysql "hljs-variable">->host_info;
?>

Access the mysql database outside the container

When we launch the container with tutum/lamp image for the first time, it will automatically create an mysql user called admin and generate a random password that can be obtained using "docker logs + container ID".


"lang-bash">core@localhost ~/php $ docker logs cb
=> An empty or uninitialized MySQL volume is detected in /var/lib/mysql
=> Installing MySQL ...
=> Done!
=> Waiting "hljs-keyword">for confirmation of MySQL service startup
=> Creating MySQL admin user with random password
=> Done!
========================================================================
You can now connect to this MySQL Server using:

  mysql -uadmin -p2Ijg6gvmM0N3 -h<host> -P<port>

Please remember to change the above password as soon as possible!
MySQL user "hljs-string">'root' has no password but only allows local connections
========================================================================

The default root user cannot log in remotely, so use the admin user, which also has root privileges.

conclusion

The above is the whole content of this article, I hope the content of this article can help you build LAMP with docker, if you have any questions, you can leave a message to communicate.


Related articles: