Skip to content

Commit

Permalink
start adding new files for creating new official Docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed Aug 4, 2019
1 parent 2fcee68 commit b2a6868
Show file tree
Hide file tree
Showing 28 changed files with 856 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
@@ -0,0 +1,21 @@
# Ignore Eclipse/Zend Studio project files
/.buildpath
/.project
/.settings

# Ignore PhpStorm files
/.idea/

# Ignore files under Mac
.DS_Store

# Ignore 3rd party tools
/composer.lock
/composer.phar
/vendor/*

# Ignore PHPUnit files
/.phpunit.result.cache

# Ignore temporary files
/temp/
55 changes: 55 additions & 0 deletions Dockerfile.twig
@@ -0,0 +1,55 @@
FROM {{ image_name }}:{{ php_version }}-cli

ENV DEBIAN_FRONTEND noninteractive
ENV TERM xterm-color

# @see https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
RUN \
apt-get update && \
apt-get install -y \
apt-transport-https \
dirmngr \
gnupg \
software-properties-common \
supervisor \
unzip \
--no-install-recommends && \
apt-key adv --no-tty --keyserver keyserver.ubuntu.com --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 && \
echo "deb http://nginx.org/packages/ubuntu/ bionic nginx" > /etc/apt/sources.list.d/nginx.list && \
apt-get update && \
apt-get install -y \
nginx={{ nginx }} \
--no-install-recommends && \
{% if extensions is not empty %}
pecl update-channels && \
{% for name, data in extensions %}
pecl install {{ name }}{% if data.version is not empty %}-{{ data.version }}{% endif %} && \
{% endfor %}
{% for name, data in extensions %}
{% if data.enabled %}
docker-php-ext-enable {{ name }} && \
{% endif %}
{% endfor %}
{% endif %}
curl \
-sfL \
--connect-timeout 5 \
--max-time 15 \
--retry 5 \
--retry-delay 2 \
--retry-max-time 60 \
http://getcomposer.org/installer | php -- --install-dir="/usr/bin" --filename=composer && \
chmod +x "/usr/bin/composer" && \
composer self-update {{ composer.version }} && \
mkdir -p /var/log/supervisor && \
rm -r /var/lib/apt/lists/* && \
mkdir -p /etc/nginx && \
ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stderr /var/log/nginx/error.log

COPY ./rootfilesystem/ /

ENTRYPOINT ["/entrypoint.sh"]
CMD []

WORKDIR "/var/www/"
15 changes: 15 additions & 0 deletions bin/generate-docker-files.php
@@ -0,0 +1,15 @@
#!/usr/bin/env php
<?php
/**
* How to use the script:
* ./bin/generate-docker-files.php swoole-version
* e.g.,
* ./bin/generate-docker-files.php 4.3.5
* Above command will have dockerfiles created for Swoole 4.3.5 under folder images/dockerfiles/.
*/

use Swoole\Docker\Dockerfile;

require_once dirname(__DIR__) . '/vendor/autoload.php';

(new Dockerfile($argv[1]))->render();
20 changes: 20 additions & 0 deletions composer.json
@@ -0,0 +1,20 @@
{
"name": "swoole/docker",
"description": "Official Docker image packing for Swoole.",
"homepage": "https://www.swoole.com",
"license": "Apache-2.0",
"require": {
"php": ">=7.1",
"crowdstar/reflection": "~1.0.0",
"overtrue/phplint": "~1.1.0",
"phpunit/phpunit": ">=8.0",
"squizlabs/php_codesniffer": ">=3.0",
"symfony/yaml": "~4.3.0",
"twig/twig": "~2.11.0"
},
"autoload": {
"psr-4": {
"Swoole\\Docker\\": "src/"
}
}
}
30 changes: 30 additions & 0 deletions config.yml
@@ -0,0 +1,30 @@
# A sample configuration file for generating Dockerfiles. You may find configuration files under folder images/config/.

# Base name of the configuration file must be of a specific version of image swoole/swoole, e.g.,
# * 4.3.5
# * 4.4.0

# Options for field "status": "under development", "released", "end-of-life".
# * "under development":
# Still under development. Please DO NOT use it in production.
# * "released":
# Released and can be used in production. The tag is frozen and won't be updated any more.
# * "end-of-life":
# There are new releases out and this tag should no longer being used in production.
status: "under development"
php: # List of PHP versions to build for.
- 7.1.29
- 7.2.19
- 7.3.6
image:
# To find out available versions of Nginx, please run commands like
# docker run --rm php:7.3.6-cli bash -c "apt-get update && apt-cache policy nginx"
# or,
# docker run --rm php:7.3.6-cli bash -c "apt-get update && apt-cache showpkg nginx"
nginx: "1.10.3-1+deb9u2"
composer:
version: 1.8.6 # Composer version.
extensions: # List of PECL extensions to be installed.
zip: # An extension name.
version: 1.15.4 # Optional.
enabled: true # Enable the extension or not. By default it's disabled.
12 changes: 12 additions & 0 deletions config/4.3.5.yml
@@ -0,0 +1,12 @@
# The YAML configuration file for generating Dockerfile of image swoole/swoole:4.3.5.
# For technical details of the configuration file, please check comments and sample configurations in file /config.yml.
#
status: "under development"
php:
- 7.1.29
- 7.2.19
- 7.3.6
image:
nginx: "1.10.3-1+deb9u2"
composer:
version: 1.8.6
6 changes: 6 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" verbose="true">
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</phpunit>
36 changes: 36 additions & 0 deletions rootfilesystem/entrypoint.sh
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

set -e

if [[ ! -z "$@" ]] ; then
BOOT_MODE=TASK
else
BOOT_MODE=SERVICE
fi

for f in /usr/local/boot/*.sh ; do
BOOT_MODE=${BOOT_MODE} . "$f"
done

# We use option "-c" here to suppress following warning message from console output:
# UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations...
if [[ -n "$(ls /etc/supervisor/conf.d/*.conf 2>/dev/null)" ]] ; then
if [[ "SERVICE" == "${BOOT_MODE}" ]] ; then
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf -n
else
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
fi
fi

if [[ ! -z "$@" ]] ; then
if [[ "${1}" =~ ^(ba|)sh$ ]] ; then
# To support Docker commands like following:
# docker run --rm swoole/swoole:4.3.5 bash -c "composer --version"
# docker run --rm swoole/swoole:4.3.5 sh -c "composer --version"
exec "$@"
else
# To support Docker commands invoked in ECS (via command "aws ecs run-task"), kind of like following:
# docker run --rm swoole/swoole:4.3.5 "composer --version"
exec $@
fi
fi
40 changes: 40 additions & 0 deletions rootfilesystem/etc/nginx/nginx.conf
@@ -0,0 +1,40 @@
user root root;
worker_processes 4;
pid /run/nginx.pid;
daemon off;

events {
worker_connections 768;
}

http {
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/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 off;
error_log /proc/self/fd/2;

gzip on;
gzip_proxied any;
gzip_types application/json application/xml text/plain text/xml application/octet-stream;
# Compression level (1-9).
# 5 is a perfect compromise between size and cpu usage, offering about
# 75% reduction for most ascii files (almost identical to level 9).
gzip_comp_level 5;
# Don't compress anything that's already small and unlikely to shrink much
# if at all (the default is 20 bytes, which is bad as that usually leads to
# larger files after gzipping).
gzip_min_length 1280;

# include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
13 changes: 13 additions & 0 deletions rootfilesystem/etc/nginx/sites-enabled/default
@@ -0,0 +1,13 @@
server {
listen 80 default_server;
root /var/www;

location / {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9501;
}
}
8 changes: 8 additions & 0 deletions rootfilesystem/etc/supervisor/available.d/README.md
@@ -0,0 +1,8 @@
Under this folder are _Supervisor_ configuration files that are available but not enabled by default.

To enable a configuration file under this file, you may run commands like following:

```bash
# Assuming there is a Supervisord configuration file "dnsmasq.conf" available.
enable-supervisord-program.sh dnsmasq
```
7 changes: 7 additions & 0 deletions rootfilesystem/etc/supervisor/conf.d/README.md
@@ -0,0 +1,7 @@
Under this folder are _.conf_ configuration files included by the _Supervisor_ configuration file "/etc/supervisor/supervisord.conf".

Besides the _.conf_ files under this folder, _.conf_ files from folder _../service.d_ or _../task.d_ will be copied to
here automatically when starting a Docker container.

If there are two _.conf_ files of the same name there both under this folder and under folder _../service.d_ or _../task.d_,
the latter will be used. You may check file _/usr/local/boot/supervisor.sh_ in the image and see how that happens.
11 changes: 11 additions & 0 deletions rootfilesystem/etc/supervisor/service.d/README.md
@@ -0,0 +1,11 @@
Under this folder are files included by the _Supervisor_ configuration file "/etc/supervisor/supervisord.conf" when
running under service mode, where the Docker container is to start some long-running webservices like Nginx. e.g.,

```bash
docker run --rm --name=app -p 80:80 swoole/swoole:4.3.5
```

In this case, the list of programs defined under this folder (along with those already under folder _../conf.d/_) will
be started by _Supervisord_.

For the list of programs defined under this folder, file extension must be "_.conf_".
12 changes: 12 additions & 0 deletions rootfilesystem/etc/supervisor/service.d/nginx.conf
@@ -0,0 +1,12 @@
[supervisord]
user = root

[program:nginx]
command = /usr/sbin/nginx
user = root
autostart = true
autorestart = true
stdout_logfile=/proc/self/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/proc/self/fd/1
stderr_logfile_maxbytes=0
11 changes: 11 additions & 0 deletions rootfilesystem/etc/supervisor/service.d/swoole.conf
@@ -0,0 +1,11 @@
[supervisord]
user = root

[program:swoole]
command = /var/www/server.php
user = root
autostart = true
stdout_logfile=/proc/self/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/proc/self/fd/1
stderr_logfile_maxbytes=0
11 changes: 11 additions & 0 deletions rootfilesystem/etc/supervisor/task.d/README.md
@@ -0,0 +1,11 @@
Under this folder are files included by the _Supervisor_ configuration file "/etc/supervisor/supervisord.conf" when
running under task mode, where a specified command is executed in a Docker container launched. e.g.,

```bash
docker run --rm -t swoole/swoole:4.3.5 bash -c "php -v"
```

In this case, the list of programs defined under this folder (along with those already under folder _../conf.d/_) will
be started by _Supervisord_ first before executing command `php -v`.

For the list of programs defined under this folder, file extension must be "_.conf_".
14 changes: 14 additions & 0 deletions rootfilesystem/usr/local/bin/disable-supervisord-program.sh
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
#
# To disable a program in Supervisord.
# Once a program is disabled, you can't enable it with script enable-supervisord-program.sh directly.
#

set -e

for conf_dir in "available.d" "conf.d" "service.d" "task.d" ; do
conf_file="/etc/supervisor/${conf_dir}/${1}.conf"
if [[ -f "${conf_file}" ]] ; then
mv "${conf_file}" "${conf_file}.disabled"
fi
done
13 changes: 13 additions & 0 deletions rootfilesystem/usr/local/bin/enable-supervisord-program.sh
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
# To enable an available program in Supervisord. List of available program can be found under folder
# images/php-fpm/rootfilesystem/etc/supervisor/available.d/.
#

set -e

conf_file="/etc/supervisor/available.d/${1}.conf"

if [[ -f "${conf_file}" ]] ; then
cp "${conf_file}" /etc/supervisor/conf.d/.
fi
29 changes: 29 additions & 0 deletions rootfilesystem/usr/local/bin/update_token.sh
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
#
# Update given token with an environment variable under given folders.
#
# Limitations:
# 1. The environment variable can not contain character "#" in it.
#
# Usage:
# ./bin/update_token.sh ENVIRONMENT_VARIABLE_NAME [FOLDER]...
# e.g.,
# ./bin/update_token.sh PHP_VERSION /usr/local/etc/nginx /usr/local/etc/php
#

set -e

if [[ -z ${!1} ]] ; then
echo "Error: environment variable '{$1}' is empty."
exit 1
fi

for path in "${@:2}" ; do
if [[ ! -d "${path}" ]] ; then
echo "Error: Path '${path}' does not point to a folder."
exit 1
fi
done
for path in "${@:2}" ; do
find "${path}" -type f -exec sed -i "s#%%${1}%%#${!1}#g" {} +
done

0 comments on commit b2a6868

Please sign in to comment.