Been trying for hours to figure out why docker run -p 3000:3000 will successfully run my image but not docker-compose up . When I run the former command the routing for my app works and I can visit the URL in the browser. However when I use the latter command the browser returns me an nginx 404 error.
Dockerfile:
#Base Image node:12.18.4-alpine
FROM node:12.18.4-alpine
#Set working directory to /app
WORKDIR /InboundBackend
#Set PATH /app/node_modules/.bin
ENV PATH /InboundBackend/node_modules/.bin:$PATH
#Copy package.json in the image
COPY package.json ./
RUN npm install
RUN npm ci --only-production
#Copy the app
COPY . /InboundBackend/
EXPOSE 3000
##Start the app
# CMD ["ls"]
CMD ["npm", "start"]
Nginx Dockerfile:
FROM nginx
COPY default.conf /etc/nginx/conf.d/default.conf
docker-compose.yaml
version: '3.8'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
restart: unless-stopped
ports:
- "3000:3000"
nginx:
image: nginx:mainline-alpine
container_name: webserver
restart: unless-stopped
build:
context: ./nginx-conf
ports:
- "80:80"
- "443:443"
My nginx configuration:
listen 80;
listen [::]:80;
#location ~ /.well-known/acme-challenge {
# allow all;
# root /var/www/html;
#}
#location / {
# rewrite ^ https://$host$request_uri? permanent;
#}
location /graphql {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://nodejs:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
#location #Nodejs {
# proxy_pass http://Nodejs:3000;
# add_header X-Frame-Options "SAMEORIGIN" always;
# add_header X-XSS-Protection "1; mode=block" always;
# add_header X-Content-Type-Options "nosniff" always;
# add_header Referrer-Policy "no-referrer-when-downgrade" always;
# add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
#}
}
server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
server_name api.***.com www.api.***.com;
server_tokens off;
# ssl_certificate /etc/letsencrypt/live/nodejs.devopslee.com/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/nodejs.devopslee.com/privkey.pem;
ssl_buffer_size 8k;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
}
When I run docker-compose up the nodejs server runs the same way and I can see all the server logging. Again it's just that the endpoints are unreachable
Thank you in advance
I try to run a Dockerized VueJS app. This is my Dockerfile
FROM node:lts-alpine
# install simple http server for serving static content
RUN npm install -g http-server
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
# build app for production with minification
RUN npm run build
CMD [ "http-server", "-p 5000", "dist" ]
I spin up the container with the following command:
sudo docker run -it -p 5000:5000 vuejs
My default nginx file has this relevant part:
server {
listen 443;
server_name data-mastery.com;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_certificate /etc/letsencrypt/live/data-mastery.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/data-mastery.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass https://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 600s;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Protocol $scheme;
}
}
Unfortunately when I try to access I donĀ“t get redirected and trying to access it with on my port (https://data-mastery.com:5000/) results in an error:
ERR_SSL_PROTOCOL_ERROR
Another container with runs on port 8443 works fine. What is wrong here?
It works as expected. Your Vue app is available on https://data-mastery.com (LE cert). And port 5000 is occupied by Keycloak (but only http protocol is enabled) - http://data-mastery.com:5000/.
I have a web project that I want to deploy using docker-compose and nginx.
Locally, I:
docker-compose build
docker-compose push
If I docker-compose up, I can access localhost/ and get redirected to my index.html.
Now on my ec2 instance (a regular ec2 instance where I installed docker and docker-compose) I docker-compose pull, then docker-compose up.
All the containers launch correctly and I can exec sh into my nginx container and see there's a /facebook/index.html file.
If I go to [instance_ip]/index.html, everything works as expected.
If I go to [instance_ip]/, I get a 404 response.
nginx receives the request (I see it in the access logs) but does not redirect to index.html.
Why is the index directive not able to redirect to my index.html file?
I tried to:
Reproduce locally by remove all local images and pulling from my registry.
Kill my ec2 instance and launch a new one.
But I got the same result.
I'm using docker-compose 1.11.1 and docker 17.05.0. On the ec2 instance it's docker 17.03.1 and I tried both docker-compose 1.11.1 and 1.14.1 (Sign that I'm a bit desperate ;)).
An extract from my docker-compose file:
nginx:
image: [image from registry]
build:
context: ./
dockerfile: deploy/nginx.dockerfile
ports:
- "80:80"
depends_on:
- web
My nginx image starts from alpine, installs nginx, adds the index.html file and copies my conf file in /etc/nginx/nginx.conf.
Here's my nginx config. I checked that it is present on the running containers (both locally and on ec2).
# prevent from exiting when using `run` to launch container
daemon off;
worker_processes auto;
#
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
sendfile off;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
server {
error_log /var/log/nginx/file.log debug;
listen 80 default_server;
# root /home/domain.com;
# Bad developers use underscore in headers.
underscores_in_headers on;
# root should be out of location block
root /facebook;
location / {
index index.html;
# autoindex on;
try_files $uri #app;
}
location #app {
include uwsgi_params;
# Using docker-compose linking, the nginx docker-compose service depends on a 'web' service.
uwsgi_pass web:3033;
}
}
}
I have no idea why the container is behaving differently on the ec2 instance.
Any pointers appreciated!
Passenger will start up on port 80, but only the home page (which is 100% HTML) shows up. No other page will resolve. And even stranger, all traffic that fails to resolve is forwarded to HTTPS (which, of course, also fails to resolve).
This works:
rvmsudo passenger start --daemonize
This does not work:
rvmsudo passenger start --daemonize --port 80
My config.ru is pretty standard, too:
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
I am using Rails 4.2.0 and Ruby 2.2.2 with Passenger 5.0.7
Anyone have any ideas?
nginx conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini
#passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.2.2
app specific conf:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name www.mydomain.com;
# Tells Nginx to serve static assets from this directory.
root /var/www/mydomain/public;
location / {
# Tells Nginx to forward all requests for www.foo.com
# to the Passenger Standalone instance listening on port 4000.
proxy_pass http://127.0.0.1:4000;
# These are "magic" Nginx configuration options that
# should be present in order to make the reverse proxying
# work properly. Also contains some options that make WebSockets
# work properly with Passenger Standalone. Please learn more at
# http://nginx.org/en/docs/http/ngx_http_proxy_module.html
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_buffering off;
}
}
I think you need a different "setup"/configuration if you want to run passenger behind a nginx.
Nginx should listen on port 80 (its specified in the server-section of your nginx.conf), and forwards traffic to your app running under passengers hood (specified in nginx.conf to be on port 4000, but started by hand at port 80), if I do not misread.
Probably nginx tells you that its unhappy in its logfile /var/log/nginx.log I believe. You can confirm what is sitting on port 80 by executing netstat -tlpn .
The nginx configuration for gitlab is:
# GITLAB
# Maintainer: #randx
# App Version: 3.0
upstream gitlab {
server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen YOUR_SERVER_IP:80; # e.g., listen 192.168.1.1:80;
server_name YOUR_SERVER_FQDN; # e.g., server_name source.example.com;
root /home/gitlab/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://gitlab;
}
}
What should i change to serve gitlab as a surURI, www.mysuperserver.com/gitlab
i've tried many different things, but nothing worked
thanks
I've successfully made it working under a subdir url.
follow the instruction in the source code, like in /home/git/gitlab/config/gitlab.yml
# Uncomment and customize the last line to run in a non-root path
# WARNING: We recommend creating a FQDN to host GitLab in a root path instead of this.
# Note that four settings need to be changed for this to work.
# 1) In your application.rb file: config.relative_url_root = "/gitlab"
# 2) In your gitlab.yml file: relative_url_root: /gitlab
# 3) In your unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
# 4) In ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab"
# To update the path, run: sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
#
relative_url_root: /gitlab
change the nginx config to serve a suburi, plz refer to my example below:
The key point is the root under context server and alias under location. Plz refer to nginx pitfalls, nginx root note for more details.
# default.conf for nginx
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen 80;
server_name $YOUR_DOMAIN;
# other settings, especially root settings, like below
# root /usr/local/nginx/html;
location /gitlab {
# serve static files from defined root folder;
alias /home/git/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://gitlab;
}
# other locations' settings...
}
As of Gitlab 5.3 you can configure it to run in a suburi out of the box using the official installation document.
Uncomment line 8 of config/puma.rb:
ENV['RAILS_RELATIVE_URL_ROOT'] = "/"
Similarly for line 23 in config/gitlab.yml:
relative_url_root: /
I didn't have to modify my nginx config at all for it to work.
For old versions, for example, gitlab 7.4.5 , there is no gitlab-git-http-server (gitlab-workhorse). But there are some solutions for gitlab 7.4.5 to use suburi in the doc.
config/application.rb
# Relative url support
# Uncomment and customize the last line to run in a non-root path
# WARNING: We recommend creating a FQDN to host GitLab in a root path instead of this.
# Note that following settings need to be changed for this to work.
# 1) In your application.rb file: config.relative_url_root = "/gitlab"
# 2) In your gitlab.yml file: relative_url_root: /gitlab
# 3) In your unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
# 4) In ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab"
# 5) In lib/support/nginx/gitlab : do not use asset gzipping, remove block starting with "location ~ ^/(assets)/"
#
# To update the path, run: sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
#
# config.relative_url_root = "/gitlab"
config/gitlab.yml
# WARNING: See config/application.rb under "Relative url support" for the list of
# other files that need to be changed for relative url support
# relative_url_root: /gitlab
Configuring gitlab 7.4.5 with other website using same port and same domain name in a nginx configuration or apache configuration file seems still a challenge. I did not get this. I can access gitlab as www.mydomain.com/gitlab , but I did not get my another website in another root directory with www.mydomain.com. It should be resoved by configure nginx or apache. Hope people who are familiar with nginx or apache can give a solution.
Other references.Support installing GitLab in a relative URL path or sub directory #1950
EDIT
It works now.
stackoverflow suggest to use www.example.com as example here.
www.example.com/gitlab access gitlab.
www.example.com access another website, say, my blog.
Steps:
config/application.rb file: config.relative_url_root = "/gitlab"
config/gitlab.yml file: relative_url_root: /gitlab
config/unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
../gitlab-shell/config.yml: gitlab_url: "http://www.example.com/gitlab"
copy lib/support/nginx/gitlab to gitlab.conf for nginx and do not use asset gzipping, remove block starting with "location ~^/(assets)/"
run: sudo -u git -H bundle exec rake assets:precompile
RAILS_ENV=production
Urls:
config/gitlab.yml file: host: example.com port: 80
config/unicorn.rb: listen "127.0.0.1:9095"
../gitlab-shell/config.yml: gitlab_url:
http://www.example.com/gitlab
gitlab will give git two way to access:
git#example.com:sample-project.git
http://example.com/gitlab/sample-project.git
I am not using https.
If you got:
git#example.com/gitlab:sample-project.git
http://example.com/gitlab/gitlab/sample-project.git
You might configure config/gitlab.yml as host: example.com/gitlab. Just remove the /gitlab.
nginx configuration file:
###################################
## configuration ##
###################################
##
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0;
}
## Normal HTTP host
server {
#listen *:80 default_server;
listen *:80 default_server;
server_name www.example.com; ## Replace this with something like gitlab.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice
#root /home/git/gitlab/public;
root html;
location /{
#root html;
index index.html index.htm;
}
## Increase this if you want to upload large attachments
## Or if you want to accept large git objects over http
client_max_body_size 20m;
## Individual nginx logs for this GitLab vhost
access_log logs/example.gitlab_access.log;
error_log logs/example.gitlab_error.log;
location /gitlab {
alias /home/git/gitlab/public;
## Serve static files from defined root folder.
## #gitlab is a named location for the upstream fallback, see below.
try_files $uri $uri/index.html $uri.html #gitlab;
}
## If a file, which is not found in the root folder is requested,
## then the proxy passes the request to the upsteam (gitlab unicorn).
location #gitlab {
## If you use HTTPS make sure you disable gzip compression
## to be safe against BREACH attack.
# gzip off;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Frame-Options SAMEORIGIN;
proxy_pass http://gitlab;
}
## Enable gzip compression as per rails guide:
## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
## WARNING: If you are using relative urls remove the block below
## See config/application.rb under "Relative url support" for the list of
## other files that need to be changed for relative url support
#location ~ ^/(assets)/ {
# root /home/git/gitlab/public;
# #gzip_static on; # to serve pre-gzipped version
# expires max;
# add_header Cache-Control public;
#}
error_page 502 /502.html;
}
Apache(2.2.9) configuration file: Reference gitlab.conf for gitlab 6.0.6 and gitlab-8.0-apache2.2.conf for gitlab 8.0.0 on apache 2.2
# Module dependencies
# mod_rewrite
# mod_proxy
# mod_proxy_http
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "/data/webapp/www/wordpress"
ServerName www.example.com
ServerAlias example.com
#ErrorLog "logs/wordpress-error_log"
#CustomLog "logs/wordpress-access_log" common
#SetEnv ZF2_PATH "/data/webapp/www/ZendFramework-2.3.3/library"
SetEnv APPLICATION_ENV "development"
<Directory /data/webapp/www/wordpress>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#ServerName www.example.com
ServerSignature Off
ProxyPreserveHost On
# Ensure that encoded slashes are not decoded but left in their encoded state.
# http://doc.gitlab.com/ce/api/projects.html#get-single-project
AllowEncodedSlashes NoDecode
<Location /gitlab>
Order deny,allow
Allow from all
ProxyPassReverse http://127.0.0.1:9095
ProxyPassReverse http://www.example.com//
RewriteEngine on
#RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]
</Location>
#apache equivalent of nginx try files
# http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
# http://stackoverflow.com/questions/10954516/apache2-proxypass-for-rails-app-gitlab
# RewriteEngine on
# RewriteCond /code/gitlab/{REQUEST_FILENAME} !-f
# RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
#DocumentRoot /home/git/gitlab/public
Alias /gitlab /home/git/gitlab/public
#Set up apache error documents, if back end goes down (i.e. 503 error) then a maintenance/deploy page is thrown up.
ErrorDocument 404 /404.html
ErrorDocument 422 /422.html
ErrorDocument 500 /500.html
ErrorDocument 503 /deploy.html
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
ErrorLog logs/example.com_error.log
CustomLog logs/example.com_forwarded.log common_forwarded
CustomLog logs/example.com_access.log combined env=!dontlog
CustomLog logs/example.com.log combined
</VirtualHost>
Have you resolved this issue yet?
If not, try updating the location / directive to:
location /gitlab {
# serve static files from defined root folder;.
root /home/gitlab/gitlab/public;
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
If that doesn't work, please paste the last few lines of /var/log/nginx/gitlab_error.log.
This configuration works
# GITLAB
# Maintainer: #randx
# App Version: 3.0
upstream gitlab {
server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen 80; # e.g., listen 192.168.1.1:80; 37.59.125.28:
server_name gitlab.<YOUR_DOMAIN>.com; # e.g., server_name source.example.com;
root /home/gitlab/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://gitlab;
}
}
And I had a bad symbolic link between nginx available configuration and the enabled configuration.