docker custom nginx container failed to start - nginx

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

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

After setting up server blocks, Nginx is not serving my domain name

I am in the process of deploying my MERN app to a Digital Ocean droplet (Ubuntu 20.04 server).
I followed the steps in the following tutorial to install Nginx. [I have completed all the previous steps]
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
When I visit http://134.122.112.22, I see the Nginx landing page, as expected.
However, after setting up server blocks, when I visit http://sundaray.io, I get the following error.
sundaray.io is my domain name.
When I run the command /var/log/nginx/error.log, I see the following:
How can I fix the error?
EDIT-1
SERVER BLOCK
In order to create the server block, I executed the following commands in order:
mkdir -p /var/www/sundaray.io/html
nano /etc/nginx/sites-available/sundaray.io
Then, I pasted in the following configuration block.
server {
listen 80;
listen [::]:80;
root /var/www/sundaray.io/html;
index index.html index.htm index.nginx-debian.html;
server_name sundaray.io www.sundaray.io;
location / {
try_files $uri $uri/ =404;
}
}
ERROR
Executing the command cat /var/log/nginx/error.log gave me the following result:
EDIT-2
Executing chown -R nginx:nginx /var/www/sundaray.io/html threw the following error:
EDIT-3
Executing ps -elf |grep nginx gave the following result:
EDIT-4
When I executed the command ls /var/www/sundaray.io/html, I got the following result:
1. chmod 777 is NEVER a good idea.
NGINX operats under a runuser. How to check the runuser:
ps -elf |grep nginx. Normaly it will be nginx. Instead of 777 (Open for everyone) do chmod -R 755 /path/to/folder and chown -R nginx:nginx /path/to/folder
But agree. For troubleshooting it can be used. But back to your problem.
2. Directory-Listing disabled.
The error is telling you nginx can not list the directory content. Which is the default behavior. Make sure
root /var/www/sundaray.io/html;
This path exists AND
index index.html index.htm index.nginx-debian.html;
there are one of these files located!
Without any of these files NGINX can't load the default index file on /. Put something in /var/www/sundaray.io/html like
printf "<html>\n<body>\n<h1>Hello from NGINX</h1>\n</body>\n</html>" > /var/www/sundaray.io/html/index.html && chown nginx:nginx /var/www/sundaray.io/html/index.html. This should generate an index.html for you.
If you just want to test your server configuration without any files:
location / {
return 200 "Hello on $host$uri\n";
}

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

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;

Static webpage on Nginx Docker Container Missing 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

Resources