场景:在一台服务器上有一个跑在本机的LAMP服务,因为经常搬迁服务器所以传统搬迁办法非常麻烦,于是打算把这个正在运行的服务迁移到Docker容器里面,方便以后搬迁。
任务:迁移本地PHP应用到Docker容器。
不废话,四步之内解决这件事,备份你的网站。
第一种方法
第一步:拉取镜像。
docker pull tutum/lamp
第二步:运行容器。
在这次实验中,本地应用根目录为/home/wwwroot/default,请根据自己情况修改路径。YOURPASSWORD保持之前的数据库密码。
docker run -d -p 81:80 -p 3307:3306 -e MYSQL_PASS="YOURPASSWORD" -v /home/wwwroot/default:/app tutum/lamp
第三步:修改应用数据库配置文件。
在这个镜像中,容器外部不允许使用root用户连接——你应该使用admin用户!
修改PHP应用的配置文件里面的数据库用户名即可。
第四步:导出本地数据库数据,然后导入容器内的数据库。
太简单,mysql命令行导出导入而已。
以上是第一种方法,这种方法不用动正在运行的程序,做到了无缝切换,但缺点在于不利于将来搬迁服务器时操作,毕竟本地数据还是要自己打包,总之有时间可以用第二种方法。
第二种方法
第一步:写个Dockerfile,自己构建一个LAMP镜像。
打包本地数据,我命名为wwwroot.zip。在打包文件目录中执行vim Dockerfile
FROM tutum/lamp:latestRUN rm -rf /appADD wwwroot.zip /app/RUN apt-get updateRUN apt-get install -y unzipRUN cd /app && unzip wwwroot.zipRUN rm -rf /tmp && rm /app/wwwroot.zipEXPOSE 80 3306CMD ["/run.sh"]
第二步:构建。
docker build -t local/lamp .
第三步:运行容器。
- 首先导出数据库
- 关闭LAMP
- 运行容器(YOURPASSWORD保持之前的数据库密码)
docker run -d -p 81:80 -p 3306:3306 -e MYSQL_PASS="YOURPASSWORD" local/lamp
第四步:数据库配置。
记住,容器外部不允许使用root用户连接——你应该使用admin用户!配置文件只需要修改一个数据库用户名即可。
导出本地数据库数据,然后导入容器内的数据库。
题外话
你可能注意到暴露的是81端口,这是因为我把前端交给Nginx做反代理。
详情:为“复杂”的容器环境配置Nginx反向代理

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

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

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

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools