Changing Strapi port on docker-compose install

System Information
  • Strapi Version: 4.0.7
  • Host Operating System: Ubuntu 20.04
  • Docker Image: node:16
  • Database: postgres
  • Image Node Version: 16
  • Image NPM Version: 8.3.1
  • Yarn Version: 1.22.17


I created a strapi project using the CLI, directly from my host machine. After, I decided to dockerize my installation, so I created this docker-compose.yml file

version: "3.9"
        image: postgres
        restart: always
            - POSTGRES_USER
            - POSTGRES_DB
            - ./postgres_data:/var/lib/postgresql/data
        build: ./back/
            - db
            - POSTGRES_USER
            - POSTGRES_DB
            - HOST=${BACK_HOST}
            - PORT=${BACK_PORT}
            - APP_KEYS=${BACK_APP_KEYS}
            - "${BACK_PORT}:${BACK_PORT}"
            - ./back:/usr/src/app/api
        build: ./front/
            - db
            API_URL: "http://back:${BACK_PORT:-1337}/api"
            - "${FRONT_PORT:-3000}:3000"
            - ./front:/usr/src/app/front

Strapi is running on the back container. As we can see, I added the same port on host and image, cause when I tried to only change the host machine port, strapi admin was making calls to the image port, and it blocked the whole page. My problem is, even with changing the port on env variable, image and host, strapi admin continue to call on port 1337. I removed the .env because I exported all variables in the docker-compose file.

Here is my Dockerfile:

FROM node:16

WORKDIR /usr/src/app/api

RUN yarn

CMD ["yarn", "develop"]

So my question is, why does Strapi Admin force the usage of 1337 port even if I change PORT environment variable (and I can confirm the variable is detected, cause when strapi start, it gives me the good URL to access to admin, and if I try to access http://localhost:custom-port, the homepage load) ?

Hope someone will be able to help.

Thanks !

Hi @navalex , if you haven’t got the answer yet, to do custom port. You will need to modify strapi setting in config/server.js.


module.exports = ({ env }) => ({
  host: env('HOST', ''),
  port:'PORT', env('STRAPI_PORT')),
  app: {
    keys: env.array('APP_KEYS'),

In .env, you declare STRAPI_PORT=<custom-port>

Or you can hard-coded the custom port to server.js.

Hope that helps!

The default Strapi config/server.js already allows the default port to be overridden by setting up the ‘PORT’ environment variable.

module.exports = ({ env }) => ({
  host: env('HOST', ''),
  port:'PORT', 1337),

Setting the environment variable ‘PORT=1338’ will make Strapi start listening on port 1338 instead of 1337. But …

Somewhere deep inside the code port 1337 is hardcoded so that even if Strapi starts up on port 1338 you can’t get further than the login page because the next step is call to http://localhost:1337/admin/project-type and after that to http://localhost:1337/admin/init

This looks like a bug in Strapi itself. It means I can’t even map ports for my docker container to something other than 1337, because /admin/project-type and admin/init is always going to be requested on port 1337.

You also need to yarn build after any changes to config/server how ever, I have not been able to get the docker image build with the admin pointing to the custom port. Even tho I added ENV PORT=1339 to my Dockerfile. So far what I had to do, is rebuild the admin after the service is running docker compose exec strapi bash and then PORT=1339 yarn build