Docker Getting In Depth - docker install and set up lnmp (Dockerfile)

First we need to install docker environment, this is relatively simple, for example to centos7

  docker installed on centos7 need kernel version 3.10+, you can check the version number of the kernel by uname -r, please check if the version does not match the information on their own replacement.

  When you are ready, use the following command to install docker Service

yum install -y docker

  Start the installation is complete docker service, the following is docker start, stop, restart commands

systemctl restart docker # restart 

systemctl stop docker # stop 

systemctl start docker # start 

systemctl enable docker # boot from the start 

systemctl status docker # service status

  So far docker service is ready, we can see the version by docker docker -v

 

Then we use to build a php environment docker

  We briefly explain some of the commonly used commands docker, as follows:

# From docker Mirror warehouse search mirror, an example of docker Search MySQL 
docker Search [need to find a mirror name] 

# pull remote mirroring to a local, example docker pull MySQL: 5.7 . 23 
docker pull [image name]: [version] 

# self definition creates a mirror, pay attention to the need for a Dockerfile file in the current directory 
. "" there is a note at the end of # is a must 
Docker Build - t [custom image name]. 

# View the local mirror, you can see the image name, image id , version number, creation time and image size 
Docker ImagesRF Royalty Free 

# remove mirror, the need to pay attention to delete the image to be no container occupied, mirroring id not all enter the full 
# example Docker Image RM 7BC 
Docker Image RM [mirror id] 

# view all container and details you can see the name of the vessel, container id, container status information 
Docker PS - A 

# launch container, which id does not need to write the whole, start 88D example Docker 
Docker start [container id] 

# halt container 
Docker STOP [container id]
 
# restart container 
docker restart [containers ID] 

# Create a mirror, a detailed description will follow 
docker run [Parameter 1] [2 parameter] ... mirror name 

# container into the interior of the interactive 
Docker Exec Expediting IT [ container name / container id] / bin / bash

  Docker entire environment can generally be understood as a three-step process to obtain image -> Create Container -> Run vessel.

 

Download php image, php-fpm

  First, we need a php mirror image acquisition, there are two ways, one is direct access to the image library docker inside an existing image, and the other is his custom file editor dockerfile a mirror, first of all we use the first method, because relatively simple, behind second way to say

  We use docker pull got me to an existing php mirror, I recommend leleos / php-fpm: 7.1 Mirror, basically on the expansion are configured to use the

docker pull leleos/php-fpm:7.1

  

Construction of nginx Mirror

  Before us is the direct download other people make use of a mirror, the mirror of our own to create a custom file using dockerfile, first create a "Dockerfile" file in the directory (recommended Dockerfile as the file name), writes the following

# Set the container base image, here we centos system based mirroring 
the FROM hub.c. 163 .com / public / centos: Latest 

# set of information 
MAINTAINER Taurus12C < 1402410174 @ qq.com> 

# installation depends 
RUN RPM --rebuilddb && yum  install -y autoconf automake the make  wget proc-devel-Tools NET zlib zlib-devel the make  gcc   G ++ OpenSSL-devel PCRE PCRE-devel tar 

# download software compression package nginx 
RUN wget HTTP: // nginx.org/download/nginx-1.17 .1.tar.gz 
 
# extract to the current directory 
RUN tar -zxvf nginx- 1.17 . 1 .tar.gz
 
# 设置当前操作目录
WORKDIR nginx-1.17.1
 
# 配置nginx
RUN ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -rf ../nginx* && yum clean all \
        && echo "${TIME_ZOME}" > /etc/timezone \
        &&
CMD [
# execute the statement after the starting container80
EXPOSE
# exposed container portNginx/ usr / local /
the WORKDIR
# sets the current directory operationlocaltime-SF5 / usr / Share / zoneinfo /} $ {TIME_ZOME / etc /LN
 
            
"./sbin/nginx","-g","daemon off;"]

  Here we generally talk about the meaning of the above command files, command attention must be all uppercase

  FROM - Set the base image

  MAINTAINER - set this dockerfile author information

  RUN - the more crucial, since the configuration is for the rest of your container or the operation step by step, because we are here to create a mirror image of nginx, so the above operations are RUN install nginx service inside the container

  WORKDIR - Set the current path in the vessel operation, similarly to the operation cd

  EXPOSE - this is to expose container port allows the host to complete the mapping

  CMD - This command is executed automatically start after the container is created, here is the command to start the service mean nginx

  Other commands can go to access information on their own to understand, here we will explain them, so that we have a concept

  After editing Well we execute build in the current directory to build a mirror, if your file is Dockerfile directly executed just fine. Note the back. "" Do not forget

docker build -t nginx .

 After we finished using a docker images to view local mirror've got nginx and leleos / php-fpm two mirrored. Then we can use the container to create a php environment php run the project

 

Create a custom network dcoker

  Sometimes we need to be able to connect between a container, such as nginx need to use fpm, but nginx and leleos / php-fpm is not inside a container, this time we need to find a way to be able to access each container, there are several Method

  1, when starting container --link connection between the container and then modify the fastcgi_pass nginx configuration file which corresponds to the address in the container nginx

  2, the acquired network ip php-fpm container, and then modify the fastcgi_pass nginx configuration file which corresponds to the address in the container nginx

  3, create a custom network docker, then modify the fastcgi_pass nginx configuration file which corresponds to the address in the container nginx

  Here the authors recommend using the third method, the other two if necessary, inform themselves about

  Create a custom network docker command, docker network create [Network Group Name]

docker network create lnmp

  After the execution we can see the network group you just created with docker network ls

 

Create a container, use docker build PHP environment

  It says so many are ready to work, and now we have to build up a formal environment

  Create a container nginx

docker run -itd --name nginx --network lnmp -p 80:80 -v /var/www/html:/var/www/html -v /usr/local/nginx/conf/vhost:/usr/local/nginx/conf/vhost nginx

  Explain the meaning of the above Directive

  docker run: Start create a container

  -itd: Here is an abbreviation, the corresponding operation -i -t -d, -i interactive operation, -t terminal, -d background

  --name: to specify the name of the vessel

  --network: container to a designated network group

  Mapping host ports in the container: -p

  -v: a container mount directory, it is to be understood that the popular container storage space add files, and the files within the host vessel and the space is common. Here I mount the two directories, is a project directory html, is a profile directory vhost, so do not enter the vessel will be able to modify the

  nginx: specify the mirror image of the container used is nginx

  

  Create php container service

docker run -itd --name php-fpm --network lnmp -p 9000:9000 -v /var/www/html:/var/www/html leleos/php-fpm:7.1

  Note that the project directory needs nginx and php container configuration in a directory that body oh. So far we have launched a container, you can docker ps -a view we just created by the state and container started.

  To not end here as well as the final step of the operation, that is correct nginx configuration. First, we nginx into the container inside docker exec -id [vessel name / container id] / bin / bash

docker exec -it nginx /bin/bash

  Enter the above command we can enter into this nginx inside the container, if you need to exit to return to the host inside, just type exit in the container on it

  Enter / usr / local / nginx / conf directory editor nginx.conf file, as follows

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;                   #域名
      index index.html index.htm index.php;
        root  /var/www/html;                     # Here to fill just mount the project directory 
        #charset KOI8 - r; 

        #access_log logs / host.access.log main; 

        #error_page   404               / 404 .html; 

        # to redirect Server Pages and the error at The static Page / 50x.html 
        # 
        error_page    500  502  503  504   / 50x.html; 
        LOCATION = / 50x.html { 
            the root HTML; 
        } 

        # The Proxy the PHP scripts to the Apache Listening ON 127.0 . 0.1 : 80 
        #
        # location ~ \ .php $ { 
        # proxy_pass HTTP: // 127.0.0.1; 
        #} 

        # Pass at The PHP scripts to FastCGI Server Listening ON 127.0 . 0.1 : 9000 
        # 
        
      LOCATION ~ \ .php $ { 
            root HTML; 
            fastcgi_pass lnmp_php: 9000 ;             # Note the use php container port here within the network group, network group to use when creating and mapping of our php container port 
            fastcgi_param SCRIPT_FILENAME / var / the WWW / HTML $ fastcgi_script_name; 
            the include fastcgi_params; 
        } 
          # .htaccess deny Access to Files,IF the Apache ' S the root Document 
          # concurs with Nginx ' S One 
          # 
          #location ~ / \ .ht { 
          # All the deny; 
          #} 
      } 
    the include / usr / LOCA / Nginx / the conf / Vhost / * .conf;   # here we hang to upload a profile directory address, so that we do not need to add new items to the container nginx.conf to modify the file, restart only need to modify the container on the host to

 
    # another virtual host using mix of IP- , name-, and -based Configuration Port 
    # 
    #server { 
    # the listen 8000; 
    # the listen somename: 8080; 
    # server_name somename Alias another.alias; 
 
    # LOCATION / {
    # the root HTML;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

  After editing Well, we reload nginx configuration files, into the implementation ./nginx reload under / usr / local / nginx / sbin directory. Then exit the container exit

  Entry / index.php create test under var / www / html directory look into whether to build a successful change, written in index.php file

<?php

    echo 'Hello World!';

  Open a browser to access the server address or domain name, if the output "Hello World!" On behalf of the environment to build success

 

other

  So far we've got some usage docker, you can try to build their own redis, mysql services such as container you, if you encounter any problems you can give me a comment below, or add our exchange group buckle group: 735,713,840 (Note blog Park) were ask

Guess you like

Origin www.cnblogs.com/Taurus12C/p/taurus12c-docker-php-lnmp.html