Change nginx.conf file inside docker - docker

I'm trying to create a reverse proxy using nginx running in docker. I have set up the nginx.conf file and it's running fine locally. However, I don't know how to set up the nginx docker with this changed nginx.conf file. Is there any way to do this?
Update: I have been able to change the nginx.conf file inside Docker. However, going to localhost:80/go returns a 502 Bad Gateway. I have a go app running on port 8081 using go run main.go and a python app running on port 8080 using flask run. I'm on a Manjaro VM.
This is the server part of the nginx.conf file
server {
listen 80;
location / {
return 200 'hey there, welcome to our amazing app :)';
}
location /cbl {
proxy_pass http://127.0.0.1:8080;
}
location /go {
proxy_pass http://127.0.0.1:8081;
}
}
And this is the Dockerfile
FROM nginx
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginxorig.conf
#RUN pwd
#RUN cp /home/shark/hwr-nginx/nginx.conf /etc/nginx/conf.d/default.conf
VOLUME /usr/share/nginx/html
VOLUME /etc/nginx
How do I fix this?
[shark#shark-virtualbox hwr-nginx]$ sudo docker build . -t nginx
Sending build context to Docker daemon 7.168kB
Step 1/3 : FROM nginx
---> 7084cd82dcbb
Step 2/3 : RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig
---> Running in 80e011c5b125
mv: cannot stat '/etc/nginx/nginx.conf': No such file or directory
The command '/bin/sh -c mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig' returned a non-zero code: 1
[shark#shark-virtualbox hwr-nginx]$ cat Dockerfile
FROM nginx
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig
RUN cp hwr-nginx/nginx.conf /etc/nginx/nginx.conf
```` RUN mv and RUN cp are also not working for me :(.

You need to copy your nginx.conf to your container:
For example: COPY conf /etc/nginx
Docs here: https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-docker/#managing-content-and-configuration-files

In your Dockerfile, add an instruction to copy your project's reverse proxy configured ngxin.conf into the docker image you intend to build, in the location where nginx will look for its configurations. For example, assuming your base image is Debian, you could do the following in your Dockerfile:
# Move the default conf out of the way
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig
# Copy in your project's new nginx conf
RUN cp my_project/nginx.conf /etc/nginx/nginx.conf

Related

Trying to setup a docker autodeploy to my server. But nginx image is not showing the app

I create the following docker image
# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM tiangolo/node-frontend:10 as build-stage
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY ./ /app/
RUN CI=true npm test
RUN npm run build
# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
COPY --from=build-stage /app/build/ /usr/share/nginx/html
# Copy the default nginx.conf provided by tiangolo/node-frontend
COPY ./nginx.conf /etc/nginx/conf.d/v2.myapp.io
Where the nginx.conf is
server {
listen 80;
server_name serverip v2.myapp.io www.v2.myapp.io;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;
}
And then i execute the following sh
docker build -t mycontainer .
docker push mycontainer:latest
ssh root#ip 'docker pull mycontainer:latest'
ssh root#ip 'docker stop mycontainer'
ssh root#ip 'docker rm mycontainer'
ssh root#ip 'sudo docker run -p 8080:80 -it -d --name mycontainer mycontainer'
Well it works fine, but the address v2.myapp.io points to a blank nginx page, so i guess i have some sort of missconfiguration. But im not sure what else can be happening
Can someone help with this issue?
I remember that when I used to do all this manually, I had a sites-enabled, and a sites-available folder, i have tried creating them and puting the nginx.conf file in but i had no luck
What is failing there?

nginx container fail to start

here is my Dockerfile:
FROM nginx
COPY /root/fe/foodsafe /app
COPY nginx.conf /etc/nginx/nginx.conf
I found in the nginx image doc:
If you add a custom CMD in the Dockerfile, be sure to include -g daemon off; in the CMD in order for
nginx to stay in the foreground, so that Docker can track the process properly (otherwise your
container will stop immediately after starting)!
but I didn't write any CMD in the Dockerfile, what's wrong?

How to define startup file in phusion/passenger-nodejs docker container

I'm attempting to get a next.js app running in a docker container based on phusion/passenger-docker.
I have what I think is a complete setup based on passenger-docker documentation but I'm getting a 404 page from nginx.
A docker log dump shows that passenger or nginx, is looking for index.html
[error] 48#48: *1 "/home/app/nhe_app/index.html" is not found
My startup file is /home/app/nhe_app/server.js
Dockerfile final stage:
# Build production container from builder stage
FROM phusion/passenger-nodejs:1.0.8
# Set correct environment variables.
ENV HOME /root
ENV NODE_ENV=production
# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]
# Enable Nginx and Passenger
RUN rm -f /etc/service/nginx/down
WORKDIR /home/app/nhe_app
RUN rm /etc/nginx/sites-enabled/default
COPY --chown=app:app ./nhe_app.conf /etc/nginx/sites-enabled/nhe_app.conf
COPY --chown=app:app ./secret_key.conf /etc/nginx/main.d/secret_key.conf
COPY --chown=app:app ./gzip_max.conf /etc/nginx/conf.d/gzip_max.conf
COPY --chown=app:app --from=builder /app/server.js /app/.env /home/app/nhe_app/
COPY --chown=app:app --from=builder app/src /home/app/nhe_app/src
COPY --chown=app:app --from=builder app/node_modules /home/app/nhe_app/node_modules
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
nginx configuration - nhe_app.conf:
server {
listen 80;
server_name glen-mac.local;
root /home/app/nhe_app/server.js;
passenger_enabled on;
passenger_user app;
passenger_startup_file server.js;
}
I expect that passenger will start nginx and run my app.
When I build and start the docker container it seems to expect index.html.
I'm building the docker container with
docker image build -t nhe_app .
And running it with
docker container run --name nhe_app -p 80:3000 nhe_app
Browsing to http://glen-mac.local/ shows nginx's formatted 404 page.
How can I configure passenger-docker to look for and execute my server.js rather than index.html?
There are several subtle problems in the OP question.
Most notably, Passenger seems to require that the app root path, defined by root in the nginx configuration above, has a top level folder named public. This folder must not contain an index.html file and probably should be empty. This is shown in examples, but not spelled out as a hard requirement in the docs.
Second major error is that Passenger bypasses the port specified in the app's server.js (3000 in this case) and replaces it with the port specified in the nginx configuration. So the docker run command changed from:
docker container run --name nhe_app -p 80:3000 nhe_app
to
docker container run --name nhe_app -p 80:80 nhe_app.
Otherwise, the best advice I can give is:
Learn the Passenger basics through a local installation (without Docker). Get the Passenger demo app working.
Get your app working in that local Passenger installation.
Apply what you have learned to implementing your app in passenger-docker.
server {
listen 7063;
server_name localhost;
root /home/app/nhe_app;
passenger_enabled on;
passenger_min_instances 1;
passenger_max_request_queue_size 100; # default: 100
passenger_app_env staging; # NODE_ENV; default: staging
passenger_app_root /home/app/nhe_app;
passenger_app_type node;
passenger_startup_file server.js;
}

Where to place nginx.conf file in docker container

I have a flask app running in AWS ec2 inside a docker container and I am having latency problems. I figured this is because inside the container, localhost routes to both ipv4's 127.0.0.1 and ipv6's ::1 (based on some other SO posts). I found this blog post which addresses the issue exactly. But in the blog he uses nginx, which I've never used before. He has an nginx config file:
location / { try_files $uri #project; }
location #project {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
and I'm not sure how I can place this in the container so that it reads properly. I read somewhere that it goes in /etc/nginx, but then how do I edit the dockerfile to include this conf there?
This is the Dockerfile he suggests. Is the line ADD nginx /etc/nginx where the file is getting copied in? and he just didn't give the file an extension?
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y build-essential nginx python3.4 python3.4-dev
RUN easy_install3 pip
WORKDIR /project
ADD requirements.txt /project/requirements.txt
RUN pip install -r requirements.txt
ADD . /project
ADD nginx /etc/nginx
CMD uwsgi -s /tmp/uwsgi.sock -w project:app --chown-socket=www-data:www-data --enable-threads & \
nginx -g 'daemon off;'
Yes, he is adding the file nginx to the container file system location /etc/nginx/
Personally I'd name the file nginx.conf as that is the default name according to the nginx documentation.
By default, the configuration file is named nginx.conf and placed in
the directory /usr/local/nginx/conf, /etc/nginx, or
/usr/local/etc/nginx.
Also, I'd recommend using COPY in stead of ADD according to the Best practices for writing Dockerfiles
For other items (files, directories) that do not require ADD’s tar
auto-extraction capability, you should always use COPY.
To make this work you'll need to install nginx on the container you're running flask on.

How do I add the Lua module for nginx on Alpine linux?

I'd like to have a lean Docker image for nginx with the Lua module enabled. How can I create this based on Alpine linux?
Here is a Dockerfile:
FROM alpine:3.6
RUN apk add --no-cache nginx-mod-http-lua
# Delete default config
RUN rm -r /etc/nginx/conf.d && rm /etc/nginx/nginx.conf
# Create folder for PID file
RUN mkdir -p /run/nginx
# Add our nginx conf
COPY ./nginx.conf /etc/nginx/nginx.conf
CMD ["nginx"]
Installing the nginx-mod-http-lua package will also install nginx and luajit, among others.
The nginx.conf should contain at least this:
load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
pcre_jit on;
events {
worker_connections 1024;
}
daemon off;
Dockerfile:
FROM nginx:1.15-alpine
RUN mkdir -p /run/nginx
RUN apk add --no-cache nginx-mod-http-lua
COPY nginx_conf/ /etc/nginx/ # Your nginx conf
COPY lua/ /etc/lua/ # Your lua files
First line of nginx conf:
load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
pcre_jit on;
We use Openresty, a platform that integrates nginx and Lua.
In the default nginx file, you could call Lua like so:
server {
listen 80;
listen 443 ssl; # 'ssl' parameter tells NGINX to decrypt the traffic
# 1
location ~ /api/(.*) {
resolver xxx.x.x.xx;
rewrite_by_lua_block {
ngx.req.set_header("x-header", "12345678901234567")
}
}
The alpine image here: https://github.com/openresty/docker-openresty/tree/master/
There is also an alpine-fat that had make, git and other libraries that can help you build within your Docker image.
See: "Adding third-party modules to nginx official image"
At: https://github.com/nginxinc/docker-nginx/tree/master/modules
"It's possible to extend a mainline image with third-party modules either from your own instuctions following a simple filesystem layout/syntax using build_module.sh helper script, or failing back to package sources from pkg-oss."
$ docker build --build-arg ENABLED_MODULES="ndk lua" -t my-nginx-with-lua .
You look on the Docker Hub
https://hub.docker.com/
and you find an Nginx image, based on Alpine Linux, with Lua support
Some examples
https://hub.docker.com/r/ilagnev/alpine-nginx-lua/
or
https://hub.docker.com/r/firesh/nginx-lua/
Have a look at the Dockerfile for more details

Resources