skip to Main Content

I have a docker instance running apache on port 80 and node.js+express running on port 3000. I need to make an AJAX request from the apache-served website to the node server running on port 3000.

I don’t know what is the appropiate url to use. I tried localhost but that resolved to the localhost of the client browsing the webpage (also the end user) instead of the localhost of the docker image.

Thanks in advance for your help!

2

Answers


  1. First you should split your containers – it is a good practice for Docker to have one container per one process.

    Then you will need some tool for orchestration of these containers. You can start with docker-compose as IMO the simplest one.

    It will launch all your containers and manage their network settings for you by default.

    So, imaging you have following docker-compose.yml file for launching your apps:

    docker-compose.yml

    version: '3'
    services:
      apache:
        image: apache
    
      node:
        image: node # or whatever
    

    With such simple configuration you will have host names in your network apache and node. So from inside you node application, you will see apache as apache host.

    Just launch it with docker-compose up

    Login or Signup to reply.
  2. make an AJAX request from the […] website to the node server

    The JavaScript, HTML, and CSS that Apache serves up is all read and interpreted by the browser, which may or may not be running on the same host as the servers. Once you’re at the browser level, code has no idea that Docker is involved with any of this.

    If you can get away with only sending links without hostnames <img src="/assets/foo.png"> that will always work without any configuration. Otherwise you need to use the DNS name or IP address of the host, in exactly the same way you would as if you were running the two services directly on the host without Docker.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search