How to modify nginx access and error log locations with dockerfile? - docker

I am running nginx in a docker container. I have to RELOCATE the nginx log files from /var/log/nginx/error.log and /var/log/nginx/access.log to different locations. For this I need to modify the /etc/nginx/nginx.conf file in my docker image. What can I mention in my dockerfile to make this modification?
I do not want to re-write the nginx.conf file with commands like echo.

You don't need to rewrite using echos, it's easier than that.
Change the file nginx.conf to your needs.
Put it next to the Dockerfile
Add the following instruction to your Dockerfile: COPY nginx.conf /etc/nginx/nginx.conf

Related

Container based Nginx configuration

Seeking help from developers familiar with Wodby container management. The main objective is changing the MIME Types that are gzipped. I'm confused with the documentation for customizing my Nginx container. The documentation:
https://wodby.com/docs/1.0/stacks/drupal/containers/
suggests I copy "/etc/nginx/conf.d/vhost.conf", modify it, deploy it the repo and use an environment variable to include it. My problem is, even if I could find this file, which is not mounted on the server when created via Wodby, it does not appear that I'm actually able to change the MIME types or the default_type as they are already defined in the nginx.conf file.
I have also attempted to modify the Wodby stack to mount the /etc/ directory so that I could manually edit the nginx.conf file if I had to, but that only freezes the deployment.
Any help would be tremendously appreciated.
Two options
clone a repo https://github.com/wodby/nginx/, change the template file /templates/nginx.conf.tmpl as much as you need and build your own image. See Makefile (/Makefile) for the commands they use to build the image themselves. Use this image as the image for your nginx container from docker-compose.
Run a container with the default settings, shell into the container with docker-compose exec nginx sh and copy the nginx file from the container (use cat /etc/nginx/nginx.conf and copy it somewhere). Create a new file locally and mount it via the docker-compose.yml for the nginx container like
volumes:
- ./nginx-custom.conf:/etc/nginx/nginx.conf

I can't see my html when I built container with docker

I have my container running like this
in the file directory C:\Users\hailey\Desktop\GitTest Where my project file are.
# getting bse image nginx
FROM nginx
MAINTAINER hailey
COPY . /usr/share/nginx/html
This is my docker file and I want to run my html file, which located in C:\Users\hailey\Desktop\GitTest
When I accessed to http://127.0.0.1:8080/
I see only this page, which is not helloWorld.html
you can copy and replace testhelloWorld.html with index.html
COPY testhelloWorld.html /usr/share/nginx/html/index.html

Dockerfile COPY not overwriting Nginx configuration (or Nginx overwrites on container start) - why?

I have a Dockerfile I use for containerizing Python Flask-based microservices that's based on this base Docker image: https://github.com/tiangolo/uwsgi-nginx-flask-docker
In my Dockerfile, I add a custom nginx.conf and overwrite Nginx's:
FROM tiangolo/uwsgi-nginx-flask:python3.6
ADD nginx.conf nginx.conf
COPY ./app /app
COPY ./data /app/data
COPY nginx.conf /etc/nginx/conf.d/
My custom nginx.conf includes only one change - a single server_name that I prepare with a custom domain name:
server {
listen 80;
location / {
try_files $uri #app;
}
location #app {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
location /static {
alias /app/static;
}
server_name my-fully-qualified-domain-name.com;
}
The reason for this is that I want to run Let's Encrypt's certbot utility to force Nginx to be SSL-only within the container.
The problem: Docker refuses to overwrite nginx.conf. It pretty much refuses to put anything I try into /etc/nginx/conf.d/.
Or maybe Docker does overwrite it, but something within Nginx on start (at container start) overwrites my changes. I haven't figured it out, but I'd really like to clobber that nginx.conf with my own changes.
Even attaching to the container and manually overwriting Nginx's configuration - then committing those changes to the container using docker commit fails. I suspect there's just something I'm not understanding about how Docker's COPY command works or how docker commit works - any thoughts/suggestions?
Note #1 - I have not been able to get a custom server_name field working with certbot using separate Nginx configuration files (per these instructions). The only way I've been able to get certbot to pick up the right server_name has been by clobbering & overwriting the default nginx.conf, hence going this approach. Perhaps I'm simply using custom Nginx configuration files incorrectly - any suggestions on that note would be greatly appreciated - but I had gone down that road before and was not successful.
Note #2 - I am able to run certbot on a running container (after attaching & overwriting Nginx's configuration), and that works great - SSL on my container, awesome - until the container stops and restarts. Then it's all wiped away and I need to overwrite Nginx's configuration & run certbot again - not ideal at all.
You shouldn't overwrite the default nginx.conf file (see https://github.com/tiangolo/uwsgi-nginx-flask-docker#customizing-nginx-configurations).
However you can still add your own configuration in a separate file within /etc/nginx/conf.d/, which should be enough for most use cases.
Edit:
If that doesn't work you can modify entrypoint.sh to better suit your needs since nginx.conf is set there. This issue contains a bit more info: https://github.com/tiangolo/uwsgi-nginx-flask-docker/issues/39

Docker + OpenResty - flexible configuration. How to?

I have the openresty application that deploys with docker.
My docker file:
FROM openresty/openresty:alpine-fat
ARG APPLICATION_PATH="/srv/www/my-app"
COPY nginx.conf /usr/local/openresty/nginx/conf
RUN mkdir ${APPLICATION_PATH}
I'm running docker with this command:
docker run -v $(CURRENT_DIRECTORY):/srv/www/my-app -v $(CURRENT_DIRECTORY)/conf:/etc/nginx/conf.d --name="$(APP_NAME)" -p $(PORT):80 -d $(CONTAINER_NAME)
This command stored in the Makefile and variables values like that:
CONTAINER_NAME = my-app
APP_NAME = my-app
override PORT = 8080
ifeq ($(OS),Windows_NT)
CURRENT_DIRECTORY=%cd%
else
CURRENT_DIRECTORY=${PWD}
endif
So also I have my-app.conf stored in the conf directory. This is nginx-configuration file, where I have this line:
content_by_lua_file '/srv/www/my-app/main.lua';
And further I have nginx.conf, where I have this line:
lua_package_path ";;/srv/www/my-app/?.lua;/srv/www/my-app/application/?.lua";
I don't want duplicate /srv/www/my-app in the 3 files. How I can avoid this?
IMO, your approach is not consistent.
You copy nginx.conf file, but mount a volume for my-app.conf (is it included into nginx.conf?)
Curiously that $(CURRENT_DIRECTORY)/conf is mounted twice - as /srv/www/my-app/conf and as /etc/nginx/conf.d.
Below is my approach for OpenResty containers:
Write simple nginx.conf without includes. Copy it into container as you do.
The only reason to mount a folder with nginx.conf is ability to reload nginx configuration after changes. Keep in mind - if you would mount a single file reload may not work. https://github.com/docker/for-win/issues/328
Copy all Lua files mentioned in *_by_lua_file directives into /usr/local/openresty/nginx
Copy all Lua files required from files above (if any) into /usr/local/openresty/lualib
Don't use absolute file paths in *_by_lua_file directives, you may specify relative path to /usr/local/openresty/nginx
Don't use lua_package_path directive, defaults should works.
Here is the simple working example https://gist.github.com/altexy/8f8e08fd13cda25ca47418ab4061ce1b

How to deal with custom nginx.conf files in Amazon ECS?

I basically have two docker images: nginx image and a php image, that I want to upload to ECS. Both are run by a docker-compose.
The nginx has a myapp.conf file that I want to copy from somewhere into the container's /etc/nginx/conf.d folder.
Whats the best way to deal with this?
Prepare your own nginx image and use the COPY command.
FROM nginx
COPY myapp.conf /etc/nginx/conf.d
Build it:
docker build -t mynginximg .
and use it in your compose files.

Resources