CORS blocked in dockerized Django REST + Angular project












0















I decided to dockerize an existing Django REST + Angular app today. The website is displaying as it should, however CORS requests are being blocked.




Access to XMLHttpRequest at 'http://localhost:8000/branches/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.




The angular App is dockerized using Nginx. I was using this tutorial in the progress (docker + compose files are listed below).



Strange thing about this is that I had this problem before and solved it through the use of the djang-cors-headers package. Along with CORS_ORIGIN_ALLOW_ALL = True, this no longer seems to be solving the issue.



Can this be related due to the fact that the application now runs in containers? I'm providing the dockerfiles for the projects along with the docker-compose file below. Not sure if they are relevant though.



Dockerfile for the Angular project:



FROM tiangolo/node-frontend:10 as build-stage
WORKDIR /app

COPY package*.json /app/
RUN npm install

COPY ./ /app/
ARG configuration=production
RUN npm run build -- --output-path=./dist/out --configuration $configuration

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
COPY --from=build-stage /app/dist/out/ /usr/share/nginx/html

# Copy the default nginx.conf provided by tiangolo/node-frontend
COPY --from=build-stage /nginx.conf /etc/nginx/conf.d/default.confenter


Dockerfile for the Django REST project:



# Pull base image
FROM python:3.7

# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# create root directory
RUN mkdir /web

# set working directory
WORKDIR /web

# Coppy current directory contents into the container
ADD . /web/

# Install any needed packages in requirements.txt
RUN pip install -r requirements.txt


And the docker-compose.yml file:



version: '3'

services:
db:
image: mysql:5.7
restart: always
container_name: db
environment:
- MYSQL_HOST=localhost
- MYSQL_PORT=3306 # cannot change this port to other number
- MYSQL_ROOT_HOST=%
- MYSQL_DATABASE=test
- MYSQL_USER=belter
- MYSQL_PASSWORD=belter_2017
- MYSQL_ROOT_PASSWORD=123456_abc
ports:
- '3302:3306'

web:
build: ./orca/
restart: always
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
container_name: web
volumes:
- ./orca:/code
ports:
- '8000:8000'
depends_on:
- db

angular:
build: ./ng-orca-new/
container_name: angular
ports:
- '3000:80'


Thanks in advance!










share|improve this question



























    0















    I decided to dockerize an existing Django REST + Angular app today. The website is displaying as it should, however CORS requests are being blocked.




    Access to XMLHttpRequest at 'http://localhost:8000/branches/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.




    The angular App is dockerized using Nginx. I was using this tutorial in the progress (docker + compose files are listed below).



    Strange thing about this is that I had this problem before and solved it through the use of the djang-cors-headers package. Along with CORS_ORIGIN_ALLOW_ALL = True, this no longer seems to be solving the issue.



    Can this be related due to the fact that the application now runs in containers? I'm providing the dockerfiles for the projects along with the docker-compose file below. Not sure if they are relevant though.



    Dockerfile for the Angular project:



    FROM tiangolo/node-frontend:10 as build-stage
    WORKDIR /app

    COPY package*.json /app/
    RUN npm install

    COPY ./ /app/
    ARG configuration=production
    RUN npm run build -- --output-path=./dist/out --configuration $configuration

    # Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
    FROM nginx:1.15
    COPY --from=build-stage /app/dist/out/ /usr/share/nginx/html

    # Copy the default nginx.conf provided by tiangolo/node-frontend
    COPY --from=build-stage /nginx.conf /etc/nginx/conf.d/default.confenter


    Dockerfile for the Django REST project:



    # Pull base image
    FROM python:3.7

    # Set environment varibles
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1

    # create root directory
    RUN mkdir /web

    # set working directory
    WORKDIR /web

    # Coppy current directory contents into the container
    ADD . /web/

    # Install any needed packages in requirements.txt
    RUN pip install -r requirements.txt


    And the docker-compose.yml file:



    version: '3'

    services:
    db:
    image: mysql:5.7
    restart: always
    container_name: db
    environment:
    - MYSQL_HOST=localhost
    - MYSQL_PORT=3306 # cannot change this port to other number
    - MYSQL_ROOT_HOST=%
    - MYSQL_DATABASE=test
    - MYSQL_USER=belter
    - MYSQL_PASSWORD=belter_2017
    - MYSQL_ROOT_PASSWORD=123456_abc
    ports:
    - '3302:3306'

    web:
    build: ./orca/
    restart: always
    command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    container_name: web
    volumes:
    - ./orca:/code
    ports:
    - '8000:8000'
    depends_on:
    - db

    angular:
    build: ./ng-orca-new/
    container_name: angular
    ports:
    - '3000:80'


    Thanks in advance!










    share|improve this question

























      0












      0








      0








      I decided to dockerize an existing Django REST + Angular app today. The website is displaying as it should, however CORS requests are being blocked.




      Access to XMLHttpRequest at 'http://localhost:8000/branches/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.




      The angular App is dockerized using Nginx. I was using this tutorial in the progress (docker + compose files are listed below).



      Strange thing about this is that I had this problem before and solved it through the use of the djang-cors-headers package. Along with CORS_ORIGIN_ALLOW_ALL = True, this no longer seems to be solving the issue.



      Can this be related due to the fact that the application now runs in containers? I'm providing the dockerfiles for the projects along with the docker-compose file below. Not sure if they are relevant though.



      Dockerfile for the Angular project:



      FROM tiangolo/node-frontend:10 as build-stage
      WORKDIR /app

      COPY package*.json /app/
      RUN npm install

      COPY ./ /app/
      ARG configuration=production
      RUN npm run build -- --output-path=./dist/out --configuration $configuration

      # Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
      FROM nginx:1.15
      COPY --from=build-stage /app/dist/out/ /usr/share/nginx/html

      # Copy the default nginx.conf provided by tiangolo/node-frontend
      COPY --from=build-stage /nginx.conf /etc/nginx/conf.d/default.confenter


      Dockerfile for the Django REST project:



      # Pull base image
      FROM python:3.7

      # Set environment varibles
      ENV PYTHONDONTWRITEBYTECODE 1
      ENV PYTHONUNBUFFERED 1

      # create root directory
      RUN mkdir /web

      # set working directory
      WORKDIR /web

      # Coppy current directory contents into the container
      ADD . /web/

      # Install any needed packages in requirements.txt
      RUN pip install -r requirements.txt


      And the docker-compose.yml file:



      version: '3'

      services:
      db:
      image: mysql:5.7
      restart: always
      container_name: db
      environment:
      - MYSQL_HOST=localhost
      - MYSQL_PORT=3306 # cannot change this port to other number
      - MYSQL_ROOT_HOST=%
      - MYSQL_DATABASE=test
      - MYSQL_USER=belter
      - MYSQL_PASSWORD=belter_2017
      - MYSQL_ROOT_PASSWORD=123456_abc
      ports:
      - '3302:3306'

      web:
      build: ./orca/
      restart: always
      command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
      container_name: web
      volumes:
      - ./orca:/code
      ports:
      - '8000:8000'
      depends_on:
      - db

      angular:
      build: ./ng-orca-new/
      container_name: angular
      ports:
      - '3000:80'


      Thanks in advance!










      share|improve this question














      I decided to dockerize an existing Django REST + Angular app today. The website is displaying as it should, however CORS requests are being blocked.




      Access to XMLHttpRequest at 'http://localhost:8000/branches/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.




      The angular App is dockerized using Nginx. I was using this tutorial in the progress (docker + compose files are listed below).



      Strange thing about this is that I had this problem before and solved it through the use of the djang-cors-headers package. Along with CORS_ORIGIN_ALLOW_ALL = True, this no longer seems to be solving the issue.



      Can this be related due to the fact that the application now runs in containers? I'm providing the dockerfiles for the projects along with the docker-compose file below. Not sure if they are relevant though.



      Dockerfile for the Angular project:



      FROM tiangolo/node-frontend:10 as build-stage
      WORKDIR /app

      COPY package*.json /app/
      RUN npm install

      COPY ./ /app/
      ARG configuration=production
      RUN npm run build -- --output-path=./dist/out --configuration $configuration

      # Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
      FROM nginx:1.15
      COPY --from=build-stage /app/dist/out/ /usr/share/nginx/html

      # Copy the default nginx.conf provided by tiangolo/node-frontend
      COPY --from=build-stage /nginx.conf /etc/nginx/conf.d/default.confenter


      Dockerfile for the Django REST project:



      # Pull base image
      FROM python:3.7

      # Set environment varibles
      ENV PYTHONDONTWRITEBYTECODE 1
      ENV PYTHONUNBUFFERED 1

      # create root directory
      RUN mkdir /web

      # set working directory
      WORKDIR /web

      # Coppy current directory contents into the container
      ADD . /web/

      # Install any needed packages in requirements.txt
      RUN pip install -r requirements.txt


      And the docker-compose.yml file:



      version: '3'

      services:
      db:
      image: mysql:5.7
      restart: always
      container_name: db
      environment:
      - MYSQL_HOST=localhost
      - MYSQL_PORT=3306 # cannot change this port to other number
      - MYSQL_ROOT_HOST=%
      - MYSQL_DATABASE=test
      - MYSQL_USER=belter
      - MYSQL_PASSWORD=belter_2017
      - MYSQL_ROOT_PASSWORD=123456_abc
      ports:
      - '3302:3306'

      web:
      build: ./orca/
      restart: always
      command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
      container_name: web
      volumes:
      - ./orca:/code
      ports:
      - '8000:8000'
      depends_on:
      - db

      angular:
      build: ./ng-orca-new/
      container_name: angular
      ports:
      - '3000:80'


      Thanks in advance!







      django angular docker django-rest-framework django-cors-headers






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 23:21









      IsoIso

      16610




      16610
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Problem was due to nginx blocking the requests. Added the headers once again at that layer, yielding the following nginx.conf:



          server {
          listen 80;
          location /
          add_header "Access-Control-Allow-Origin" "*";
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
          add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

          root /usr/share/nginx/html;
          index index.html index.htm;
          try_files $uri $uri/ /index.html =404;
          }
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403103%2fcors-blocked-in-dockerized-django-rest-angular-project%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Problem was due to nginx blocking the requests. Added the headers once again at that layer, yielding the following nginx.conf:



            server {
            listen 80;
            location /
            add_header "Access-Control-Allow-Origin" "*";
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
            add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

            root /usr/share/nginx/html;
            index index.html index.htm;
            try_files $uri $uri/ /index.html =404;
            }
            }





            share|improve this answer




























              0














              Problem was due to nginx blocking the requests. Added the headers once again at that layer, yielding the following nginx.conf:



              server {
              listen 80;
              location /
              add_header "Access-Control-Allow-Origin" "*";
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
              add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

              root /usr/share/nginx/html;
              index index.html index.htm;
              try_files $uri $uri/ /index.html =404;
              }
              }





              share|improve this answer


























                0












                0








                0







                Problem was due to nginx blocking the requests. Added the headers once again at that layer, yielding the following nginx.conf:



                server {
                listen 80;
                location /
                add_header "Access-Control-Allow-Origin" "*";
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
                add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

                root /usr/share/nginx/html;
                index index.html index.htm;
                try_files $uri $uri/ /index.html =404;
                }
                }





                share|improve this answer













                Problem was due to nginx blocking the requests. Added the headers once again at that layer, yielding the following nginx.conf:



                server {
                listen 80;
                location /
                add_header "Access-Control-Allow-Origin" "*";
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
                add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

                root /usr/share/nginx/html;
                index index.html index.htm;
                try_files $uri $uri/ /index.html =404;
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 23:48









                IsoIso

                16610




                16610
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403103%2fcors-blocked-in-dockerized-django-rest-angular-project%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()