Application practice of docker entry Foundation

Time:2020-4-24

Application practice of docker entry Foundation

When we have mastered the basic usage of docker image and container, what can we do now? Now let’s see how to use docker container to install common software, and then run a dynamic website.

Let’s learn:

  • 1. Install nginx
  • 2. Install PHP
  • 3. Using MySQL services
  • 4. Run wordpress blog

Install nginx
Run an alpine container. Choose Alpine as the basic image of the system because of its light volume. The basic image is only 5.53mb, which is ten times smaller than the 88.9mb of Ubuntu image.

 [email protected]:~# docker run -it -p 8080:80 alpine sh

Install nginx

 apk add nginx

Modify nginx configuration

 vi /etc/nginx/conf.d/default.conf

The content of default.conf is as follows:

server {
    listen 80 default_server;
 ​
    root /home/www;
    index index.php index.html;
 }

Create Hello World

mkdir /home/www && echo "Hello World" > /home/www/index.html

Create / run / nginx directory

mkdir /run/nginx

start nginx

nginx

Visit http://192.168.43.122:8080 in the browser

Nginx installation succeeded, web service access is normal!
Application practice of docker entry Foundation

Install PHP
Now let’s install PHP in the same wayapk addCommand to install php7, PHP FPM, and related extensions.

apk add --no-cache php7 php7-fpm php7-ftp php7-pdo php7-mysqli php7-simplexml php7-xmlwriter php7-zlib php7-imagick php7-memcached php7-sockets php7-mcrypt php7-zip php7-pgsql php7-pdo_odbc php7-odbc php7-curl php7-iconv php7-xml php7-json php7-gd php7-session php7-opcache php7-pdo_sqlite php7-mbstring php7-common php7-pdo_mysql

Many PHP extensions are installed along with the above, which can be increased or decreased according to the actual demand.

Now start php-fpm7

php-fpm7

Modify nginx configuration and add PHP support

vi /etc/nginx/conf.d/default.conf

The content is as follows

server {
        listen 80 default_server;

        root /home/www;
        index index.php index.html;
        
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /home/www$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
}

Create phpinfo.php in the website directory

echo "<?php phpinfo(); ?>" > /home/www/phpinfo.php

Restart nginx

nginx -s reload

Visit http://192.168.43.122:8080/phpinfo.php in the browser

PHP installation succeeded, web service access is normal!
Application practice of docker entry Foundation

Using MySQL services
We don’t install MySQL in Alpine. In order to use efficiency, we can directly use the official image here. Open a new SSH terminal to connect to the host computer, and create a new database directory under the home directory

mkdir /home/database

Use the official MySQL 5.7 image to run a container, set the root password to 123456, map port 3306, and mount the host / home / database directory to the container / var / lib / MySQL directory

docker run -dit -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /home/database:/var/lib/mysql mysql:5.7

docker psView container

As shown in the figure above, we have two containers running, one is the alpine container with nginx and PHP installed, and the other is mysql5.7 container

Enter MySQL container

docker exec -it c8c85af64822 sh

Log in and create a WordPress database

#Log in as root
mysql -u root -p
#Create WordPress database
mysql> CREATE DATABASE wordpress;

Run wordpress blog
The PHP Website Environment and MySQL database are ready. Now we go back to alpine’s SSH terminal and download a WordPress website program in the container.

#Enter the website directory
cd /home/www/
#Download WordPress
wget https://wordpress.org/latest.tar.gz
#Unzip
tar zxvf latest.tar.gz
#Copy files in WordPress directory to WWW directory
cp -ri /home/www/wordpress/* /home/www/
#Set 777 permissions
chmod -R 777 /home/www/

Visit http://192.168.43.122:8080 in the browser to see the WordPress page
Application practice of docker entry Foundation

Fill in database information
Application practice of docker entry Foundation

Installation is complete, WordPress is in normal use!
Application practice of docker entry Foundation