
Halo, kalau kamu merasa tulisan saya ngebantu kamu, kamu bisa ucapkan terima kasih lewat saweria.
Hello, if you find this article helpful, you can express your gratitude through saweria.
Awalan
Bagi yang sering menggunakan docker-compose, sudah familiar dengan opsi environment, kita coba lihat dari Shlink
name: service-contoh
services:
service-contoh:
.
.
.
#kita akan belajar custom bagian ini
environment:
- DB_DRIVER=${DB_DRIVER}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_HOST=${DB_HOST}
- DB_NAME=${DB_NAME}
#dengan yg lebih simpel tentunya
.
.
.
Menggunakan sed
Siapkan beberapa file
.env,docker-compose.yml,Dockerfile,entrypoint.sh,index.html&nginx.confFile
index.html
<html>
<head>
<title>Test</title>
</head>
<body>
Selamat datang di {URL}
</body>
</html>
nginx.conf
worker_processes 1;
error_log stderr warn;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# Define custom log format to include reponse times
# log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for" '
# '$request_time $upstream_response_time $pipe $upstream_cache_status';
access_log off;
error_log off;
# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on;
# request timed out -- default 60
client_body_timeout 10;
# if client stop responding, free up memory -- default 60
send_timeout 2;
# server will close connection after this time -- default 75
keepalive_timeout 30;
# number of requests client can make over keep-alive -- for testing environment
keepalive_requests 100000;
# Write temporary files to /tmp so they can be created as a non-privileged user
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
# Default server definition
server {
#please use uncommon port
listen [::]:80 default_server;
listen 80 default_server;
server_name _;
server_tokens off;
sendfile off;
root /var/www/html;
location / {
index index.html;
}
}
gzip on;
gzip_proxied any;
gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss font/truetype font/opentype;
gzip_vary on;
gzip_disable "msie6";
# Include other server configs
include /etc/nginx/conf.d/*.conf;
}
entrypoint.sh
#!/bin/bash
find /var/www/html -type f -name "*.html" -exec sed -i -e "s|{URL}|URL|g" {} \;
nginx -g "daemon off;"
Dockerfile
FROM nginx:alpine
# Install tzdata and remove default.conf
RUN apk --no-cache add tzdata && \
rm /etc/nginx/conf.d/default.conf
WORKDIR /var/www/html
# Set timezone to Asia/Jakarta
RUN cp /usr/share/zoneinfo/Asia/Jakarta /etc/localtime && \
echo "Asia/Jakarta" > /etc/timezone
# Copy custom nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
# Create document root directory
RUN mkdir -p /var/www/html
# Copy application files
COPY . /var/www/html/
# Set ownership of document root
RUN chown -R nginx:nginx /var/www/html
EXPOSE 8080
COPY entrypoint.sh /var/www/html/entrypoint.sh
RUN chmod +x /var/www/html/entrypoint.sh
ENTRYPOINT [ "sh", "/var/www/html/entrypoint.sh" ]
CMD []
docker-compose.yml
name: env-custom-test
services:
env-custom-test:
image: docker.io/library/env-custom-test-image:latest
container_name: env-custom-test-ctr
restart: always
ports:
- 88:80
env_file:
- .env
environment:
- URL=${URL}
networks:
- env-custom-test-net
networks:
env-custom-test-net:
- File
.env
URL=ipang.my.id
Build image dengan
docker build -t env-custom-test-image:latest .Jalankan
docker compose up -dCoba ganti value di .env untuk bereksperimen
Menggunakan envsubst
Siapkan beberapa file
.env,docker-compose.yml,Dockerfile,entrypoint.sh,index.html&nginx.confFile
index.html
<html>
<head>
<title>Test</title>
</head>
<body>
Selamat datang di $URL
</body>
</html>
nginx.conf
worker_processes 1;
error_log stderr warn;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# Define custom log format to include reponse times
# log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for" '
# '$request_time $upstream_response_time $pipe $upstream_cache_status';
access_log off;
error_log off;
# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on;
# request timed out -- default 60
client_body_timeout 10;
# if client stop responding, free up memory -- default 60
send_timeout 2;
# server will close connection after this time -- default 75
keepalive_timeout 30;
# number of requests client can make over keep-alive -- for testing environment
keepalive_requests 100000;
# Write temporary files to /tmp so they can be created as a non-privileged user
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
# Default server definition
server {
#please use uncommon port
listen [::]:80 default_server;
listen 80 default_server;
server_name _;
server_tokens off;
sendfile off;
root /var/www/html;
location / {
index index.html;
}
}
gzip on;
gzip_proxied any;
gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss font/truetype font/opentype;
gzip_vary on;
gzip_disable "msie6";
# Include other server configs
include /etc/nginx/conf.d/*.conf;
}
entrypoint.sh
#!/bin/bash
#envsubst cannot override original file
envsubst < "index.html" > "index.html.temp"
mv index.html.temp index.html
nginx -g "daemon off;
Dockerfile
FROM nginx:alpine
# Install tzdata and remove default.conf
RUN apk --no-cache add tzdata && \
rm /etc/nginx/conf.d/default.conf
WORKDIR /var/www/html
# Set timezone to Asia/Jakarta
RUN cp /usr/share/zoneinfo/Asia/Jakarta /etc/localtime && \
echo "Asia/Jakarta" > /etc/timezone
# Copy custom nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
# Create document root directory
RUN mkdir -p /var/www/html
# Copy application files
COPY . /var/www/html/
# Set ownership of document root
RUN chown -R nginx:nginx /var/www/html
EXPOSE 8080
COPY entrypoint.sh /var/www/html/entrypoint.sh
RUN chmod +x /var/www/html/entrypoint.sh
ENTRYPOINT [ "sh", "/var/www/html/entrypoint.sh" ]
CMD []
docker-compose.yml
name: envsubst-custom-test
services:
envsubst-custom-test:
image: docker.io/library/envsubst-custom-test-image:latest
container_name: envsubst-custom-test-ctr
restart: always
ports:
- 888:80
env_file:
- .env
environment:
- URL=${URL}
networks:
- envsubst-custom-test-net
networks:
envsubst-custom-test-net:
- File
.env
URL=ipang.my.id
Build image dengan
docker build -t envsubst-custom-test-image:latest . --no-cacheJalankan
docker compose up -dCoba ganti value di .env untuk bereksperimen
