1

When I deploy a JSS app to a Sitecore instance running in a Docker Container and run our applications in Integrated Mode, our application is accessible with a hostname and port number (e.g. http://jss.sc9.local:44001/). However, the page that Sitecore renders contains image links that are missing the port number and result in HTTP 404 errors (e.g. the link Sitecore renders in the page is
http://jss.sc9.local/-/jssmedia/jss-helix-react-ts-starter/data/media/img/sc_logo.ashx?h=51&iar=0&w=204&hash=D53403E7F32EFFCEAB6C3FBC8E88E034
instead of
http://jss.sc9.local:44001/-/jssmedia/jss-helix-react-ts-starter/data/media/img/sc_logo.ashx?h=51&iar=0&w=204&hash=D53403E7F32EFFCEAB6C3FBC8E88E034).

How do we get Sitecore to render the pages with the port number in the image URLs? Is it related to this problem?

This happens with our own application as well as with this starter application. Both are React applications. I tried adding the port number to the hostname attribute of the site element of the sitecore/config/*.config as shown below, but that breaks Sitecore's URL application resolution (e.g. going to http://jss.sc9.local:44001 gives you the generic Sitecore page instead of our application).
enter image description here

Thanks in advance!

1 Answer 1

0

Using a port (ie. 44001) to browse the instance in your running container makes sense if you use the localhost domain. Each running Docker container has its own IP address that gets assigned by the pool of the network that it is connected to. If you have a specific domain used only by a container, you just need to tell your host machine which IP address your domain needs to resolve to.

You can achieve this using one of the following approaches:

  1. Manually retrieving the container IP address and then manually adding a record in the hosts file (C:\Windows\System32\drivers\etc\hosts) of your host machine. You can find the IP address of a running container, running the docker inspect <container_id or container_name> command in Powershell. Keep in mind that the IP address of a running container is not always the same and it might be different next time you run your container.

  2. Using and configuring the Windows Hosts Writer service implemented by Rob Ahnemann, that takes care of automatically monitoring the running containers and updating the hosts file on your host machine using container names and aliases. In addition on adding this service container in your docker-compose file, you will need to configure the needed alias for your container. For example:

    networks:
      default:
        aliases:
          - jss.sc9.local

Browsing your instance without using a port with one of these approaches should solve your issue.

9
  • Thanks Alessandro! However, please correct me if I'm wrong, but the hosts file only allows for DNS hostname resolution so it won't be able to redirect a hostname in a URL to a specific IP and port. For example, I already have this in my hosts file (and I tried it with the IP returned by docker inspect as you suggested) 127.0.0.1 jss.sc9.local but the following is illegal (the browser just returns an error since it can't find a page) 127.0.0.1:44001 jss.sc9.local Commented Sep 8, 2020 at 14:50
  • Similarly, modifying the alias in the docker-compose doesn't allow for aliasing the IP with the port number. I gave that a try too just in case, and it led to the same web browser error. Commented Sep 8, 2020 at 15:00
  • @KingTwinkie The IP address of the running container should not be your localhost IP (127.0.0.1). When you run docker inspect <container_name>, the IP address of your container will be under NetworkSettings.Networks.nat.IPAddress (if you are using the nat network). Commented Sep 8, 2020 at 15:26
  • Once you have the correct hosts file record, you will be able to browse your instance without using a port. The docker engine takes care of forwarding your incoming request to the open port. Commented Sep 8, 2020 at 15:29
  • Thanks Alessandro! I got it working using your hosts suggestion. The part I didn't understand earlier is that the containers had their own IP, I thought it was only accessible through the port forwarding from localhost. Also, you have to let your host file change sit there for a few minutes...probably some routing tables need to get updated. Yea, the IP address returned by docker inspect was slightly different in our config as it's at NetworkSettings.Networks.<name of network adapter for services>.IPAddress. Commented Sep 9, 2020 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.