Home > OS >  docker automatically redirects to https
docker automatically redirects to https

Time:01-26

I've dockerized a ruby app and am trying to view it locally but every browser I have used redirects from http://0.0.0.0:3000 to https://0.0.0.0:3000 so I'm getting the browser error

This site can’t provide a secure connection 0.0.0.0 sent an invalid response.

I've tried clearing the cache, browsing history etc. rebooted, and included HTTPS_METHOD: noredirect in docker compose environment.

I can access the site from rails server using http://127.0.0.1:3000 which doesn't redirect and suggests it's something in the dockerfile or docker compose that is doing it. I'm new to docker so forgive me I'm missing something obvious.

Here's the dockerfile

FROM ruby:2.7.7-alpine3.16 AS builder

ENV RAILS_ENV production
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
ENV SECRET_KEY_BASE 1

# RUN mkdir /app
# WORKDIR /app

# Install required libraries on Alpine
# note: build-base required for nokogiri gem
# note: postgresql-dv required for pg gem
RUN apk update && \
    apk add tzdata postgresql-dev && \
    apk add postgresql-client && \
    apk add nodejs yarn && \
    apk add build-base && \
    gem install rails bundler && \
    # Throw errors if Gemfile has been modified since Gemfile.lock
    bundle config --global frozen 1

# Copy Gemfile so we can cache gems
COPY Gemfile Gemfile.lock ./

# Install Ruby gems
RUN bundle install --without development test

# Copy all application files
COPY . .

# Precompile assets
RUN bundle exec rails assets:precompile && \
    bundle exec rails webpacker:compile


RUN chmod  x entrypoint.sh
CMD ["/entrypoint.sh"]


EXPOSE 3000

Here's the docker compose

version: "3.8"

services:
  rails:

    build:
      dockerfile: Dockerfile
      context: .
    env_file:
      - rails.env
    environment:
      HTTPS_METHOD: noredirect
      DB_USERNAME: xxxxxxxx
      DB_PASSWORD: .env
      DB_DATABASE: xxxxxx_production
      DB_PORT: 5432
      DB_HOST: db
      RAILS_MAX_THREADS: 5
      
    ports:
      - "3000:3000"
    restart: always
    depends_on:
      - "db"
  db:
    image: postgres:13.9-alpine
    env_file:
      - db.env
    environment:
      HTTPS_METHOD: noredirect
      POSTGRES_USER: xxxxxxx
      PGDATA: /var/lib/postgresql/data/pgdata  
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

and the entrypoint.sh

#!/bin/sh

# Note: !/bin/sh must be at the top of the line,
# Alpine doesn't have bash so we need to use shell.
# Docker entrypoint script.
# Don't forget to give this file execution rights via `chmod  x entrypoint.sh`
# which I've added to the Dockerfile but you could do this manually instead.

# Wait until Postgres is ready before running the next step.
while ! pg_isready -q -h $DB_HOST -p $DB_PORT -U $DB_USERNAME
do
  echo "$(date) - waiting for database to start."
  sleep 2
done

# If the database exists, migrate. Otherwise setup (create and migrate)
echo "Running database migrations..."
bundle exec rails db:migrate 2>/dev/null || bundle exec rails db:create db:migrate
echo "Finished running database migrations."

# Remove a potentially pre-existing server.pid for Rails.
echo "Deleting server.pid file..."
rm -f /tmp/pids/server.pid

# Start the server.
echo "Starting rails server..."
bundle exec rails server

Any advice much appreciated. Thanks.

CodePudding user response:

Docker doesn't redirect http to https. There is one possible option in Ruby on Rails. In your environment file for production settings, you're forcing ssl.

config.force_ssl = true

This option forces your browser to use https instead to http. This means the rails server sends a redirect to the browser to https. But your server is not configured for https, so the described errors occurs.

References:

CodePudding user response:

You're probably using http://0.0.0.0:3000/ because your app tells you that that's the address that it's listening on.

However, 0.0.0.0 is not the IP address of the service. It's the address that it's 'bound' to. And 0.0.0.0 means that it'll accept connections from anywhere.

The actual address of your service is http://localhost:3000/ or, as you've discovered, http://127.0.0.1:3000/. The two addresses are equivalent.

Read this for more information.

  • Related