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 create a lnmp environment in docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to create a lnmp environment in docker? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

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 MySQL-- name mysql-d\-p 3306 var/lib/mysql/:/var/lib/mysql/ 3306\-v / var/lib/mysql/:/var/lib/mysql/\-e MYSQL_ROOT_PASSWORD=ilovec\

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 data/wwwroot/:/data/wwwroot/ 9000\-- name php-fpm\-- link mysql\-v / data/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 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 b41d2569c06d

3.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 php-fpm through link.

Docker run-d-p 80:80\-- name nginx\-- link php-fpm\-v / data/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: mysql: image: "mysql" ports:-"3306 image 3306" volumes:-/ var/lib/mysql/:/var/lib/mysql/ environment: MYSQL_ROOT_PASSWORD: password php-fpm: image: "php-fpm" depends_on:-mysql links:-mysql ports:-"9000Para9000" Volumes:-/ data/wwwroot/:/data/wwwroot/ nginx: image: "nginx" depends_on:-php-fpm links:-php-fpm volumes:-/ data/wwwroot/:/data/wwwroot/ ports:-"80:80"

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 container associated with the service $docker-compose rm# run the service that already exists docker-compose $docker-compose start after reading the above, do you know how to create a lnmp environment in docker? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Servers

Wechat

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

12
Report