How to build php-nginx-alpine image from scratch in Docker

How to build php-nginx-alpine image from scratch in Docker

Although I have run some projects in Docker environment before, I still don’t understand the image very well. In addition, the existing images on the Internet contain too many unused libraries, so I decided to build my own image from scratch.

Alpine Linux is the base image

docker pull gliderlabs/alpine

alpine linux mirror address

The latest tag is version 3.8, and the image is only 4M in size, which is very streamlined.

Run the image

docker run -it gliderlabs/alpine

After running, you can see that there is a basic Linux file system. I won’t demonstrate it here, but those who are interested can try it by themselves.

Install PHP and nginx

apk update
apk add php7 nginx

Next, you can continue to install PHP-related extension packages or other Linux extension packages according to your needs.

To search for extension packages, such as the PHP7 toolkit, use the following command:

# apk search php7

Install PHP extension

Copy the code as follows:
apk add php7-mysqli php7-pdo_mysql php7-mbstring php7-json php7-zlib php7-gd php7-intl php7-session php7-fpm php7-memcached

Directory Structure

  • php7 directory: /etc/php7
  • nginx directory: /etc/nginx

Start php-fpm and nginx

# Create the pid file first, otherwise nginx cannot run mkdir /run/nginx 
touch /run/nginx/nginxpid

# Run php first, then run nginx
/usr/sbin/php-fpm7
/usr/sbin/nginx

At this point, we can see that PHP and nginx are running normally, and the mirroring environment has been basically completed. Next, let’s see how to save this image.

Create an image

Open another terminal and use docker ps to view the image ID. The following command can save the image:

docker commit -a "yisonli" -m "my first php7-nginx" 9d9c6030e5e9 yisonli/php7-nginx-alpine:0.1

Note: 9d9c6030e5e9 is the image ID, yisonli/php7-nginx-alpine is the saved image name, and the version is defined as 0.1

Run the newly generated image

Bind port 8080 to see the effect

docker run -it -p 8080:8080 yisonli/php7-nginx-alpine:0.1

After manually starting php-fpm and nginx, the effect can be seen in the browser.

http://127.0.0.1:8080/index.php

Docker additional records

【Shared folder】

docker run -v `pwd`/www:/var/www/html -it -p 8080:8080 yisonli/php7-nginx-alpine:0.1

Deleting non-running containers

docker rm $(docker ps -a -q)

[Build an image using Dockerfile]

docker build -t yisonli/php7-nginx-alpine:0.2 .

0.2 is built after slightly optimizing the process in this article and writing it into a Dockerfile, and it also comes with a startup script.

It has been uploaded to Docker Hub and is available for download and research.

Add additional tags to the image

docker tag 9d9c6030e5e9 test/mytag

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Alpine Docker image font problem solving operations
  • Implementation of tomcat image created with dockerfile based on alpine
  • Implementation of crawler Scrapy image created by dockerfile based on alpine
  • Perfect solution to Docker Alpine image time zone problem

<<:  js to achieve star flash effects

>>:  Vue code highlighting plug-in comprehensive comparison and evaluation

Recommend

JavaScript Interview: How to implement array flattening method

Table of contents 1 What is array flattening? 2 A...

Implementing Markdown rendering in Vue single-page application

When rendering Markdown before, I used the previe...

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...

Comparison of various ways to measure the performance of JavaScript functions

Table of contents Overview Performance.now Consol...

Using JavaScript in HTML

The <script> tag In HTML5, script has the f...

MySQL 5.7.17 installation and configuration tutorial for Mac

1. Download MySQL Click on the official website d...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

Some ways to eliminate duplicate rows in MySQL

SQL statement /* Some methods of eliminating dupl...

Several ways to hide Html elements

1. Use CSS Copy code The code is as follows: style...

Detailed explanation of slave_exec_mode parameter in MySQL

Today I accidentally saw the parameter slave_exec...

6 inheritance methods of JS advanced ES6

Table of contents 1. Prototype chain inheritance ...

Detailed explanation of MySQL Explain

In daily work, we sometimes run slow queries to r...