I am using docker-compose for my python web app. I am using nginx as one of service. Currently I am running on localhost and it is working fine. Now, instead of localhost I am trying to use domain name as server_name www.mywebapp.com
and I also need to edit /etc/hosts
file to add 127.0.0.1 www.mywebapp.com
, Although file already has default entry as 127.0.0.1 localhost
.
- File: default.conf.template
server {
listen 8080;
server_name ${DOMAIN_NAME};
client_max_body_size 8G;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://web:5000;
}
}
- File: nginx Dockerfile
ARG NGINX_IMG
FROM $NGINX_IMG
ARG DOMAIN_NAME
ENV DOMAIN_NAME=$DOMAIN_NAME
USER root
# RUN sed -i 's/127.0.0.1 localhost/127.0.0.1 localhost $DOMAIN_NAME/' /etc/hosts
RUN echo "127.0.0.1 $DOMAIN_NAME" >> /etc/hosts
COPY deploy/docker/proxy/default.conf.template /etc/nginx/conf.d/default.conf.template
USER nginx
CMD ["/bin/sh" , "-c" , "envsubst '$DOMAIN_NAME' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]
Output:
- On running,
sed
it gives error as busy operation - On running,
echo
it executes successfully, but on checking from nginx container terminalcat /etc/hosts
No modification for domain name - On checking
default.conf
theserver_name
gets updated byenvbust
asserver_name www.mywebapp.com
As getting error to modify /etc/hosts
file unable to access through domain name.
How to edit /etc/hosts
file on nginx docker image?