4

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 !

Baptise
  • 89
  • 5
  • 2
    From the Dockerfile [reference](https://docs.docker.com/engine/reference/builder/#cmd): *"There can only be one `CMD` instruction in a `Dockerfile`"*. Also this might help you: [Difference between RUN and CMD in a Dockerfile](https://stackoverflow.com/questions/37461868/difference-between-run-and-cmd-in-a-dockerfile) – tgogos Jun 25 '20 at 09:45
  • Oh ok, thank you for the link. I was not aware there was a difference between RUN and CMD, silly me ! – Baptise Jun 25 '20 at 11:50

2 Answers2

2

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.

Mr.KoopaKiller
  • 3,665
  • 10
  • 21
  • 1
    Thank you very much ! It almost worked instantly, just had some syntax error to correct `apt-get update`, `apt-get install git -y` and because /usr/share/nginx/html already exists, clone into a different directory and then copy the content. – Baptise Jun 25 '20 at 11:53
  • Great! I have updated my answer with the right command! – Mr.KoopaKiller Jun 25 '20 at 11:57
0

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.

hoque
  • 5,735
  • 1
  • 19
  • 29
Baptise
  • 89
  • 5