Baptise
Baptise

Reputation: 89

How to cleanly stop containers from restarting Kubernetes

I want to create a nginx container that copies the content of my local machine /home/git/html into container /usr/share/nginx/html. However I cannot use Volumes and Mountpath, as my kubernetes cluster has 2 nodes. I decided instead to copy the content from my github account. I then created this dockerfile:

FROM nginx
CMD ["apt", "get", "update"]
CMD ["apt", "get", "install", "git"]
CMD ["git", "clone", "https://github.com/Sonlis/kubernetes/html"]
CMD ["rm", "-r", "/usr/share/nginx/html"]
CMD ["cp", "-r", "html", "/usr/share/nginx/html"]

The dockerfile builds correctly, however when I apply a deployment with this image, the container keeps restarting. I know that once a docker has done its job, it shutdowns, and then the deployment restarts it, creating the loop. However when applying a basic nginx image it works fine. What would be the solution ? I saw solutions running a process indefinitely to keep the container alive, but I do not think it is a suitable solution.

Thanks !

Upvotes: 4

Views: 214

Answers (2)

Baptise
Baptise

Reputation: 89

Thanks to the help of @tgogos and @KoopaKiller, here's how my Dockerfile looks:

FROM nginx

RUN apt-get update && \ 
    apt-get install git -y

RUN git clone https://github.com/Sonlis/kubernetes.git temp

RUN rm -r /usr/share/nginx/html



RUN cp -r temp/html /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

And the kubernete pod keeps running.

Upvotes: 0

Mr.KoopaKiller
Mr.KoopaKiller

Reputation: 3962

You need to use RUN to perform commands when build a docker image, as mentioned in @tgogos comment. See the refer.

You can try something like that:

FROM nginx
RUN apt-get update && \
    apt-get install git
RUN rm -rf /usr/share/nginx/html && \
    git clone https://github.com/Sonlis/kubernetes/html /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

Also, I would like to recommend you to take a look in this part of documentation of how to optmize your image taking advantages of cache layer and multi-stages builds.

Upvotes: 2

Related Questions