Ubuntu + Nginx with autostart at boot time


Im very new to docker, however I’ve dived into it, I created my first python app.
I managed to create an image and then a run a container, however I’ve been wrestling to get nginx started automatically. So far I tried these, but none of those starts nginx at boot time:

FROM ubuntu

LABEL maintainer=“K Z zoleelikesdebian@gmail.com

RUN apt-get update -y
RUN apt-get install -y python3 python3-pip nginx mc nano net-tools
RUN pip3 install paramiko

COPY ./ ./app

COPY ./nginx.conf /etc/nginx/sites-enabled/default
CMD [“nginx”, “-g”, “daemon off;”]
#CMD [“nginx”, “-g”, “daemon on;”]
#CMD [ “service nginx start” ]

CMD [ “python3”, “./main.py” ]

Could you please enlighten me what am I doing wrong:-) Thanks

Every Dockerfile must run CMD one time. And I think you must create different a Dockerfile for python.

1 Like

Last CMD declartion wins. You will need to write an entypoint script and script your desired start process inhere. You have plenty of examples, If you browse Dockerhub descriptions - usualy they have a github link. Checkout their Dockerfile to see how they copy the entrypoint script into the container and declare it in CMD. Then check the entrypoint script to see how they do things.

A container is not “booting”. When a container starts, it will start the command defined in ENTRYPOINT (if the declaration exists) or CMD (if ENTRYPOINT is not defined). Those commands are usualy scripts - so called entrypoint scripts - that take care of preparation work, like render env variables into config files or start a service your main service depends on, and starts the main service.

1 Like

Yeap, I never knew that 2 Dockerfile can exist to create 1 image. I’ll check it out. Thanks

Thanks, in the meantime I figured it out: I created a bash script what starts nginx, and then starts my python app. And I gave my script to CMD :slight_smile:

yep, those type of scripts are called entrypoint scripts :wink: People do the wildest things in them…

1 Like