Static webpage on Nginx Docker Container Missing CSS - css

I am trying to host the Swagger UI on a docker container using Nginx.
When I access my webpage via hostAddress.com it returns the webpage as plain text and inspecting it tells me that it can't find any of the javascript or css files despite them seeming to be present in the container as I have ssh into the container to check.
My dockerfile
FROM nginx
COPY src /usr/share/nginx/html
COPY config/nginx.conf /etc/nginx
EXPOSE 80
nginx.config
events {
worker_connections 4096; ## Default: 1024
}
http {
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
location /swagger {
try_files $uri /index.html;
}
#Static File Caching. All static files with the following extension will be cached for 1 day
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1d;
}
}
}

Here what you can do achieve that.
Like it was mentioned the comment, you can download the artifacts from
https://github.com/ianneub/docker-swagger-ui/blob/master
Create a directory say ngix
Copy Dockerfile, run.sh into that directory
Edit the Dockerfile to make the customization that you need to change the location as shown below:
FROM nginx:1.9
ENV SWAGGER_UI_VERSION 2.1.2-M2
ENV URL **None**
RUN apt-get update \
&& apt-get install -y curl \
&& curl -L https://github.com/swagger-api/swagger-ui/archive/v${SWAGGER_UI_VERSION}.tar.gz | tar -zxv -C /tmp \
&& mkdir /usr/share/nginx/html/swagger \
&& cp -R /tmp/swagger-ui-${SWAGGER_UI_VERSION}/dist/* /usr/share/nginx/html/swagger \
&& rm -rf /tmp/*
COPY run.sh /run.sh
CMD ["/run.sh"]
If you compare the above changes with original Dockerfile, there are changes made in the lines 9, 10 to include additional path i.e., swagger. Of course, you may change as needed.
Next, run the following docker commands to build and run
Build Image:
docker build -t myswagger .
Run it
docker run -it --rm -p 3000:80 --name testmyswagger -e "URL=http://petstore.swagger.io/v2/swagger.json" myswagger
Now you should be able to access your swagger using http://localhost:3000/swagger/index.html

Related

Docker Image with Nginx and running with docker compose and in Jenkins Pipeline

I already have a docker file for customized image for nginx and this works fine.
FROM library/nginx:1.13.2
LABEL maintainer="san#test.com"
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
# Make PageSpeed cache writable
&& mkdir -p /var/cache/ngx_pagespeed && \
chmod -R o+wr /var/cache/ngx_pagespeed
ADD server.crt /etc/nginx/ssl/
ADD server.key /etc/nginx/ssl/
ADD conf.d/ /etc/nginx/conf.d/
ADD proxy.d/ /etc/nginx/proxy.d/
CMD ["nginx", "-g", "daemon off;"]
I am trying to also have aws cli installed so I can copy some s3 files and dynamically change nginx configuration which i will do with CMD[] once awsCli is available within the container.
I tried and read many a links from google, but the documentation or reads are not helping especially how to have credentials passed.
I am creating the image in two ways. First is via jenkins pipeline (snippet below
stages {
stage('Build Docker image') {
steps {
script {
docker.withRegistry("http://xyz-1.amazonaws.com", "ecr:eu-central-1:aws-credentials") {
def customImage = docker.build("web-proxy:${CY_RELEASE_VERSION}", ".")
customImage.push()
}
}
}
}
And other way is in local I manually build the image like following
docker build -t web-proxy-dev_san_1:1.11 .
What I am not sure is how I can have aws-cli in DockerFile and have the image take credentials automatically both locally and in jenkins. I think for jenkins it may work if I manage to have aws-cli installed as I am using aws-credentials specified in pipeline but I havent reached that stage yet.
You can use AWS plugin to interact AWS in your pipeline, check the following example: link

Nginx use a custom domain name

I'm a beginner with nginx and I just created a server, I can access it through localhost\ on my browser but when I put my custom domain name it doesn't work, I just get the message "this site is not accessible", here are my server config and my Dockerfile
--- ft_server.conf ---
server {
autoindex on;
listen 80 default;
listen [::]:80 default;
root /var/www/ft_server/html;
index index.html;
server_name ft_server localhost;
location / {
try_files $uri $uri/ =404;
}
}
--- Dockerfile ---
FROM debian:buster
ARG SHARED=/var/www/ft_server
RUN mkdir -p ${SHARED}
RUN apt-get update && apt-get upgrade -y && apt-get install -y wget tar
RUN apt-get install -y nginx
RUN mkdir -p /var/www/ft_server/html
RUN chown -R $USER:$USER /var/www/ft_server/html
ADD /srcs/conf/ft_server.conf /etc/nginx/sites-available
RUN ln -s /etc/nginx/sites-available/ft_server.conf /etc/nginx/sites-enabled/
ADD /srcs/conf/nginx.conf /etc/nginx
RUN rm -f /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
I'm running my container by doing:
docker build -t ft_server .
docker run -ti -p 80:80 ft_server:latest
then inside of my container:
service nginx start
When I see nginx tutorials it seems like it's supposed to work for custom domain names but I don't know why it doesn't for me, thanks for your help

Adding LUA module to nginx

I rpm installed nginx 1.12 on a redhat 7.5 server and It also has LUA 5.1.4
I downloaded lua-nginx-module-0.10.13 tar ball and put it under /etc/nginx/modules, but I am not able to run nginx with LUA auth file.
I also have openresty under /opt/openresty/ ..
http://openresty.org/en/installation.html
I followed the "make" method here.
Unfortunately this server doesnt have access to the internet so I cant install stuff from git which slows this down considerably. I am not sure how to add the module here. Any comments would be helpful.
This is what my nginx config looks like ..
server
{
listen 80;
access_log /opt/elk/logs/nginx/access.log main;
#auth_basic "admin";
#auth_basic_user_file "/etc/nginx/passwd";
client_max_body_size 100M;
location /
{
proxy_pass http://127.0.0.1:9200;
keepalive_timeout 300s;
#auth_basic on;
auth_basic "admin";
auth_basic_user_file "/etc/nginx/passwd";
access_by_lua_file '/etc/nginx/authorized.lua';
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root /usr/share/nginx/html;
}
}
The lua_access_file is causing an error
nginx: [emerg] unknown directive "access_by_lua_file" Is there some "include" I need to define in the config to get rid of this ?
Thanks.
I am breaking down your problem into the small task, as per question and my understanding.
1) The error clearly says that you have not install Lua-nginx-module properly.
Lua-nginx-module documentation
2) The server does not have access to the internet so cannot download from git.
*
Assuming you are doing ssh into your machine from windows. So, please
check below link to copy the files from windows to Linux.
Installing/Accessing via WinSCP
how-to-copy-files-from-one-machine-to-another-using-ssh
this step will get all the necessary files on your server.
3) Steps to install nginx with lua-nginx-module.
lua nginx module compatibility check.
Nginx Compatibility
The latest version of this module is compatible with the following versions of Nginx:
1.13.x (last tested: 1.13.6)
1.12.x
1.11.x (last tested: 1.11.2)
1.10.x
1.9.x (last tested: 1.9.15)
1.8.x
1.7.x (last tested: 1.7.10)
1.6.x
Nginx cores older than 1.6.0 (exclusive) are not supported.
referance document for nginx compatibility
Prerequisites
**- Centos/RHEL**[**In case if internet is working in your server**].
yum install -y wget unzip gcc make openssl-devel pcre-devel zlib-devel
- Downloading .rpm package manually and installing.
Search the prerequisites from the RPM resource site
RPM resource site
Copy the file in your Linux box
Please refer above point (2)"The server does not have access to the internet so cannot download from git".
Install with the following command.
rpm -i rpm-package-name
Install-rpm-file-on-linux
- Tarball installation for prerequisites.
- [Installing gcc from source code ][6] Similarly,you can look for
other prerequistes.
Downloading the source
$ rm -fr /tmp/nginx-build
$ mkdir /tmp/nginx-build
$ cd /tmp/nginx-build
$ wget http://nginx.org/download/nginx-1.13.0.tar.gz
$ wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
$ wget -O nginx_devel_kit.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
$ wget -O nginx_lua_module.tar.gz https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
Extracting
$ tar xvf LuaJIT-2.0.4.tar.gz
$ tar xvf nginx-1.11.10.tar.gz
$ tar xvf nginx_devel_kit.tar.gz
$ tar xvf nginx_lua_module.tar.gz
Building LuaJIT
To build Nginx with LuaJIT, we need to build LuaJIT first. This is as simple as a make command
$ cd /tmp/nginx-build/LuaJIT-2.0.4
$ make install
==== Building LuaJIT 2.0.4 ====
make -C src
make[1]: Entering directory `/tmp/nginx/LuaJIT-2.0.4/src'
...
...
ln -sf luajit-2.0.4 /usr/local/bin/luajit
==== Successfully installed LuaJIT 2.0.4 to /usr/local ====
Building Nginx
$ cd /tmp/nginx-build/nginx-1.11.10
$ LUAJIT_LIB=/usr/local/lib LUAJIT_INC=/usr/local/include/luajit-2.0 \
./configure \
--user=nobody \
--group=nobody \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module ${NGINX_DEBUG:+--debug} \
--with-cc-opt=-O2 --with-ld-opt='-Wl,-rpath,/usr/local/lib' \
--add-module=/tmp/nginx/ngx_devel_kit-0.3.0 \
--add-module=/tmp/nginx/lua-nginx-module-0.10.8
$ make install
Syntanx Check
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Nginx Lua Testing
*As per you nginx file.
location /
{
proxy_pass http://127.0.0.1:9200;
keepalive_timeout 300s;
#auth_basic on;
auth_basic "admin";
auth_basic_user_file "/etc/nginx/passwd";
access_by_lua_file '/etc/nginx/authorized.lua'; }
Reload / restart nginx
systemctl nginx restart
systemctl nginx reload.
how-to-reload-nginx-systemctl-or-nginx-s
In case you are running nginx with docker you can just follow the instructions on the docker-nginx github repository.
In your nginx-conf make sure to also load the module by adding
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
load_module modules/ngx_stream_lua_module.so;
Now in most Operating Systems (e.g. Ubuntu, etc) Lua support for Nginx can be enabled by installing the libnginx-mod-http-lua package. e.g:
sudo apt install libnginx-mod-http-lua
This package includes the dynamic library and the corresponding load_module directive in /usr/share/nginx/modules-available/mod-http-lua.conf which is usually enabled when the package is installed (by softlinking it into the /etc/nginx/modules-enabled/ directory).
Akshay barahate's answer is long and comprehensive.
However to answer your question
Is there some "include" I need to define in the config to get rid of this ?
Try adding
load_module "ngx_http_lua_module.so"
The package name may vary, the above file name is derived from an ubuntu 18.04 installation from package.
(Usually you find your modules in /usr/share/nginx/modules)
Note: If you compiled nginx from source (which seems to be the recommended way to get lua running), pathes and filenames can vary.
Happy codin'.

Openresty Hello world with docker

I'm trying make my application dockerize for that I've been following official openresty dockerfile. Os in my system is Ubuntu 16.04 64 bit.
I have already pull that image using this cmd.
docker pull openresty/openresty:1.11.2.3-xenial
Now I want to use this image and want make simple hello world application. For that I have created my work directory, create one custom dockerfile and build my custom image with that. And finaly I run that image. Below is my dockerfile content.
FROM openresty/openresty:1.11.2.3-xenial
EXPOSE 8080
CMD nginx -p `pwd` -c nginx.conf
nginx.conf
worker_processes 1;
error_log stderr notice;
events {
worker_connections 1024;
}
http {
include /usr/local/openresty/nginx/conf/mime.types;
server {
listen 8888;
location / {
default_type text/html;
content_by_lua_file "app.lua";
}
}
}
app.lua
ngx.say('Hello World!')
ngx.exit(200)
Build image
docker build -t user/openresty .
Start container
docker run rahul/openresty
When I try to start container, it gives me an error like nginx: invalid option: "/bin/sh"
I have no idea that I'm going on right or wrong direction.
Update:
docker run -it -p 8888:80 -v /home/software/docker/openresty:/usr/local/openresty/nginx/html:ro openresty/openresty:1.11.2.3-xenial
I just used this CLI and it's start showing index.html that I have created. Again I tried to link my custom nginx.conf using below CLI but it's not working.
docker run -it -p 8888:8888 -v /home/software/docker/openresty:/usr/local/openresty/nginx/html:ro -v /home/software/docker/openresty/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro openresty/openresty:1.11.2.3-xenial
docker run -it -p 8888:8888 -v $(pwd):/app openresty/openresty:1.11.2.3-xenial -p /app -c nginx.conf
With below command it starts working but can anyone please explain it?
docker run -it -p 8888:8888 -v $(pwd):/app openresty/openresty:1.11.2.3-xenial -p /app -c nginx.conf
You have wrong port settings, look:
EXPOSE 8080
!=
listen 8888;

docker custom nginx container failed to start

I am trying to build a nginx image from scratch (instead of using the official nginx image)
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
COPY ./files/ /var/www/html/
CMD service nginx start
And this is my nginx.conf file under current directory.
server {
root /var/www/html
location / {
index.html
}
}
And my dummy index.html file under ./files folder
<p1>hello world</p1>
I run this command
docker build -t hello-world .
And
docker run -p 80:80 hello-world
But I got error saying
* Starting nginx nginx
...fail!
What maybe the issue?
Don't use "service xyz start"
To run a server inside a container, don't use the service command. That is a script which will run the requested server in the background, and then exit. When the script exits, the container will stop (because that script was the primary process).
Instead, directly run the command that the service script would have started for you. Unless it exits or crashes, the container should remain running.
CMD ["/usr/sbin/nginx"]
nginx.conf is missing the events section
This is required. Something like:
events {
worker_connections 1024;
}
The server directive is not a top-level element
You have server { } at the top level of the nginx.conf, but it has to be inside a protocol definition such as http { } to be valid.
http {
server {
...
nginx directives end with a semicolon
These are missing at the end of the root statement and your index.html line.
Missing the "index" directive
To define the index file, use index, not just the filename by itself.
index index.html;
There is no HTML element "p1"
I assume you meant to use <p> here.
<p>hello world</p>
Final result
Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
RUN rm -v /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
COPY ./files/ /var/www/html/
CMD ["/usr/sbin/nginx"]
nginx.conf:
http {
server {
root /var/www/html;
location / {
index index.html;
}
}
}
events {
worker_connections 1024;
}
daemon off;
one can use directly the official image of nginx in docker hub, just start your docker file with this line : FROM nginx
here is an example of docker file that you can use :
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY static-html-directory /usr/share/nginx/html
EXPOSE 80
as you see there is no need to use a CMD to run your nginx server

Resources