Skip to content

Commit 3b8f84a

Browse files
authored
Merge pull request #20 from SimplyEdit/feature/cleanup
Cleanup dockerfile
2 parents 260266f + 5787377 commit 3b8f84a

File tree

16 files changed

+1230
-25
lines changed

16 files changed

+1230
-25
lines changed

Dockerfile

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
FROM php:7.4-apache as builder
1+
FROM node:22.12-alpine AS builder
22

3-
RUN apt-get update && apt-get install -y git ssl-cert \
4-
&& git clone 'https://github.com/SimplyEdit/simplycode.git' /app/simplycode
3+
RUN apk add --no-cache bash git \
4+
&& mkdir /app \
5+
&& git -C /app/ clone 'https://github.com/SimplyEdit/simplycode-electron.git' \
6+
&& git -C /app/ clone 'https://github.com/SimplyEdit/simplyedit-backend.git' \
7+
&& npm --prefix /app/simplycode-electron install
58

69
FROM php:7.4-apache
710

8-
COPY --from=builder /app/simplycode/lib /var/www/lib
9-
COPY --from=builder /app/simplycode/www/api/.htaccess /var/www/html/api/.htaccess
10-
COPY --from=builder /app/simplycode/www/api/data/generated.html /var/www/html/simplycode/index.html
11-
COPY --from=builder /app/simplycode/www/api/index.php /var/www/html/api/index.php
12-
COPY --from=builder /app/simplycode/www/css /var/www/html/simplycode/css
13-
COPY --from=builder /app/simplycode/www/js /var/www/html/simplycode/js
14-
COPY --from=builder /app/simplycode/www/simply /var/www/html/simplycode/simply
11+
COPY lib/ /var/www/lib/
12+
COPY html/ /var/www/html/
1513

16-
COPY --from=builder /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/certs/ssl-cert-snakeoil.pem
17-
COPY --from=builder /etc/ssl/private/ssl-cert-snakeoil.key /etc/ssl/private/ssl-cert-snakeoil.key
14+
COPY --from=builder /app/simplycode-electron/simplycode/ /var/www/html/simplycode/
15+
COPY --from=builder /app/simplyedit-backend/www/ /var/www/www/
1816

19-
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
20-
COPY entrypoint.sh /entrypoint.sh
21-
COPY 403.php /var/www/html/403.php
17+
RUN echo "ServerName simplycode" >> /etc/apache2/apache2.conf \
18+
&& a2enmod --quiet rewrite ssl headers \
19+
&& chmod +x /var/www/lib/entrypoint.sh
2220

23-
RUN a2enmod --quiet rewrite ssl headers \
24-
&& chmod +x /entrypoint.sh \
25-
&& ln -s /var/www/html/simplycode/js/ /var/www/html/js \
26-
&& ln -s /var/www/www/api/data/generated.html /var/www/html/index.html \
27-
&& mkdir /var/www/html/data && echo '{}' > /var/www/html/data/data.json
28-
29-
ENTRYPOINT ["/entrypoint.sh"]
21+
ENTRYPOINT ["/var/www/lib/entrypoint.sh"]

bin/run-simplycode-docker.sh

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit # Exit script when a command exits with non-zero status.
4+
set -o errtrace # Exit on error inside any functions or sub-shells.
5+
set -o nounset # Exit script on use of an undefined variable.
6+
set -o pipefail # Return exit status of the last command in the pipe that exited with a non-zero exit code
7+
8+
: "${DOCKER:=docker}"
9+
10+
# ==============================================================================
11+
# Exit codes
12+
# ------------------------------------------------------------------------------
13+
: readonly -i "${EXIT_OK:=0}"
14+
: readonly -i "${EXIT_NOT_ENOUGH_PARAMETERS:=65}"
15+
: readonly -i "${EXIT_INVALID_PARAMETER:=66}"
16+
: readonly -i "${EXIT_COULD_NOT_FIND_DIRECTORY:=75}"
17+
: readonly -i "${EXIT_NOT_CORRECT_TYPE:=81}"
18+
19+
# ------------------------------------------------------------------------------
20+
# Foreground colors
21+
# ------------------------------------------------------------------------------
22+
: readonly "${COLOR_BLUE:=$(tput setaf 4)}"
23+
: readonly "${COLOR_GREEN:=$(tput setaf 2)}"
24+
: readonly "${COLOR_RED:=$(tput setaf 1)}"
25+
: readonly "${COLOR_WHITE:=$(tput setaf 7)}"
26+
# ------------------------------------------------------------------------------
27+
: readonly "${RESET_TEXT:=$(tput sgr0)}" # turn off all attributes
28+
# ==============================================================================
29+
30+
31+
# ==============================================================================
32+
## Run the SimplyCode Docker container
33+
# ------------------------------------------------------------------------------
34+
## \nUsage: $0 [-dh][-i <docker-image>] <project-path>
35+
##
36+
## Where:
37+
## <docker-image> is an alternative Docker image to use
38+
## <project-path> is the path to the SimplyCode project to run
39+
##
40+
## Options:
41+
## -d|--dry-run Show the command that would be run without executing it
42+
## -h|--help Print this help dialogue and exit
43+
## -i|--docker-image=<docker-image> Use the specified Docker image
44+
##
45+
## Any additional arguments will be passed to the `docker run` command.
46+
##
47+
## For example:
48+
##
49+
## $0 <project-path> /bin/bash
50+
##
51+
## The Docker executable can be overridden by setting the DOCKER environmental
52+
## variable before calling this script:
53+
##
54+
## DOCKER=/usr/local/docker $0 <project-path>
55+
##
56+
## The SimplyCode Docker container will be run with the given project path mounted
57+
## to the container's /var/www/www/api/data directory. If the project has an assets
58+
## directory, this will be mounted to /var/www/html/assets.
59+
# ==============================================================================
60+
usage() {
61+
local sScript sUsage
62+
63+
sScript="$(basename "$0")"
64+
sUsage="$(grep '^##' < "$0" | cut -c4-)"
65+
66+
echo -e "${sUsage//\$0/${sScript}}"
67+
}
68+
69+
error(){
70+
message "ERROR" "${COLOR_RED}" "${@}" >&2
71+
}
72+
73+
info(){
74+
message "INFO" "${COLOR_BLUE}" "${@}"
75+
}
76+
77+
message(){
78+
local sType="${1?Three parameters required: <type> <color> <message>}"
79+
local sColor="${2?Three parameters required: <type> <color> <message>}"
80+
local sMessage="${3?Three parameters required: <type> <color> <message>}"
81+
82+
echo -e "${COLOR_WHITE}[${sColor}${sType}${COLOR_WHITE}]${RESET_TEXT} ${sMessage}"
83+
84+
# Each additional parameter will be treated as extra information to display with the error
85+
if [[ "$#" -gt 3 ]]; then
86+
shift 3
87+
for sMessage in "$@"; do
88+
echo -e " ${sMessage}"
89+
done
90+
fi
91+
}
92+
93+
run_simplycode_docker() {
94+
dryRun() {
95+
local sResult
96+
97+
DOCKER='echo'
98+
sResult=$(executeCommand "${@}")
99+
100+
iLines=$(echo "${sResult}" | sed -E 's/ --/\n--/g' | wc -l)
101+
sResult="$(echo -n "${sResult}" | sed -E 's/ --/ \\\n --/g' | sed -n "1,${iLines}p")"
102+
103+
echo -en "docker "
104+
echo -n "${sResult}" | head -n -1
105+
echo -n "${sResult}" | tail -n 1 | cut -d ' ' -f 5-6 | sed -E 's/$/ \\/g' | sed 's/^/ /'
106+
echo -n "${sResult}" | tail -n 1 | cut -d ' ' -f 7- | sed -E 's/ / \\\n/g' | sed 's/^/ /'
107+
}
108+
109+
executeCommand() {
110+
local sDockerImage sProjectPath
111+
112+
readonly sDockerImage="${1?Two parameters required: <docker-image> <project-path>}"
113+
shift
114+
readonly sProjectPath="${1?Two parameters required: <docker-image> <project-path>}"
115+
shift
116+
117+
# ======================================================================
118+
# Build the run command
119+
# ----------------------------------------------------------------------
120+
local -a aCommand=("${DOCKER}" 'run')
121+
122+
aCommand+=(
123+
'--env' "USER_GID=$(id -g)"
124+
'--env' "USER_ID=$(id -u)"
125+
'--interactive'
126+
'--network=default'
127+
'--publish' '80:80'
128+
'--publish' '443:443'
129+
'--rm'
130+
'--tty'
131+
'--volume' "${sProjectPath}:/var/www/www/api/data"
132+
)
133+
134+
if [[ -d "${sProjectPath}/assets" ]]; then
135+
aCommand+=(
136+
'--volume' "${sProjectPath}/assets:/var/www/html/assets"
137+
)
138+
fi
139+
140+
aCommand+=(
141+
"${sDockerImage}"
142+
"${@}"
143+
)
144+
145+
# @TODO: Split command creation and execution into separate functions
146+
# so dry-run can be simplified
147+
148+
"${aCommand[@]}"
149+
}
150+
151+
local -a aParameters
152+
local sArgument sDockerImage
153+
local bDryRun=false
154+
155+
aParameters=()
156+
157+
sDockerImage='ghcr.io/simplyedit/simplycode-docker:main'
158+
159+
while (( "$#" )); do
160+
sArgument="${1}"
161+
shift
162+
case "${sArgument}" in
163+
-d | --dry-run)
164+
bDryRun=true
165+
;;
166+
167+
-\? | -h | --help)
168+
usage
169+
exit "${EXIT_OK}"
170+
;;
171+
172+
-i | --docker-image | --docker-image=?*)
173+
# If the parameter contains a `=` the path is part of the param, so we need to split it to get the value
174+
if grep '=' <(echo "${sArgument}");then
175+
sDockerImage="${sArgument#*=}"
176+
else
177+
# Else, the next param is the value, unless no param is provided
178+
if [[ -n "${1:-}" && ! "${1}" =~ ^- ]]; then
179+
sDockerImage="${1}"
180+
shift
181+
else
182+
error "No value provided for ${sDockerImage}" "Call with --help for more information."
183+
exit "${EXIT_NOT_ENOUGH_PARAMETERS}"
184+
fi
185+
fi
186+
;;
187+
188+
--*|-*)
189+
error "Invalid option '${sArgument}'" "Call with --help for more information."
190+
exit "${EXIT_INVALID_PARAMETER}"
191+
;;
192+
193+
*)
194+
aParameters+=("${sArgument}")
195+
;;
196+
esac
197+
done
198+
199+
if [[ "${#aParameters[@]}" -lt 1 ]]; then
200+
error "One parameter required: <project-path>" "Call with --help for more information."
201+
exit "${EXIT_NOT_ENOUGH_PARAMETERS}"
202+
fi
203+
204+
local sProjectPath="${aParameters[0]}"
205+
sProjectPath="$(realpath "${sProjectPath}")"
206+
aParameters=( "${aParameters[@]:1}" )
207+
208+
if [[ ! -e "${sProjectPath}" ]];then
209+
error "Could not find directory: ${sProjectPath}"
210+
exit "${EXIT_COULD_NOT_FIND_DIRECTORY}"
211+
elif [[ ! -d "${sProjectPath}" ]];then
212+
error "Provided path is not a directory: ${sProjectPath}"
213+
exit "${EXIT_NOT_CORRECT_TYPE}"
214+
fi
215+
216+
if [[ "${bDryRun}" == true ]]; then
217+
info "Dry run enabled. The following command would be run:\n"
218+
dryRun "${sDockerImage}" "${sProjectPath}" "${aParameters[@]}"
219+
echo ""
220+
else
221+
executeCommand "${sDockerImage}" "${sProjectPath}" "${aParameters[@]}"
222+
fi
223+
}
224+
225+
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
226+
export -f run_simplycode_docker
227+
else
228+
run_simplycode_docker "${@}"
229+
fi
230+
231+
#EOF

html/.htaccess

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Files settings.json>
2+
<Limit GET>
3+
Order Allow,Deny
4+
Deny from all
5+
</Limit>
6+
</Files>
7+
<Files data.json>
8+
Header set Cache-Control "max-age=0, public, must-revalidate"
9+
</Files>
10+
RewriteEngine on
11+
#<Limit POST PUT DELETE>
12+
# RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
13+
#
14+
# RewriteCond %{REQUEST_METHOD} PUT
15+
# RewriteRule ^(.*)$ simply-edit/store.php [L]
16+
#
17+
# RewriteCond %{REQUEST_METHOD} DELETE
18+
# RewriteRule ^(.*)$ simply-edit/store.php [L]
19+
#
20+
# RewriteCond %{QUERY_STRING} _method=(PUT|DELETE)
21+
# RewriteRule ^(.*)$ simply-edit/store.php [L]
22+
#</Limit>
23+
<Limit GET POST>
24+
RewriteRule ^logout$ simply-edit/logout.php [L]
25+
#RewriteRule ^login$ simply-edit/login.php [L]
26+
</Limit>
27+
<Limit GET>
28+
RewriteCond %{HTTP_USER_AGENT} Lynx|w3m|googlebot|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|Validator [NC,OR]
29+
RewriteCond %{QUERY_STRING} _escaped_fragment_
30+
31+
# Only proxy the request to Prerender if it's a request for HTML
32+
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) simply-edit/prerender.php [L]
33+
</Limit>
34+
35+
Options +Indexes
36+
37+
RewriteCond %{REQUEST_FILENAME} !-f
38+
RewriteCond %{REQUEST_FILENAME} !-d
39+
RewriteRule .* simply-edit/router.php [L]

html/api/.htaccess

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RewriteEngine on
2+
<Limit POST PUT DELETE>
3+
RewriteCond %{QUERY_STRING} _method=(GET|PUT|DELETE)
4+
RewriteRule ^(.*)$ index.php [L]
5+
</Limit>
6+
7+
RewriteCond %{REQUEST_FILENAME} !-f
8+
RewriteCond %{REQUEST_FILENAME} !-d
9+
RewriteRule .* index.php [L]

0 commit comments

Comments
 (0)