Docker is getting stopped after creating the image

When I am building the image from this dockerfile the image is getting built but the container is not getting started I tried to check logs I didn’t find any informative
Here is the docker file

FROM php:7.4 as cron
RUN apt-get update && apt-get install -y cron
#Run the echo
RUN echo "* * * * * root php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1" >> /etc/crontab
USER root
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.12/supercronic-linux-amd64 \
    SUPERCRONIC=supercronic-linux-amd64 \
    SUPERCRONIC_SHA1SUM=048b95b48b708983effb2e5c935a1ef8483d9e3e
RUN curl -fsSLO "$SUPERCRONIC_URL" \
 && echo "${SUPERCRONIC_SHA1SUM}  ${SUPERCRONIC}" | sha1sum -c - \
 && chmod +x "$SUPERCRONIC" \
 && mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
 && ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
#Create the log file to be able to run taild
RUN touch /var/log/cron.log
EXPOSE 9000
CMD bash -c "cron && php-fpm"

Here is the Inspect of Container

 docker inspect cron
[
    {
        "Id": "54ad1fc4a8f2e5be65ffad5084cb6f1da2db5255f6211f6302cd06ebb8915931",
        "Created": "2022-01-25T08:04:20.065641695Z",
        "Path": "docker-php-entrypoint",
        "Args": [
            "/bin/sh",
            "-c",
            "bash -c \"cron && php\""
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-25T08:04:20.817610984Z",
            "FinishedAt": "2022-01-25T08:04:20.95033829Z"
        },
        "Image": "sha256:3dd61d4ae8f4991451882c28be21843c5f8a3742cf7faa8c20f8fa680a9ffdef",
        "ResolvConfPath": "/var/lib/docker/containers/54ad1fc4a8f2e5be65ffad5084cb6f1da2db5255f6211f6302cd06ebb8915931/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/54ad1fc4a8f2e5be65ffad5084cb6f1da2db5255f6211f6302cd06ebb8915931/hostname",
        "HostsPath": "/var/lib/docker/containers/54ad1fc4a8f2e5be65ffad5084cb6f1da2db5255f6211f6302cd06ebb8915931/hosts",
        "LogPath": "/var/lib/docker/containers/54ad1fc4a8f2e5be65ffad5084cb6f1da2db5255f6211f6302cd06ebb8915931/54ad1fc4a8f2e5be65ffad5084cb6f1da2db5255f6211f6302cd06ebb8915931-json.log",
        "Name": "/cron",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/devops/Desktop/manas/public_html:/var/www/public_html:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "manas_manas-network",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d962aede310211a036c98df22dd6a835523bf0281cc8169f0541eab47aa5adec-init/diff:/var/lib/docker/overlay2/4c854b5c4204d084d52892e47d1f3234d23ebb74cfd12ee39f3b326be98d6583/diff:/var/lib/docker/overlay2/dbf9ab098782c99d35de82887ee23004f83984f348aff4f965ab7d0e211123d3/diff:/var/lib/docker/overlay2/a085dc25c6b777242861c1630e842c20bf3f7de01d556cb4d55209761478e08f/diff:/var/lib/docker/overlay2/0bf515800b5ef39f5de565dba17d1d3f06e9e4cb67991fc653f6101a2df5c36c/diff:/var/lib/docker/overlay2/9407a08845446d90604285c20222f7f814f63f2a1701bae2062ea0334925e1a7/diff:/var/lib/docker/overlay2/1b1133bad0c0f87c3b37dc73dac7fe12eff9e07739578086088d267fc98b4969/diff:/var/lib/docker/overlay2/66a4a4176ab19a57e7e1c78caa4364e908ecb0d38f5db67513812e7d07fc706e/diff:/var/lib/docker/overlay2/b30fda3e81492607b600b41a798be86876097e085936c6a5c7372e8eb6425349/diff:/var/lib/docker/overlay2/818268870f639651111fe7f3d3a5481d4de0edc9c6f34b9f64c6290fff40b07f/diff:/var/lib/docker/overlay2/d9b131a954e8449b30aadf95e3f9d48eecf1cb95b13de7b41b47d03fe410e28c/diff:/var/lib/docker/overlay2/dd5da1b51e4f16e72e2a914177030a983541939359099dea9b44668e93ed45b4/diff:/var/lib/docker/overlay2/2dcb838422d61a2bac37a9bd76c73d71448ffbbe58b9bf0f8380ee39f6fae41f/diff:/var/lib/docker/overlay2/e491672686906fda7602592c9dee0079970320e9c03e6f4492b739e1bb8e2ff2/diff",
                "MergedDir": "/var/lib/docker/overlay2/d962aede310211a036c98df22dd6a835523bf0281cc8169f0541eab47aa5adec/merged",
                "UpperDir": "/var/lib/docker/overlay2/d962aede310211a036c98df22dd6a835523bf0281cc8169f0541eab47aa5adec/diff",
                "WorkDir": "/var/lib/docker/overlay2/d962aede310211a036c98df22dd6a835523bf0281cc8169f0541eab47aa5adec/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/devops/Desktop/manas/public_html",
                "Destination": "/var/www/public_html",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "54ad1fc4a8f2",
            "Domainname": "",
            "User": "root",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "9000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_LDFLAGS=-Wl,-O1 -pie",
                "GPG_KEYS=42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312",
                "PHP_VERSION=7.4.27",
                "PHP_URL=https://www.php.net/distributions/php-7.4.27.tar.xz",
                "PHP_ASC_URL=https://www.php.net/distributions/php-7.4.27.tar.xz.asc",
                "PHP_SHA256=3f8b937310f155822752229c2c2feb8cc2621e25a728e7b94d0d74c128c43d0c",
                "SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.12/supercronic-linux-amd64",
                "SUPERCRONIC=supercronic-linux-amd64",
                "SUPERCRONIC_SHA1SUM=048b95b48b708983effb2e5c935a1ef8483d9e3e"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "bash -c \"cron && php\""
            ],
            "Image": "manas_cron",
            "Volumes": {
                "/var/www/public_html": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-php-entrypoint"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "ecf1880bebf07b65becbe0d7f4feab7cfaaea3ce484d7628ca1c16d1f01ce3a9",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "manas",
                "com.docker.compose.project.config_files": "docker-compose.yml",
                "com.docker.compose.project.working_dir": "/home/devops/Desktop/manas",
                "com.docker.compose.service": "cron",
                "com.docker.compose.version": "1.29.2"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "3a3f85e18d3be9d8484bc493fb9e5c21b105179434660bb4bf00eb77da939227",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/3a3f85e18d3b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "manas_manas-network": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "cron",
                        "54ad1fc4a8f2"
                    ],
                    "NetworkID": "bfa2edf94c717f1985dd382d1f808b17a8d16710b8ab066d83779acf109c68a4",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

I don’t know if you could solve it, but I don’t see any solution on StackOverflow. There are multiple problems here, but I see one reason why the container stops without error message.

  1. Your quoted Dockerfile is not what the image was created from. In the Dockerfile there is “php-fpm” and the inspect shows it was “php”. I guess php-fpm was just manually added on the forum by accident.

  2. You want to start multiple services in one container without any process manager. Use something like Supervisor or write a script where you can handle the linux signals for the cron in the background and for php in the foreground. I tried to explain here with examples: GitHub - itsziget/tutorial-linux-signals: Source code for a youtube tutorial about Linux signals

  3. You should not use a shell for PID 1. You can start from there and run exec php to let your php command to has PID 1 and the shell to disappear. You would still need to handle the signals (stop or restart) for cron

  4. Usually it is better to use the “exec” form instead of the “shell” form after CMD. It means you define your command as a json list: CMD ["bash", "-c", "cron && php"]
    Of yourse writing cron && php is still not recommneded, but in that case your final command would not be /bin/sh -c bash -c "cron && php" but bash -c "cron && php". The shell form is what you usually use after the RUN instruction, because you want to use a shell without complicating your command with backslashes.

  5. And finally, I think the problem is, that you probably try to run the container without interactive terminal. So if my guess is right, then you tried this:

    docker run --rm imagename
    # or
    docker run -d imagename
    

    instead of

    docker run --rm -it imagename
    # or
    docker run -d -it imagename
    

    So bash starts, and stops immediately stopping everything inside the container.
    But even if your container could run, you could not stop it with docker stop properly, so Docker would kill it 10 seconds after you ran docker stop.