Home >Backend Development >PHP Tutorial >PHP local development environment docker installation

PHP local development environment docker installation

不言
不言Original
2018-05-09 10:14:394586browse

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 帐号设置密码
没了。

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

3. Test:

We create a new index.php in the app directory

<?php 
phpinfo();

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

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 这样就开启了这个扩展

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 类似,每次新增扩展 都需要重新启动一下。

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