[Docker] Build ssh + nginx + fpm image and realize self-starting after booting through supervisor

Posted May 27, 20204 min read

Written in front

Docker container orchestration may be more suitable for Docker Compose and Kubernetes. In daily development, you don't want to be so complicated, just throw it in a container.

Create mirror

There are two ways to create an image

  1. Update the image from the created container
  2. Use the Dockerfile instruction to create a new image

Here we choose the first method and use the official centos7 image as the initial image.

Pull initial image(Centos7)

docker pull centos:7

Start container

docker run -itd centos:7/bin/bash
  • \ -i indicates interactive display operation
  • \ -t means open a terminal
  • \ -d runs in the background
  • centos:7 is our mirror name
  • /bin/bash specifies an interactive window, one must have a window to start the container, otherwise the container will automatically close

Enter the container

docker exec -it ab3d851192b2/bin/bash
  • ab3d851192b2 is the CONTAINER ID we just started

Start to install software

Before installing the software, let's update the system

yum update -y

Install some commonly used tools, the following is used

yum -y install wget bzip2 gcc gcc-c ++ pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel curl curl-devel

Install ssh(server and client)

yum -y install passwd openssh-server openssh-clients

Modify configuration file

vi/etc/ssh/sshd_config

Find PermitRootLogin yes, remove the preceding comment symbol #, and allow the root user to log in, because it is used for daily development, use root, which can save a lot of things

Set a password for the root user

passwd root

Test if the service can start normally

/usr/sbin/sshd -D

Oh wrong

Could not load host key:/etc/ssh/ssh \ _host \ _rsa \ _key
Could not load host key:/etc/ssh/ssh \ _host \ _ecdsa \ _key
Could not load host key:/etc/ssh/ssh \ _host \ _ed25519 \ _key
sshd:no hostkeys available-exiting.

Prompt for lack of available hotskeys, it doesn't matter, create them

ssh-keygen -t rsa -f/etc/ssh/ssh_host_rsa_key
ssh-keygen -t ecdsa -f/etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -f/etc/ssh/ssh_host_ed25519_key

Try again, it's ok

Install nginx

Create a program user

useradd nginx

We use the source code installation method to download the source code

cd/usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -xvf nginx-1.18.0.tar.gz

cd nginx-1.18.0

./configure --prefix =/usr/local/nginx --with-http_ssl_module

make && make install

Modify environment variables

echo 'export PATH =/usr/local/nginx/sbin:$PATH' >>/etc/profile

source/etc/profile

Modify nginx configuration

cd/usr/local/nginx/conf

mkdir conf.d # Create this directory to put our configuration file

vi nginx.conf

Amend the following three
image.png

user nginx;
daemon off;
include /usr/local/nginx/conf/conf.d/*.conf;

Install php

cd/usr/local/src
wget https://www.php.net/distributions/php-7.3.18.tar.bz2

tar -xvf php-7.3.18.tar.bz2

cd php-7.3.18

./configure --prefix =/usr/local/php --enable-fpm --with-fpm-user = nginx --with-fpm-group = nginx --enable-inline-optimization --enable-shared- enable-soap --enabbe-opcache --enable-pdo --with-mysqli = mysqlnd --with-pdo-mysql = mysqlnd --with-libzip --with-zlib --with-openssl --with-curl- -enable-mbstring

make && make install

Modify environment variables

echo 'export PATH =/usr/local/php/bin:$PATH' >>/etc/profile

source/etc/profile

Modify php configuration

cd/usr/local/php/etc
cp php-fpm.conf.default cp php-fpm.conf
vi php-fpm.conf

# Find the following line and open the previous comment
pid = run/php-fpm.pid

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

Install supervisor

Install directly with yum

yum install -y epel-release
yum install -y supervisor

Modify configuration file

vi /etc/supervisord.conf

# The following lines of comments are opened. This is to manage the supervisor as a web, modify the port to port = 0.0.0.0:9001, and modify the username and password to your own
[inet_http_server]; inet(TCP) server disabled by default
port = 0.0.0.0:9001;(ip_address:port specifier, *:port for all iface)
username = user;(default is no username(open server))
password = 123;(default is no password(open server))

# Find nodaemon and change it to true(because docker itself is a service, it is not allowed to talk about other resident services, all applications are the same)
nodaemon = true

Configure sshd to start the service

cd /etc/supervisord.d/

vi sshd.ini

# Fill in the following content, just save it
[program:sshd]
command =/usr/sbin/sshd -D

Configure nginx + fpm to start the service

vi webserver.ini

# Fill in the following and save
[program:nginx]
command =/usr/local/nginx/sbin/nginx
stopsignal = QUIT

[program:php-fpm]
command =/usr/local/php/sbin/php-fpm --nodaemonize
stopsignal = QUIT

Submit image

At this point, our image is completely updated, we save this container as an image

docker commit -m = "nginx + fpm" -a = "actors315" ab3d851192b2 actors315/webdev:v1
  • Descriptive information submitted by \ -m

  • \ -a author

  • ab3d851192b2 our CONTAINER ID

  • actors315/nginx-fpm:v1 mirror name

    docker images

You will find that our mirror has been built

Use mirroring

docker run --name lingyin-dev -p 9001:9001 -p 2222:22 -p 80:80 -p 443:443 -v F:\ www:/data/www -itd actors315/webdev:v1/usr/bin/supervisord
  • -name specifies the container name
  • \ -p Local and container port mapping, can have multiple
  • \ -v Mount directory, there can be multiple

Check the effect

docker exec -it/bin/bash
ps -ef | grep sshd
ps -ef | grep nginx
ps -ef | grep php-fpm

Browser visit http://127.0.0.1 :9001/You can also see that the services are started normally

Visit http://127.0.0.1/ the familiar nginx welcome page.

You're done here.

If you want to share the image, you can also submit it to the public warehouse

docker push actors315/webdev:v1