First of all, we need to install the docker environment. This is relatively simple. Take centos7 as an example.
docker needs kernel version 3.10 + to install on centos7. You can check the kernel version number through uname-r. if the version does not match, please consult the data for replacement.
When you are ready, use the following command to install the docker service
yum install -y docker
After installation, start the docker service. Here are the commands for docker start, stop, restart, etc
systemctl restart docker #restart
systemctl stop docker #Stop it
systemctl start docker #start-up
systemctl enable docker #Power on self start
systemctl status docker #Service state
Now that the docker service is ready, we can view the docker version through docker-v
Then we use docker to build php environment
Let's briefly introduce some common commands of docker, as follows:
#Search image from docker image warehouse, for example, docker search mysql docker search [Image name to find] #Pull remote mirror to local, example docker pull mysql:5.7.23 docker pull [Mirror name]:[Version number] #Create a customized image. Note that there should be a Dockerfile file in the current directory #Note that a "." is required at the end docker build -t [Custom image name] . #To view the local image, you can see the image name, image id, version number, creation time and image size docker images #To delete an image, please note that the deleted image does not occupy the container, and the image id does not need to be entered completely #Example docker image rm 7bc docker image rm [image id] #View all containers and details, and you can see container name, container id, container status and other information docker ps -a #Start the container, and the container id does not need to be written completely. For example, docker start 88d docker start [container id] #Stop container docker stop [container id] #Restart container docker restart [container id] #Create a mirror image, which will be described in detail later docker run [Parameter 1] [Parameter 2] ... Mirror name #Enter container internal interaction docker exec -it [Container name/container id] /bin/bash
The whole docker environment process can be roughly understood as three steps: obtain image - > create container - > Run container.
Download php image, php FPM
First of all, we need a php image. There are two ways to obtain the image. One is to directly obtain the existing image in the docker image library. The other is to edit the dockerfile file and customize an image. First, we use the first way, because it is relatively simple, and the second way is later
We use docker pull to pull out an existing php image. Here I recommend the leleos / php FPM: 7.1 image. Basically, all the extensions above are configured
docker pull leleos/php-fpm:7.1
Building nginx image
Previously, we downloaded other people's images for use. This time, we used the Dockerfile file to create a custom image. First, we created a "Dockerfile" file in the directory (it is recommended that the Dockerfile be used as the file name). Write the following
#Set the container basic image. Here we take centos system as the basic image FROM hub.c.163.com/public/centos:latest #Set author information MAINTAINER Taurus12C<1402410174@qq.com> #Installation dependency RUN rpm --rebuilddb && yum install -y autoconf automake make wget proc-devel net-tools zlib zlib-devel make gcc g++ openssl-devel pcre pcre-devel tar #Download nginx package RUN wget http://nginx.org/download/nginx-1.17.1.tar.gz # Extract to current directory RUN tar -zxvf nginx-1.17.1.tar.gz # Set current operation directory WORKDIR nginx-1.17.1 # Configure nginx RUN ./configure --prefix=/usr/local/nginx && make && make install RUN rm -rf ../nginx* && yum clean all \ && echo "${TIME_ZOME}" > /etc/timezone \ && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime # Set current operation directory WORKDIR /usr/local/nginx #Exposed container port EXPOSE 80 #Execute statement after starting container CMD ["./sbin/nginx","-g","daemon off;"]
Let's roughly explain the command meaning of the above file. Note that the command must be all capitalized
FROM - set basic image
MAINTAINER -- set the author information of this dockerfile
RUN - this is the key step for you to configure or depend on the container step by step. Because we are creating a nginx image here, all the above RUN operations are to install nginx services in the container
WORKDIR -- set the path in the current operation container, similar to cd operation
EXPOSE -- this is to EXPOSE the port of the container for the host to complete the mapping
CMD -- this is the command automatically executed after container creation and startup. The command here is to start nginx service
For other orders, you can refer to the information and learn by yourself. Here we will only explain these, so that you can have a concept
After editing, we will execute build in the current directory to build an image. If your file is Dockerfile, just execute it directly. Pay attention to the "." at the back. Don't forget
docker build -t nginx .
After execution, we use docker images to check that the local image has two images: nginx and leleos / php FPM. Then we can use the container to create a php environment to run the php project
Create a dcoker custom network
Sometimes we need to be able to connect containers. For example, nginx needs to use fpm, but nginx and leleos / PHP fpm are not in the same container. At this time, we need to find ways to make containers access each other. Here are several ways
1. When starting the container, use -- link to connect the containers, and then modify the corresponding address of fastcgi ﹣ u pass in the nginx configuration file in the nginx container
2. Obtain the intranet ip of the PHP FPM container, and modify the corresponding address of fastcgi ﹣ u pass in the nginx configuration file in the nginx container
3. Create a docker custom network, and then modify the fastcgi ﹐ pass address in the nginx configuration file in the nginx container
Here, the author recommends the third method. The other two methods can be understood by yourself if necessary
Create docker custom network command, docker network create [network group name]
docker network create lnmp
After execution, we can use docker network ls to view the network group just created
Create a container and use docker to build a PHP environment
It said that so much is preparation work. Now we officially set up the environment
Create nginx container
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 instructions
docker run: create and start a container
- i T d: abbreviation here, corresponding to - i -t -d, - i interactive operation, - t terminal, - d background operation
-- Name: specify a name for the container
-- Network: specify a network group for the container
- p: map the host and ports in the container
- v: to mount a directory to a container is to add a space for storing files to the container, and the host and container of the files in the space are shared. Here I mount two directories, one is the project directory html, the other is the configuration file directory vhost, so it can be modified without entering the container
Nginx: Specifies that the image used by the container is nginx image
Create php service container
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 containers configured in the body directory. At this point, our container is also started. You can view the status of the container we just created and started through docker ps -a.
The last step is to configure nginx correctly. First, we go to the nginx container, docker exec -id [container name / container id] /bin/bash
docker exec -it nginx /bin/bash
Enter the above command to enter the nginx container. If you need to exit and return to the host, just enter exit in the container
Enter the directory / usr/local/nginx/conf to edit the nginx.conf file. The contents of the file are 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; #domain name index index.html index.htm index.php; root /var/www/html; #Fill in the project directory just attached here #charset koi8-r; #access_log logs/host.access.log main; #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass lnmp_php:9000; #Note the port in the NetGroup of the php container. The NetGroup and mapped port are used when we create the php container fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } include /usr/loca/nginx/conf/vhost/*.conf; #Here is the directory address where we mount the configuration file, so we do not need to modify the nginx.conf file in the container to add new items, just modify and restart the container on the host # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # 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, we reload the nginx configuration file and execute. / nginx reload in / usr/local/nginx/sbin directory. Then exit the container
Enter the directory / var/www/html to create index.php to test whether the switch in is built successfully. Write in the index.php file
<?php echo 'Hello World!';
Open the browser to access the server address or domain name. If "Hello World!" is output, the environment will be built successfully
Other
Now that we have mastered some usage of docker, we can try to build redis, mysql and other service containers by ourselves. If you have any problems, please comment on it below, or add our communication group: 735713840 (notes blog Park) for inquiry