Localhost sent an invalid response[DOCKER SOLVED]

child process writing to socket - localhost sent an invalid response -  Stack Overflow

Introduction

If you've Dockerized your Vue or React app and followed the manuals correctly but are still encountering errors, this article will help you troubleshoot the issues. Specifically, it addresses the case where you have set up the app to run on port 8080 but cannot access it via localhost:8080.

Problem

Despite mapping your ports correctly in the docker-compose.yml file and exposing port 8080 from inside the container, you may find that your host localhost:8080 cannot open the app, as it constantly hits containers on 0.0.0.0:8080.

Cause

This issue may occur when the app is served inside the container on port 8080 but with the host as localhost. In this case, the localhost:8080 address is causing the problem.

Solution

To resolve this issue, you need to force your app to be served on 0.0.0.0:8080 in your package.json file.

For Vue Apps

In the case of a Vue app, modify the scripts section of the package.json file as shown below:

Before:

jsonCopy code"scripts": {
  "serve": "vue-cli-service serve",
  "build": "vue-cli-service build",
  "test:unit": "vue-cli-service test:unit",
  "lint": "vue-cli-service lint"
},

After:

jsonCopy code"scripts": {
  "serve": "vue-cli-service serve --host=0.0.0.0",
  "build": "vue-cli-service build",
  "test:unit": "vue-cli-service test:unit",
  "lint": "vue-cli-service lint"
},

The only change made is the addition of the --host=0.0.0.0 flag in the serve script. This change ensures that your app will be served on 0.0.0.0:8080 instead of localhost:8080, allowing you to access it without any issues.

For React Apps

For React apps, the solution is similar. Update the scripts section of your package.json file to include the --host flag with the correct IP address:

Before:

jsonCopy code"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},

After:

jsonCopy code"scripts": {
  "start": "react-scripts start --host=0.0.0.0",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},

By updating the start script with the --host=0.0.0.0 flag, you can serve your React app on the correct host and access it without any issues.

Conclusion

By serving your Dockerized Vue or React app on 0.0.0.0:8080 instead of localhost:8080, you can successfully access it from your host. Ensure that you've correctly updated the scripts section in your package.json file to include the --host=0.0.0.0 flag for your app to run without any errors.