Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update code from Git to a Docker container

I have a Docker file trying to deploy Django code to a container

FROM ubuntu:latest MAINTAINER { myname }  #RUN echo "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc) main universe" >> /etc/apt/sou$  RUN apt-get update  RUN DEBIAN_FRONTEND=noninteractive apt-get install -y tar git curl dialog wget net-tools nano buil$ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python python-dev python-distribute python-p$  RUN mkdir /opt/app WORKDIR /opt/app  #Pull Code RUN git clone [email protected]/{user}/{repo}  RUN pip install -r website/requirements.txt  #EXPOSE = ["8000"] CMD python website/manage.py runserver 0.0.0.0:8000 

And then I build my code as docker build -t dockerhubaccount/demo:v1 ., and this pulls my code from Bitbucket to the container. I run it as docker run -p 8000:8080 -td felixcheruiyot/demo:v1 and things appear to work fine.

Now I want to update the code i.e since I used git clone ..., I have this confusion:

  • How can I update my code when I have new commits and upon Docker containers build it ships with the new code (note: when I run build it does not fetch it because of cache).
  • What is the best workflow for this kind of approach?
like image 943
Cheruiyot Felix Avatar asked Dec 17 '14 15:12

Cheruiyot Felix


People also ask

Can I use Git in a Docker container?

Even if you are running your project on Docker, you can still access your git account inside Docker Containers. All you need to do is just install Git inside your Docker Container.

Can Docker pull from GitHub?

You can use the docker pull command to install a docker image from GitHub Packages, replacing OWNER with the name of the user or organization account that owns the repository, REPOSITORY with the name of the repository containing your project, IMAGE_NAME with name of the package or image, HOSTNAME with the host name of ...


1 Answers

There are a couple of approaches you can use.

  1. You can use docker build --no-cache to avoid using the cache of the Git clone.
  2. The startup command calls git pull. So instead of running python manage.py, you'd have something like CMD cd /repo && git pull && python manage.py or use a start script if things are more complex.

I tend to prefer 2. You can also run a cron job to update the code in your container, but that's a little more work and goes somewhat against the Docker philosophy.

like image 76
seanmcl Avatar answered Sep 23 '22 08:09

seanmcl