Home Backend Development PHP Tutorial Use docker to create an integrated service lnmp environment

Use docker to create an integrated service lnmp environment

Apr 09, 2018 pm 03:14 PM
docker Serve integrated

This article mainly introduces the use of docker to create an integrated service lnmp environment. Now I share it with everyone and provide a reference for friends in need.

After mastering the basic commands of docker, I also wanted to use docker to create some actual supporting environments, so I used my most commonly used lnmp environment for testing. The order of running the supporting environment is mysql->php->nginx. The reason why will be explained below.

1.MySQL

Unless there are special instructions, the images for running services are all pulled from the official images. For small businesses and individual developers, the official The mirror is safer and saves worry and effort.

# 拉取镜像
$ docker pull mysql
# 运行MySQL
$ docker run MySQL --name mysql -d \
    -p 3306:3306 \
    -v /var/lib/mysql/:/var/lib/mysql/ \
    -e MYSQL_ROOT_PASSWORD=ilovec \
Copy after login

The following will explain each of the above running parameters in turn

  1. --name: the specified running container The name

  2. -d: Run the container in the background

  3. -p: The mapping of the host and container ports

  4. -v: Mount the container to the local directory mapping

  5. -e: Specify the environment variable for running the container

2.PHP

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

FROM php:7.1-fpm
# Install modules
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN docker-php-ext-install pdo_mysql
CMD ["php-fpm"]
Copy after login

Run php-fpm

docker run -d -p 9000:9000 \
  --name php-fpm \
  --link mysql \
  -v /data/wwwroot/:/data/wwwroot/ \
  php-fpm
Copy after login

Note the parameters The --link parameter is very useful for connecting between containers. It will add a domain name resolution in /etc/hosts of the current container. Through this domain name, you can connect to the corresponding container, such as the above-mentioned php-fpm Inside, link to mysql, then the php program in php-fpm can connect to the just-running mysql container through the mysql string, and you can see the parsing records inside by cat /etc/hosts.

172.17.0.2 mysql b41d2569c06d
Copy after login

3.Nginx

Run nginx through the following command, because nginx needs to pass 127.0.0.1 :9000 port to connect to php-fpm to parse php files, so 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
Copy after login

It is worth noting that when nginx parses a file, if a static file is requested, the file in the nginx container will be directly returned to On the client side, if the request is for 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, which is the file of the php-fpm container itself.

After running the above three service startup commands in sequence, we can build our common lnmp. However, it is a bit troublesome to run the above command every time you run the service. We can use the docker-compose command for centralized management.

To use docker-compose

you only need to create a lnmp directory, and then create docker-compose.yml in the lnmp directory and enter the following command to manage the integrated environment .
In fact, you can easily know the meaning of each command through the name of the command.

version: Since docker-compose is a developing tool, it is very likely that the instructions for each version will be different, so the applicable version of the docker-compose instructions needs to be declared at the beginning.

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

depends_on: This specifies which software the software depends on. In fact, it also declares the order in which the software runs.

version: '2'

services:
  mysql:
    image: "mysql"
    ports:
      - "3306:3306"
    volumes:
      - /var/lib/mysql/:/var/lib/mysql/
    environment:
      MYSQL_ROOT_PASSWORD: password
      
  php-fpm:
    image: "php-fpm"
    depends_on:
      - mysql
    links:
      - mysql
    ports:
      - "9000:9000"
    volumes:
      - /data/wwwroot/:/data/wwwroot/

  nginx:
    image: "nginx"
    depends_on:
      - php-fpm
    links:
      - php-fpm
    volumes:
      - /data/wwwroot/:/data/wwwroot/
    ports:
      - "80:80"
Copy after login

After that, execute compose related commands in this lnmp directory to control it.

# 运行docker-compose服务
$ docker-compose up -d
# 停止服务
$ docker-compose stop
# 删除该服务相关的容器
$ docker-compose rm
# 运行已存在docker-compose的服务
$ docker-compose start
Copy after login

Related recommendations:

Building a PHP development environment in Docker


The above is the detailed content of Use docker to create an integrated service lnmp environment. 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)

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 <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

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] <Container Path> <Host Path>. 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 <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

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

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

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles