IBM Cloud Code Engine: How to Specify a Port in a PHP Apache Image

1 min read

Learn how to specify the port that your PHP application uses.

IBM Cloud Code Engine is a fully managed, serverless platform that runs your containerized workloads, including web apps, microservices, event-driven functions or batch jobs. The Code Engine experience is designed so that you can focus on writing code and not on the infrastructure that is needed to host it.

When you choose PHP Apache as a base image, your docker container will expose port 80 by default. This is the official PHP image on Dockerhub

Specifying a different port (such as 8080) using an EXPOSE statement in your Dockerfile does not work. In this blog post, I will share how to specify the port that your PHP application uses.

Prerequisites

  • IBM Cloud account and access to deploy apps in IBM Cloud Code Engine
  • Basic understanding of how to write a Dockerfile
  • Deploying a code using php:X.X-apache image

Current problem and symptom

Your Dockerfile probably looks like the following, in the simplest form:

FROM php:7.4-apache
COPY src/ /var/www/html/
EXPOSE 8080

Once you kick off build and deploy, you will see a new image successfully built and saved on your image repository, but deployment will fail due to timeout and the log will not show adequate information:

Once you kick off build and deploy, you will see a new image successfully built and saved on your image repository, but deployment will fail due to timeout and the log will not show adequate information:

Essentially, this problem is caused by the port mismatch. You would have defined whatever port you defined in the Dockerfile as a listening port on the Code Engine side. However, your PHP application is still using the default port 80, so Code Engine cannot find your application.

How to specify the port in your Dockerfile

In PHP Apache, the /etc/apache2/ports.conf configuration file stores the directives that determine the TCP ports Apache is listening on. The listen directive in the configuration file determines the port Apache will bind to and prioritise. So, let's overwrite it to 8080:

FROM php:7.4-apache
COPY src/ /var/www/html/
RUN sed -i "s/Listen 80/Listen 8080/" /etc/apache2/ports.conf 

Once you build and deploy again, it should work:

Once you build and deploy again, it should work:

Another workaround

Alternatively, you can make Code Engine to use the default port 80. Edit your revision setting and modify listening port:

Alternatively, you can make Code Engine to use the default port 80. Edit your revision setting and modify listening port:

Having a trouble deploying an app? 

Please feel free to contact IBM client support for any questions or concerns. 

Be the first to hear about news, product updates, and innovation from IBM Cloud