dockerurljekyll

Broken urls in Jekyll in Docker


I have this problem I have Jekyll site that runs inside docker and the urls are broken because it use http://0.0.0.0:4000 as site url.

I'm using this to run docker:

docker run --rm -ti -v $(pwd):/tmp/www -p 8080:4000 jcubic.pl

and it execute:

CMD jekyll serve --host 0.0.0.0

I can access http://localhost:8080 but all urls are broken.


Solution

  • The problem is that in Jekyll if you use development mode it will replace the {{ site.url }} with current URL taken from host and port so it will be http://0.0.0.0:4000.

    The solution found in this issue on GitHub:

    Use non-production enthronement, so your checks for production in code will not break (if you don't want to show something in development mode)

    then add _config_dockerl.yml with:

    url: ""
    

    and run this:

    # Dockerfile
    CMD jekyll serve --host 0.0.0.0 --config _config.yml,_config_docker.yml
    
    #bash
    docker run --rm -ti -v $(pwd):/tmp/www -e "JEKYLL_ENV=docker" -p 8080:4000 image
    

    That way {{ site.url }} will be empty string. It will only break in case where you need to pass real url with http (I had this with share buttons so I've enabled them only on production).