NYITCOMATG's avatar

Images in /Public and CSS/JS files not loaded (404)

My application runs fine locally, all images in /Public folder and CSS/JS are loaded correctly. Once dockerized, all assets cannot be loaded. When I exec into the container, the build folder is there, the images are there and everything is where its supposed to be. My apache configuration is correct and root is set to /var/www/html/public.

0 likes
1 reply
LaryAI's avatar
Level 58

When dealing with assets not loading in a Dockerized environment, there are a few common issues to check. Here's a step-by-step solution to troubleshoot and resolve the problem:

  1. Check Dockerfile and Docker Compose Configuration: Ensure that your Dockerfile and Docker Compose (if used) are correctly set up to copy the necessary files into the container.

    Example Dockerfile snippet:

    FROM php:8.0-apache
    COPY . /var/www/html
    WORKDIR /var/www/html
    

    If using Docker Compose, ensure the volumes are correctly mapped:

    services:
      web:
        build: .
        volumes:
          - .:/var/www/html
    
  2. Verify Apache Configuration: Double-check your Apache configuration to ensure the DocumentRoot is correctly set to /var/www/html/public.

    Example Apache configuration:

    <VirtualHost *:80>
        DocumentRoot /var/www/html/public
        <Directory /var/www/html/public>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    
  3. File Permissions: Ensure that the file permissions are set correctly so that the Apache user inside the container can read the files.

    You can set permissions using:

    RUN chown -R www-data:www-data /var/www/html
    
  4. Asset URLs: Verify that the asset URLs in your HTML or templates are correct. They should be relative to the /public directory.

    Example:

    <link rel="stylesheet" href="/css/style.css">
    <script src="/js/app.js"></script>
    <img src="/images/logo.png" alt="Logo">
    
  5. Check for .htaccess Issues: If you are using .htaccess files, ensure they are correctly configured and not causing any redirects or blocking access to your assets.

  6. Docker Network and Port Configuration: Ensure that your Docker container is correctly exposing the necessary ports and that you are accessing the correct port from your host machine.

    Example Docker Compose port mapping:

    ports:
      - "8080:80"
    
  7. Clear Browser Cache: Sometimes, the browser cache might cause issues. Clear your browser cache or try accessing the site in incognito mode.

By following these steps, you should be able to identify and resolve the issue with your assets not loading in the Dockerized environment. If the problem persists, consider checking the Docker logs for any errors or additional clues.

Please or to participate in this conversation.