Table of Contents
docker php local development environment
Preface
mysql installation text
php installation text
1. Preparation work
2. Start
3. Test:
4. Install the extension
1. Enter the container to install the extension
2. External installation expansion
Home Backend Development PHP Tutorial PHP local development environment docker installation

PHP local development environment docker installation

May 09, 2018 am 10:14 AM
docker php Install

This article mainly introduces the docker installation of PHP local development environment, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

docker php local development environment

Lu Xun: Programmers who don’t want to make products are not easy to sell

Preface

Many people configure docker because they don’t understand the directory structure inside it , configuration issues, which will lead to a lot of confusion.

But, boy, if you read this article, you are right. I will use "short and concise" language to quickly lead you into the docker pit. There may be something written in the article that is inconsistent with your values. Please use your little fist to hit your ctrl F4. I am a person who cannot stand scolding. If you scold me (will you give me a chance to scold me? Beat me to death first) (manual funny).

It is strongly recommended that csdn add emoji expressions.

mysql installation text

Mysql installation is relatively simple.

[root@test app]#  docker pull mysql:5.7
[root@test app]#   docker run - -name mysql_server -p 3308:3308 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql:5.7
命令解释 

# -e 内置环境变量 这里是给ROOT 帐号设置密码
没了。
Copy after login

However, our installation needs to install mysql first and then install php because –link is required between containers. There is interactive communication between the two containers. otherwise. Hehe, you know. Mysql cannot be connected in php. The command explanation is given below. .

php installation text

1. Preparation work

Because the subject’s computer is an ubuntu system, some of the commands in this article are unified for ubuntu. In addition to installation, it seems There is nothing incompatible with other systems.
docker installation,
windows installation
linux
mac

1, docker environment
2, php:7.2.4-fpm image (this should be based on the environment of your project ) Official image
3, MySql official image (it depends on your mood)

(funny) Some readers may ask, why not nginx|apache, because it simplifies your operation. It allows you to get started with installation faster, and also allows you to write code faster.

When you have downloaded docker, start docker

PHP local development environment docker installation
When docker is turned on, docker version prints the Server information. If it is not turned on, it will not print.
Please open docker

2. Start

1、[root@test app]# docker pull php:7.2.5-fpm              #docker pull 镜像,从docker镜像中拉取某个镜像 
2、[root@test app]# docker images                          #docker 当前所有的镜像名字 imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/php       7.2.5-fpm           e6970efc6d34        3 days ago          367 MB
3、[root@test app]# docker run -d -p 8080:8080 --link mysql_server:mysql_server -v ~/app:/app -w /app php:7.2.5-fpm php -S 0.0.0.0:8080 -t /app 
命令解释
# -d 后台默认启动
# -p 映射端口8080 映射到本机8080  使用方式 本地端口:容器端口
# -v 挂在目录 ~/app 挂载到容器里面 /app目录 
# -w 工作目录 /app目录 相当于cd (在这里,我们可以不用)
# --link  连接容器    容器名:内部使用的名字  
# php:7.2.5-fpm 镜像名
# php -S 0.0.0.0:8080 -t /app  php自带cli Server  用这个可以免除nginx|apache 安装,指定端口为8080 。
4、[root@test app]# docker ps   #查看正在运行的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
9354f9338e29        php:7.2.5-fpm       "docker-php-entryp..."   4 minutes ago       Up 4 minutes        0.0.0.0:8080->8080/tcp, 9000/tcp   naughty_fermi

这里我们可以看出,NAMES 下是容器名,当我们没有指定的时候,docker 会自动创建一个容器名。
PORTS 端口,0.0.0.0:8080->8080/tcp 本机8080 映射到容器8080
Copy after login

3. Test:

We create a new index.php in the app directory

<?php 
phpinfo();
Copy after login

Open 127.0.0.1:8080 You can see the familiar phpinfo

4. Install the extension

If you don’t need to install the extension, please see the note! ! !

Here we explain 2 ways to install extensions

1. Enter the container to install the extension

1. View the docker container name

[root@test app]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
9354f9338e29        php:7.2.5-fpm       "docker-php-entryp..."   31 minutes ago      Up 31 minutes       0.0.0.0:8080->8080/tcp, 9000/tcp   naughty_fermi
Copy after login

We will see here When it comes to docker, the name is naughty_fermi
2. Enter the container

1.进入容器
[root@test app]# docker exec -i -t naughty_fermi /bin/bash
root@9354f9338e29:/app# 
#这样子,看到我们就进入了正在运行的容器命令解释
# docker exec  进入正在使用的容器
# -i :即使没有附加也保持STDIN 打开 一般和-t合作使用
# -t :分配一个伪终端  一般和-i 合作使用  
# /bin/bash 打开交互式终端终端
2. [root@test app]# docker- 按两下tab 可以看到
docker-php-entrypoint     docker-php-ext-configure  docker-php-ext-enable     docker-php-ext-install    docker-php-source 

#docker 根据一些常用库 已经给我们写好了一些脚本 docker -php  github 地址[github]( 
3.安装扩展 
举例:sockets 
[root@test app]#  docker-php-ext-install sockets 
静静等待他编译安装
[root@test app]# php -m  # 就能够看到sockets库了。
4.退出容器 
退出容器的方法有点特殊,需要ctrl+p  再crtl + q 这样,才能在后台继续挂起

root@9354f9338e29:/app# [root@test app]# 
[root@test app]# 
4.我们需要重启一下我们cli server 
[root@test app]# ps -ef | grep php 
root     11840 11808  0 17:04 ?        00:00:00 php -S 0.0.0.0:8080 -t /app
root     14923  9900  0 17:54 pts/1    00:00:00 grep --color=auto php

找到我们的 php cli-server pid 为11840 docker 里面的这些进程,是在本机里面能够看到的。这里是解释 [解释](http://dockone.io/question/529)
我们回到了我们的本机上。
[root@test app]# kill -9 11840  # 杀死我们的进程
因为我们杀死了我们的进程,所以 php 也会自动关闭

我们从新开启这个 容器 

比如 上面可以看到 我们NAMES 为 naughty_fermi
[root@test app]# docker start naughty_fermi 这样就开启了这个扩展
Copy after login

This way our extensions have been installed

Note: Some extensions require some dependencies. In our Install some extensions, which may require some dependencies. When connecting in the php code, mysql host cannot use 127.0.0.1 or localhost. Replace the link with the container name of mysql_server mysql.

2. External installation expansion

1. [root@test app]
#  docker exec -d naughty_fermi docker-php-ext-install opcache#这里我们用opcache 为例
docker exec 不用解释了吧。上面有
# -d 后台默认
# 容器名 后面是在容器里面运行的 命令
2.重复内部安装的 4操作 就可以了cli-server 和php-fpm 类似,每次新增扩展 都需要重新启动一下。
Copy after login

After we configure it for the first time, we need docker start container name to open it later. Here are a few commands

1. docker start container name to start the container
2. docker stop container name to stop the container
3. docker kill container name to kill the container

You can specify - -name container in docker run Name to give the container a name, for example

docker run -d -p 8080:8080 - -name php_server -v ~/app:/app -w /app php:7.2.5-fpm php -S 0.0. 0.0:8080 -t /app
In this way, we can define the container name by ourselves

Others. If you have any questions, please send me an email. My email is

uyy2244@gmail.com

Remember to explain the problem in detail. Otherwise, ignore it. .

Remember: This article only applies to local development environments.

The above is the entire content of this article. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

The 6 best development environment recommendations for building a php local development environment


The above is the detailed content of PHP local development environment docker installation. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

See all articles