简体   繁体   中英

Docker running a command on container start up

I have a docker container, for which I need to run the following command

php /var/www/html/artisan queue:work &

It starts a worker process that looks for jobs and executes them.

I can run it by doing exec -it when the container is running.

But I need to do it using Dockerfile so that when my container re-deploys, it starts this automatically. I have tried

RUN php /var/www/html/artisan queue:work

CMD ["php","/var/www/html/artisan","queue:work"]

ENTRYPOINT ["php","/var/www/html/artisan","queue:work"]

separately of course. but none of them work. In the case of CMD and ENTRYPOINT my container starts giving out a 502 error and my service becomes inaccessible.

What am I doing wrong?

You could do this in multiple ways.

You could write a shell script that starts the background process first and then starts your API.

CMD ["./start_server.sh"]

Contents of ./start_server.sh

#!/bin/bash

php /var/www/html/artisan queue:work &

exec php-server-serving-api

You could also do this through a docker entrypoint shell script

ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["php-server-serving-api"]

Contents of ./docker-entrypoint.sh

#!/bin/bash

php /var/www/html/artisan queue:work &

exec $@

However, what I recommend is, if they are separate type of workloads, run them in separate container. If the background processing task crashes, there is no one to restart it. If you run it as a separate container, you could use a system to restart it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM