← Back to Dispatch Articles
Engineering

Why Coolify's Dockerfile-based deploys break if you don't set PORT env var

The direct answer is that Coolify's Dockerfile-based deployments require explicit PORT environment variable settings because the platform uses this value to.

By Deployxa Editorial Published Updated

Why Coolify's Dockerfile-based deploys break if you don't set PORT env var

Key Facts

  • Direct answer: The direct answer is that Coolify's Dockerfile-based deployments require explicit PORT environment variable settings because the platform uses this value to automatically configure reverse proxy routing and port forwarding.

  • What the error/limitation actually means: When you deploy an application using a Dockerfile in Coolify, the platform expects to know which port your application listens on for incoming traffic.

  • When you'll hit it: This limitation will affect any Dockerfile-based deployment in Coolify where the PORT environment variable is not explicitly set.

  • How to verify if it applies to you: To determine if this issue affects your deployment, check your Coolify application's environment variables.

The challenge of containerized application deployment often lies in the subtle details that can cause entire pipelines to fail. For developers using Coolify to deploy applications via Dockerfiles, one such critical detail is the PORT environment variable. When overlooked, this seemingly minor configuration can lead to deployment failures that leave applications inaccessible despite appearing to deploy successfully.

The direct answer is that Coolify's Dockerfile-based deployments require explicit PORT environment variable settings because the platform uses this value to automatically configure reverse proxy routing and port forwarding. Without it, Coolify cannot properly route traffic to your application, resulting in a deployed container that appears to be running but is actually unreachable from the internet.

What the error/limitation actually means

When you deploy an application using a Dockerfile in Coolify, the platform expects to know which port your application listens on for incoming traffic. This information is typically provided through the PORT environment variable. Coolify's deployment system uses this value to configure its reverse proxy, which sits between your application and the internet. The reverse proxy needs to know the internal port where your containerized application is listening so it can forward external requests to the correct location inside the container.

Without the PORT environment variable set, Coolify cannot determine the correct port for routing traffic. This doesn't prevent your Docker container from starting (assuming your application has a default port it listens on), but it does prevent the reverse proxy from forwarding external traffic to your application. The result is a container that appears healthy in Coolify's dashboard but is essentially isolated from external access, making your application effectively unavailable to users.

When you'll hit it

This limitation will affect any Dockerfile-based deployment in Coolify where the PORT environment variable is not explicitly set. This includes applications that have a default listening port but don't explicitly declare it through an environment variable. You might encounter this issue when deploying applications that were developed for other platforms or when working with existing projects that don't follow Coolify's specific deployment requirements.

For example, if you're deploying a Node.js application that typically listens on port 3000 by default but doesn't have a PORT environment variable in its configuration, Coolify won't know to route traffic to port 3000. Similarly, Python Flask applications that default to port 5000 or Go applications that default to port 8080 will face the same issue if the PORT variable isn't set. The problem becomes particularly evident when you deploy your application and see it marked as "running" in Coolify's dashboard but can't access it via the provided URL.

How to verify if it applies to you

To determine if this issue affects your deployment, check your Coolify application's environment variables. Navigate to your application in the Coolify dashboard, go to the "Environment Variables" section, and verify that a PORT variable is present with the correct port number your application uses. If it's missing, this is likely the cause of your deployment issues.

You can also verify by checking the container logs in Coolify. If you see entries indicating that the container started successfully but no incoming traffic is being logged, this suggests that while the container is running, the reverse proxy isn't forwarding traffic to it. Additionally, you can use the docker ps command in Coolify's terminal to inspect the container and check its port mappings. If you notice that there are no published ports (indicated by the PORTS column being empty), this confirms that Coolify isn't properly configuring port forwarding due to the missing PORT environment variable.

Your options

  • Set PORT environment variable: Add the PORT environment variable to your Coolify application configuration with the correct port number your application listens on.

  • Modify your Dockerfile: Update your Dockerfile to explicitly set the PORT environment variable using the ENV instruction.

  • Use a docker-compose file: Instead of a Dockerfile, use a docker-compose.yml file where you can explicitly define the ports and environment variables.

  • Deployxa: Consider using a platform like Deployxa that provides more flexible port configuration options without requiring explicit environment variable settings.

Common Pitfalls and Troubleshooting

The first pitfall is assuming that your application's default port will be automatically recognized by Coolify. Many developers expect that since their application works locally with a default port, it will work the same way in Coolify. However, Coolify requires explicit port declaration through environment variables for proper routing. To fix this, always explicitly set the PORT environment variable in Coolify with the correct port number.

The second pitfall is setting the PORT environment variable incorrectly. Sometimes developers might set it to the external port they want to access rather than the internal port their application listens on. Remember that the PORT environment variable should always match the internal port your application uses inside the container, not the external port that Coolify exposes. To fix this, verify that the PORT value matches what your application expects internally.

The third pitfall is forgetting that some frameworks require additional configuration beyond just setting the PORT variable. For example, some Node.js applications might need both PORT and HOST variables set, or Python applications might need FLASK_ENV and FLASK_APP variables in addition to PORT. To fix this, check your framework's documentation for all required environment variables and ensure they're all properly configured in Coolify.

The fourth pitfall is not restarting the application after adding or modifying environment variables. Coolify doesn't automatically restart containers when environment variables are changed. To fix this, after adding or updating the PORT environment variable, explicitly restart your application from the Coolify dashboard to ensure the changes take effect.

The fifth pitfall is confusing the PORT environment variable with port mappings in the Dockerfile. These are separate concerns - the Dockerfile's EXPOSE instruction declares which ports the container will listen on, while the PORT environment variable tells Coolify which of those ports to route external traffic to. To fix this, ensure both are properly configured if needed, but remember that Coolify primarily uses the PORT environment variable for routing, not the EXPOSE instruction.

Conclusion

Understanding the relationship between environment variables and Coolify's deployment process is crucial for successful Dockerfile-based deployments. The PORT environment variable serves as a critical piece of information that enables Coolify's reverse proxy to properly route traffic to your application. By ensuring this variable is correctly set, you can avoid the frustrating situation of having a deployed but inaccessible application.

For developers who frequently encounter this issue or prefer a more flexible deployment environment, exploring alternative platforms like Deployxa might be worthwhile. Regardless of your choice, always remember to verify your environment variables and check your container logs when facing deployment issues. Proper configuration is the key to seamless containerized application deployments.

Ready to deploy with Deployxa?

Deploy your apps globally with automatic SSL and AI diagnostics.

Start Free Now