Created flask application and deploying on gunicorn server on docker.
I want to write shell script to create linux service inside docker container, so I can start, stop and restart my flask app inside container.
Dockerfile:
FROM python
COPY ./app /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 9003
CMD ["gunicorn", "-b", "0.0.0.0:9003", "main:app"]
Build it and run using command : docker run -p 9003:9003 myapp
.
What I have tried opened docker container cli: docker exec -it <container_id> bash
. You can find container id by command docker ps
.
Step 1: changed directory to /etc/init.d and created myservice.service file.
[Unit]
Description=Gunicorn instance to serve app
[Service]
WorkingDirectory=/home/app
ExecStart=gunicorn --workers 3 --bind 0.0.0.0:9003 unix:app.sock -m 007 main:app
Restart=always
[Install]
WantedBy=multi-user.target
step 3: I enable service by first running cd /etc/rc3.d
and then run ln -s ../init.d/{SERVICENAME} S95{SERVICENAME}
step 4: Give permission : chmod +x /etc/init.d/{SERVICENAME}
step 5: start service : `service myservice start
But this give me error
enter image description here
Docker container OS: Debian 10
Why its giving me Unit not found? any idea? How to resolve?
2
Answers
Thanks for answer.
Using Window 10
please see references and way what I was doing. Created myproject.service in /etc/systemd/system inside docker container.
this newly created service is not catched and when I try to start this service using command
service myproject start
, It gives errorunrecognized service: myproject
.How will OS know that new service has been created and to reload it.
In Linux, by running daemon-reload, It catches all new created services. But In case of docker, how to do this?
Reference: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04
For rc script, Reference: https://unix.stackexchange.com/questions/106656/how-do-services-in-debian-work-and-how-can-i-manage-them
You are improperly creating the file in wrong directory and creating unrelated links. You seem to be mixing rc-scripts with systemd. They are not alike, they are completely different. Toss aside rc-scripts and forget
/etc/rc.d
ever existed. Re-read and follow a good tutorial on how to add a systemd service file to your system.This will make no difference, as the container you are running does not run systemd – you run only
CMD ["gunicorn"...
, so your service will never be running. Typically in docker, multiple services are run with a shell script or in more complicated cases with a supervisord. In your case, there is no need to setup anything – just let the docker run the service as its pid 0 process, it’s only one process. Re-research how docker works and research how services are written in docker.What you may want to create is a service file on host, not inside docker container.