Initialization script not executed when container is run in detached mode

I have a Dockerfile that builds a Django environment, which is based on an Ubuntu image and runs Apache HTTP Server. (httpd) Let’s call that image backend_image. Along with another container for the PostgreSQL database, I plan to use this image as the development environment, firing both containers up and using the --network flag to connect them.

I planned to have an initialization script within the backend_image, which creates the relevant tables whenever the backend container is run. The contents of the script is rather standard for a Django app, and are as follows.

#!/bin/bash
source ${VIRTUAL_ENV_DIR}/bin/activate
python ${SCRIPT_LOCATION} makemigrations library
python ${SCRIPT_LOCATION} migrate
deactivate

This script is run correctly when I run the backend_image in the foreground. (i.e. without the -d flag in the docker run command) I checked it by connecting to the database container using psql, and dt command printed the relevant databases.
However, in order to orchestrate the running of multiple containers required by the project, I need to run them in the detached mode. (I have a shell script running the containers, and connecting them) The problem is, when I run the backend container in detached mode, the tables are not created in the database container.

Below is the CMD of the Dockerfile of the backend_image.

CMD ["/bin/bash", "-c", "/tmp/initialization/initialize-db.sh && apachectl -D FOREGROUND"]

Like I said, if the container is run in the foreground, the script works and the relevant tables are created in the database container. Similarly, if I use the docker run command on the backend_image by overriding the CMD (i.e. specifying /bin/bash on the command line) and run the script manually from within the container, it is executed successfully. But somehow, when run in detached mode with -d flag, the script does not seem to create the tables in the PostgreSQL database. (as far as I can observe by connecting to the database container through psql and printing the relations by dt command)

Note that the apachectl command seems to be running, and the server (along with the Django app) seems accessible, even when the backend container is run in detached mode. The only issue seems to be that the relevant tables are not created in the database container. It seems like the initialization script is never run in detached mode, as if portion of the CMD command before the && is somehow ignored when the container is run in detached mode.

Is the problem with the initialization script? (or the way I execute it through CMD of the Dockerfile of backend_image) Or is there something I am missing about the detached mode, which keeps the script from being executed?

Source: StackOverflow