docker run can't find application - nginx

my project structure is:
/docker-test
/app
/static
....
/templates
....
-__init__.py
....
-nginx.conf
-supervisord.conf
-uwsgi.ini
-Dockerfile
-app.py
-requirements.txt
I normally run the app by going into /docker-test>python app.py
Dockerfile:
FROM python:2.7
# Install uWSGI
RUN pip install uwsgi
# Standard set up Nginx
ENV NGINX_VERSION 1.9.11-1~jessie
RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \
&& echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y ca-certificates nginx=${NGINX_VERSION} gettext-base \
&& rm -rf /var/lib/apt/lists/*
# 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
EXPOSE 80 443
# Finished setting up Nginx
# Make NGINX run on the foreground
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Remove default configuration from Nginx
RUN rm /etc/nginx/conf.d/default.conf
# Copy the modified Nginx conf
COPY nginx.conf /etc/nginx/conf.d/
# Copy the base uWSGI ini file to enable default dynamic uwsgi process number
COPY uwsgi.ini /etc/uwsgi/
# Install Supervisord
RUN apt-get update && apt-get install -y supervisor \
&& rm -rf /var/lib/apt/lists/*
# Custom Supervisord config
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY . /deploy
WORKDIR /deploy
RUN pip install -r /deploy/requirements.txt
CMD ["/usr/bin/supervisord"]
app.py:
#!flask/bin/python
from app import app
from flask import url_for
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=80)
app.add_url_rule('/favicon.ico', edirect_to=url_for('static', filename='favicon.ico'))
supervidord.conf:
[supervisord]
nodaemon=true
[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /etc/uwsgi/uwsgi.ini --ini /deploy/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
nginx.conf:
server {
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
}
uwsgi.ini:
[uwsgi]
wsgi-file=/app.py
socket = /tmp/uwsgi.sock
chown-socket = nginx:nginx
chmod-socket = 664
cheaper = 2
processes = 16
I am able to docker build and run without issue.
I get 500 error when trying to access the app in chrome:
no python application found

What is your current working directory? This just sounds like wsgi-file=/app.py should be wsgi-file=./app.py or you need to reference the correct absolute location such as wsgi-file=/deploy/app/app.py.

Related

Getting an error while starting up nginx container

I'm using dockerfile to create an nginx image and it is created successfully but when I try to start the container I get below error
Error - exec /docker-entrypoint.sh: no such file or directory
If I use /bin/bash as an entrypoint then I can see that the /docker-entrypoint.sh is present inside the image.
FROM linux:8
ARG USER="oracle"
RUN useradd --create-home --home-dir /app --shell /bin/bash ${USER} && \
dnf update -y && \
dnf module install -y nginx:1.20 && \
dnf clean all && \
ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stderr /var/log/nginx/error.log && \
mkdir /var/cache/nginx && \
chown -R ${USER}:${USER} /var/cache/nginx && \
chmod -R 775 /var/cache/nginx && \
chown -R ${USER}:${USER} /var/lib/nginx && \
chown -R ${USER}:${USER} /var/log/nginx && \
chown -R ${USER}:${USER} /etc/nginx && \
chmod -R 775 /etc/nginx
COPY --chown=oracle:oracle conf.d /etc/nginx/conf.d
COPY --chown=oracle:oracle nginx.conf /etc/nginx/
COPY --chown=oracle:oracle docker-entrypoint.d /docker-entrypoint.d
COPY --chown=oracle:oracle docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
USER ${USER}
WORKDIR /app
STOPSIGNAL SIGQUIT
EXPOSE 8000
CMD ["nginx", "-g", "daemon off;"]
Try to replace ENTRYPOINT ["/docker-entrypoint.sh"] to ENTRYPOINT ["bash", "/docker-entrypoint.sh"]

How to run only one thing as root in docker

I'm trying to create a Dockerfile which runs as non-root user.
When i building this all works fine, but nginx cannot write the log file because it dosen't have enough permissions. Can I, when building a Docker, give root permissions only for nginx?
I'm trying chmod, chown for blocked directories. Doesn't work
FROM php:7.1-fpm-alpine
RUN apk add --no-cache shadow
RUN apk add --no-cache --virtual .ext-deps \
openssl \
unzip \
libjpeg-turbo-dev \
libwebp-dev \
libpng-dev \
freetype-dev \
libmcrypt-dev \
imagemagick-dev \
nodejs-npm \
nginx \
git \
inkscape
# imagick
RUN apk add --update --no-cache autoconf g++ imagemagick-dev libtool make pcre-dev \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& apk del autoconf g++ libtool make pcre-dev
# Install Blackfire
RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
&& curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version \
&& tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp \
&& mv /tmp/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini
RUN apk add -y icu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl
RUN docker-php-ext-configure pdo_mysql && \
docker-php-ext-configure opcache && \
docker-php-ext-configure exif && \
docker-php-ext-configure pdo && \
docker-php-ext-configure zip && \
docker-php-ext-configure gd \
--with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-webp-dir=/usr/include --with-freetype-dir=/usr/include && \
docker-php-ext-configure sockets && \
docker-php-ext-configure mcrypt
RUN docker-php-ext-install pdo zip pdo_mysql opcache exif gd sockets mcrypt && \
docker-php-source delete
RUN ln -s /usr/bin/php7 /usr/bin/php && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
mkdir -p /run/nginx
COPY ./init.sh /
COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY ./.env /
RUN chmod +x /init.sh
EXPOSE 80
RUN addgroup -g 1001 node \
&& adduser -u 1001 -G node -s /bin/sh -D node
ARG UID=1001
ARG GID=1001
ENV UID=${UID}
ENV GID=${GID}
RUN usermod -u $UID node \
&& groupmod -g $GID node
RUN chown 1001:1001 /var/lib/nginx -R
RUN mkdir -p /var/tmp/nginx
RUN chown 1001:1001 /var/tmp/nginx -R
USER node
ENTRYPOINT [ "/init.sh" ]
There are quite a few unknowns in your question, for example, the contents of your default.conf file. By default the nginx logs are stored in /var/log/nginx, but I'll assume you're overriding that in the configuration.
The next thing is that the master process of nginx needs to be run as root if you wan't it to be able to bind to system ports (0 - 1023) so in case you are using nginx as a web server and intend to use ports 80 and 443 you should stick with running the nginx process as root.
In case you plan to use other ports and are set on the idea of running the master process as non-root, then you can check this answer for suggestions on how to do that - https://stackoverflow.com/a/42329561/5359953
I am using the term master process a lot here, because nginx spawns worker processes to handle the actual requests and those can be run as a different user (Defined in the nginx configuration file)
I found the solution. I just changed RUN chown 1001:1001 /var/lib/nginx -R to RUN chown -R 1001:1001 /var/. Thats works fine
RUN chown -R 1001:1001 /var/
sometimes it's will be actually bad decision.
u can try add permissions like this
RUN chown -R 1001:1001 /var/tmp/nginx
RUN chown -R 1001:1001 /var/lib/nginx
RUN chown -R 1001:1001 /var/log/nginx
RUN chown -R 1001:1001 /run/nginx
I guess RUN chown 1001:1001 /var/lib/nginx -R work wrong because I set the flag -R too late

Deploying ASP.NET 4.* MVC and Web API applications to Linux server

Is there any way of Deploying ASP.NET 4.* MVC and Web API applications to Linux server? I searched and read about Docker but I think that is for .NET 5.
Appreciate your help!
I managed to do that with some bricolage on a Linux-based Docker image.
This helped me a lot: https://github.com/junalmeida/docker-mono-web
Basically, you can host your web application with nginx, but you also need fastcgi-mono-server4.
The dockerfile looks like this:
FROM mono:latest
RUN apt-get update \
&& apt-get install -y \
iproute2 supervisor ca-certificates-mono fsharp mono-vbnc nuget \
referenceassemblies-pcl mono-fastcgi-server4 nginx nginx-extras \
&& rm -rf /var/lib/apt/lists/* /tmp/* \
&& echo "daemon off;" | cat - /etc/nginx/nginx.conf > temp && mv temp /etc/nginx/nginx.conf \
&& sed -i -e 's/www-data/root/g' /etc/nginx/nginx.conf
# this copies nginx configuration files to the proper directory
COPY nginx/ /etc/nginx/
In the link above, the supervisord command is used to start both nginx and fastcgi-mono-server4.
It is configured in a supervisord.conf file like the following, in which the --appconfigdir specifies the root folder of your application:
[supervisord]
logfile=/var/log/supervisor/supervisord.log
logfile_maxbytes = 50MB
nodaemon=true
user=root
[program:mono]
command=fastcgi-mono-server4 --appconfigdir=appconfig --socket=unix --filename=/var/run/mono-fastcgi.sock --printlog --name=mono
user=root
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=nginx
user=root
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
and launched with this command:
/usr/bin/supervisord -c supervisord.conf
(a folder contains a configuration for nginx, too).

In vagrant can not access to the default keystone site with nginx

The vagrant server I configure with the following script still serve the default nginx page instead of the default keystone page.
Here the scripts I use:
The vagrant file:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "private_network", ip: "192.168.1.10"
config.vm.provider "virtualbox" do |vb|
config.vm.provision "file", source: "mongodb-org-3.2.repo", destination: "~/mongodb-org-3.2.repo"
config.vm.provision "shell", path: "provision.sh"
end
The provision file:
sudo yum -y update
sudo hostnamectl set-hostname melanie
echo "given hostname :"
hostnamectl status --static
echo -e "\e[1;34m
***************************************************
add host names
***************************************************"
sudo cp /etc/hosts /etc/hosts.origin
echo "192.168.1.10 melanie.misite.com melanie" | sudo tee -a /etc/hosts > /dev/null
echo -e "\e[1;34mIP, FQDN and Server name setted in /etc/hosts:"
cat /etc/hosts
echo -e "\e[1;34m
***************************************************
set timezone
***************************************************"
sudo timedatectl set-timezone America/Guayaquil
echo -e "\e[1;34msetted time zone:"
timedatectl | grep "Time zone"
echo -e "\e[1;34m
***************************************************
add automatic security update
***************************************************"
sudo yum -y install yum-cron
sudo sed -i.bak 's/.*update_cmd =.*/update_cmd = security/' /etc/yum/yum-cron.conf
sudo sed -i.bak 's/.*apply_updates =.*/apply_updates = yes/' /etc/yum/yum-cron.conf
sudo sed -n /update_cmd/p /etc/yum/yum-cron.conf
sudo sed -n /apply_updates/p /etc/yum/yum-cron.conf
sudo systemctl status yum-cron
sudo systemctl start yum-cron
echo -e "\e[1;34m
***************************************************
create limited user account
***************************************************"
sudo useradd me
sudo echo me:admin | chpasswd
echo -e "\e[1;34m
***************************************************
SSH Dameon Options
***************************************************"
sudo sed -i.bak 's/.*PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
echo yum-cron.conf modified parameters:
sudo sed -n /PermitRootLogin/p /etc/ssh/sshd_config
systemctl restart sshd
echo -e "\e[1;34m
***************************************************
installing fail2ban
***************************************************"
sleep 15 #put sleep hoping it will help to fail2ban to be installed => do not work
sudo yum -y install fail2ban
sudo yum -y install sendmail
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
systemctl start sendmail
systemctl enable sendmail
cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sed 's/.*backend =*/backend = systemd./' /etc/fail2ban/jail.local
echo -e "\e[1;34m
***************************************************
installing nginx
***************************************************"
sudo yum -y install epel-release
sudo yum -y install nginx
sudo systemctl start nginx
echo -e "\e[1;34m
***************************************************
configure nginx
***************************************************"
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
sudo mkdir /var/www/misite.com/logs
sudo cp /home/vagrant/misite.conf /home/vagrant/misite.com
sudo mv /home/vagrant/misite.com /etc/nginx/sites-available > /dev/null
sudo ln -s /etc/nginx/sites-available/misite.com /etc/nginx/sites-enabled
sudo rm -rf /etc/nginx/sites-available/default
sudo chown -R nginx:nginx /var/www
sudo service nginx restart > /dev/null
echo -e "\e[1;34m
***************************************************
installing nodejs
***************************************************"
sudo yum -y install npm
sudo yum -y install nodejs
node --version
echo -e "\e[1;34m
***************************************************
installing mongoDB
***************************************************"
sudo mv /home/vagrant/mongodb-org-3.2.repo /etc/yum.repos.d/mongodb-org-3.2.repo
sudo yum -y install mongodb-org
systemctl start mongod
systemctl status mongod
echo -e "\e[1;34m
***************************************************
installing keystone
***************************************************"
sudo npm install -g yo
sudo mkdir /var/www
sudo mkdir /var/www/misite.com
cd /var/www/misite.com
sudo npm install -g generator-keystone
sudo chown -R vagrant:vagrant /var/www/
The nginx server conf file (/etc/nginx/sites-available/misite.com):
Here the keystone site should be redirect to the port 80 of the vagrant server (I think the mistake is in this file but can not see where)
# IP which nodejs is running on
upstream app_misite.com {
server 0.0.0.0:3000;
}
# nginx server instance
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
access_log /var/www/misite.com/logs/access.log;
error_log /var/www/misite.com/logs/error.log;
location / {
root /var/www/misite.com;
index index.html index.htm;
try_files $uri $uri/ #node;
}
location #node {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://app_misite.com;
}
}
I also remove the default keyword from the /etc/nginx/nginx.conf
Then:
vagrant ssh
[vagrant#melanie ~]$cd /var/www/misite.com
[vagrant#melanie misite.com]$ yo keystone
[vagrant#melanie misite.com]$ node keystone
And I have keystone js running:
------------------------------------------------
KeystoneJS Started:
My Site is ready on http://0.0.0.0:3000
------------------------------------------------
But still see the default nginx page from http://192.168.1.10/
Any help will be appreciate.
Disclaimer: I'm not familiar with Nginx, I'm trying to see if it may be settings with Keystone that are affecting the port it is running on versus Nginx.
Keystone defaults to port 3000 (more specifically, process.env.PORT || 3000), unless you specify another one. If you can set the environment variable of port to whatever value you want (80 in this case), that should make it work on http://192.168.1.10:80/.
process.env.PORT = 3000
Looking at your nginx server conf file also shows this:
upstream app_misite.com {
server 0.0.0.0:3000;
}
Try changing :3000 to :80.
I think you have cupple of issues :
nginx installed on centos has a default nginx.conf file with a server directive so you cannot override this directive in your config misite file.
You need to remove the server default declaration in /etc/nginx/nginx.conf file or you can just use your provisioning script to copy a new default conf file without server declaration
I am also not even sure if the default file has an include directive on sites-available directory (look if you have include /etc/nginx/sites-enabled/*; in your conf file)
when you create the keystone app, it does not contain the /var/www/misite.com/logs/ directory and log file, I do not see you create them in your script so nginx will fail on this (btw you can create a directory structure with mkdir -pv single command)
The keystone app you created is owned by vagrant. Make sure vagrant is added to nginx group otherwise you might get a Forbidden exception when accessing your site
can help on centos if you dont want to fight with SELinux, just disabled it on a dev instance. edit the /etc/sysconfig/selinux and just set SELINUX=disabled

WordPress Docker Proxy Error 502 : The proxy server received an invalid response from an upstream server

Working on WordPress, We put a project on docker.
I had a 502 proxy error. I was able to fix it restarting docker. However, the problem is still here on coworkers installation.
I try to fix it. However, I can not recreate the bug, so I have no log for this error.
Here is the dockerfile:
FROM debian:8
MAINTAINER xxxxxxxxxxxxx
LABEL version="1.0"
LABEL description="Debian 8 / Apache 2 / PHP 5"
ARG DEBIAN_FRONTEND=noninteractive
ENV DOCKER_CONTAINER_APP=/var/www
RUN apt-get -y update && apt-get install -y \
apache2 \
php5 \
libapache2-mod-php5 \
mysql-server \
php5-mysql \
supervisor \
phpmyadmin
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
RUN /usr/sbin/mysqld & \
sleep 10s &&\
echo "GRANT ALL ON *.* TO admin#'%' IDENTIFIED BY 'heliopsis' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql
EXPOSE 3306
CMD ["/usr/bin/mysqld_safe"]
COPY ressources/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY ressources/my.cnf /etc/mysql/my.cnf
COPY ressources/init.sql /tmp/init.sql
RUN /etc/init.d/mysql start && mysql -uroot < /tmp/init.sql && /etc/init.d/mysql stop
COPY sql-dumps /tmp/sql-dumps
RUN /etc/init.d/mysql start && gunzip < /tmp/sql-dumps/dump.sql.gz | mysql -uroot -D nalian-local && /etc/init.d/mysql stop
RUN /etc/init.d/mysql start && mysql -uroot -e "SET PASSWORD = PASSWORD('heliopsis');" && /etc/init.d/mysql stop
# Dev env : show errors
RUN sed -i -e 's/^error_reporting\s*=.*/error_reporting = E_ALL/' /etc/php5/apache2/php.ini
RUN sed -i -e 's/^display_errors\s*=.*/display_errors = On/' /etc/php5/apache2/php.ini
RUN a2enmod rewrite
RUN mkdir /etc/apache2/ssl
COPY ressources/vhost /etc/apache2/sites-available/000-default.conf
RUN a2ensite 000-default
# add our local files in docker instance
ADD . $DOCKER_CONTAINER_APP
# add the docker instance as a volume
VOLUME $DOCKER_CONTAINER_APP
# define the workspace of the container
WORKDIR $DOCKER_CONTAINER_APP
# launching apache # startup
CMD cd wp-content; tar xzf $DOCKER_CONTAINER_APP/ressources/uploads.tar.gz; cd ..; ln -s wp-config-dev.php wp-config.php; ln -s htaccess_dev .htaccess;/usr/bin/supervisord
The install-vhost.sh :
#!/usr/bin/env bash
if [ -z "$1" ]
then
echo "dev name required"
exit
fi
if [ -z "$2" ]
then
echo "HTTP port required"
exit
fi
VHOST_TEMPLATE=`find . -name "*.DEV.rocks.conf"`
VHOST=`echo $VHOST_TEMPLATE | sed 's/.*\///' | sed "s/DEV/$1/"`
sudo cat $VHOST_TEMPLATE | replace "DEV" "$1" | replace "PORT" "$2" > /etc/apache2/sites-available/$VHOST
sudo a2ensite $VHOST
sudo service apache2 restart
And the vhost :
############## www.nalian.coralie.rocks
<VirtualHost *:80>
ServerName www.nalian.coralie.rocks
<Proxy *>
Allow from localhost
</Proxy>
ProxyPass / http://localhost:1250/
ProxyPassReverse / http://localhost:1250/
ProxyPreserveHost On
</VirtualHost>
Do you have any idea to help me?

Resources