Search code examples
processpodmanrootless

podman - How to Start a Process in a Containerfile?


I have put a script with an endless loop inside a Containerfile.
When I go inside the container and run that script in the background I can see that the process is running by doing a ps -ef.
But when I try to start the process inside the Containerfile it is not running, even though the podman build and podman run commands are without error.

I am using rootless podman.

This is my Containerfile:

$ cat Containerfile 
FROM alpine
RUN apk update
RUN apk add vim
RUN apk add bash

COPY ./useless_process.sh /home
RUN bash /home/useless_process.sh &

# how to build:
# podman build . -t "manualpihimage"
# how to run:
# podman run -it --name "manualpihcontainer" manualpihimage

I have also tried using the CMD and the ENTRYPOINT commands but the process did not start.

The expectation was that the process would run in the background.


Solution

  • I have tried it with Containerfile as follows. Note that I removed the useless & - makes no sense in the context of the container and used CMD because we don't want to run it while building the image but when we start the container.

    FROM alpine
    RUN apk update
    RUN apk add vim
    RUN apk add bash
    
    COPY ./useless_process.sh /home
    CMD bash /home/useless_process.sh
    

    I created useless_process.sh with:

    #!/bin/sh
    while `/bin/true`; do
        date
        sleep 1
    done
    

    Then podman build . -t=image1 and podman run -d --name=container1 image1 to start it detached.

    $ podman ps -a
    CONTAINER ID  IMAGE                            COMMAND               CREATED         STATUS             PORTS                    NAMES
    0744f29bec7c  localhost/image1:latest          /bin/sh -c bash /...  22 seconds ago  Up 23 seconds ago                           container1
    

    And we can see our useless process is running

    $ podman exec -it container1 /bin/sh
    / # ps
    PID   USER     TIME  COMMAND
        1 root      0:00 bash /home/useless_process.sh
      188 root      0:00 /bin/sh
      197 root      0:00 sleep 1
      198 root      0:00 ps