Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build lnmp environment in docker

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to build a lnmp environment in docker. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1.MySQL

After that, the images run by the service are all pulled from the government if there is no special prompt. For small businesses and individual developers, the official images are more secure and save effort and effort.

# pull the mirror $docker pull mysql# and run MySQL$ docker run-- name lnmp_mysql-d-p 3306 name lnmp_mysql-v ~ / Public/docker/mydocker/mysql:/var/lib/mysql/-e MYSQL_ROOT_PASSWORD=123456 mysql

Let's explain the above running parameters in turn.

-- name: the name of the specified running container

-d: the container is run in the background

-p: mapping between the host and the port of the container

-v: the container is mounted to the local directory mapping

-e: specify the environment variable that runs the container

2.PHP

Pull the official image php-fpm and download it according to the php version you need, but some packages commonly used in php are not included in the official image, so we need to use dockerfile to rebuild it. The following is to install mysqli and pdo php expansion packages in Dockerfile.

FROM php:7.1-fpm# Install modulesRUN docker-php-ext-install mysqli & & docker-php-ext-enable mysqliRUN docker-php-ext-install pdo_mysqlCMD ["php-fpm"]

Run php-fpm

Docker run-d-p 9000 link lnmp_mysql 9000-- name lnmp_php-- link lnmp_mysql-v ~ / Public/docker/mydocker/wwwroot:/data/wwwroot/ php-fpm

Pay attention to the parameter-- link. This parameter is very useful for connections between containers. He will add a domain name resolution to the current container's / etc/hosts, through which you can connect to the corresponding container. For example, in the above php-fpm, link to lnmp_mysql, then the php program in php-fpm can connect to the mysql container you just ran through the mysql string. Cat / etc/hosts can see the parsing record inside.

172.17.0.2 mysql b41d2569c06d3.Nginx

Run nginx with the following command, and since nginx needs to connect to php-fpm through port 127.0.0.1 to parse the php file, you need to connect to lnmp_php through link.

Docker run-d-p 80:80-- name lnmp_nginx-- link lnmp_php-v ~ / Public/docker/mydocker/wwwroot:/data/wwwroot/ nginx

It is worth noting that if nginx parses a file, if the request is a static file, it will directly return the file in the nginx container to the client. If the request is a php file, he will forward the request to php-fpm, and then php-fpm will go locally to find the php file for parsing, that is, the file of the php-fpm container itself.

After running the startup commands of the above three services in turn, we can build our common lnmp. But it is a bit troublesome to run the above command every time the service is run, and we can use the docker-compose command for centralized management.

Use docker-compose

Just create a lnmp directory and then create a docker-compose.yml under the lnmp directory and enter the following command to manage the integrated environment.

In fact, it is easy to know the meaning of each instruction by the name of the command.

Version: since docker-compose is a developing tool, it is likely that each version of the directive will be different, so you need to declare the applicable version of the docker-compose directive at the beginning.

Image: refers to the image through which the service runs.

Depends_on: this indicates which software the software depends on, and actually declares the order in which the software runs.

Version: '2'services: lnmp_mysql: image: "mysql:latest" ports:-"3307 image 3306" volumes:-"~ / Public/docker/mydocker/mysql:/var/lib/mysql" environment: MYSQL_ROOT_PASSWORD: 123456 lnmp_php: image: "nginx:latest" depends_on:-lnmp_mysql links:-lnmp_mysql ports: -"9010 image 9000" volumes:-"~ / Public/docker/mydocker/wwwroot:/data/wwwroot" lnmp_nginx: image: "nginx" depends_on:-lnmp_php links:-lnmp_php volumes:-"~ / Public/docker/mydocker/wwwroot:/data/wwwroot" ports:-"8089 image"

Then you can control it by executing compose-related commands in this lnmp directory.

# run the docker-compose service $docker-compose up-d # stop the service $docker-compose stop# delete the service-related container $docker-compose rm# run the existing docker-compose service $docker-compose start on how to build a lnmp environment in docker, so much for sharing here. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report