I have this dockerfile whcih builds an image:
# syntax = docker/dockerfile:1.2
FROM node:16-alpine as builder
COPY package.json package-lock.json .npmrc ./
RUN npm install
COPY . .
RUN --mount=type=secret,id=auto-devops-build-secrets . /run/secrets/auto-devops-build-secrets && \
npm run build
COPY nginx.conf /opt/bitnami/nginx/conf/server_blocks/my_server_block.conf
COPY --from=builder dist /app
Then in portainer I have this docker-compose file where I want to run a command after the image is instantiated (command: “abc”).
The problem is that as soon as the command is ran the container exits.
I understood that the image does not support this but I can’t understand why and how to solve my problem.
I read a lot of documentation and probably I don’t fully understand how things are working (image startup, entrypoints, main process, etc…)
command: echo "abc"
Your command needs to run continuously in the foreground. Without a running process there is no container.
If you just need a test container running “forever” you can do something like this:
Thank you very much for your answer.
Why do you say “test container”?
What if I have the exact same configuration in production and I want to run that command?
Currently if I start the container without the command, it just starts and it serves my JS app without the container exiting. I asume this means that the nginx process keeps running in the foreground.
So how could I run my command and still have nginx running in the foreground?
Becuse you are not supposed to keep the container alive using only a sleep command in production. As In mentioned before the process in the container needs to run in the foreground and your NginX is that process.
A container is not a virtual machne. We usually run only one process in it. Without knowing what you actually want to achieve (because I don’t think that you just want to echo “abc”) I can’t tell you what you should do, but in most of the cases another process can run in another container. When it is not the case you can use s6-init or supervisord to run as a process manager similar to systemd