Working in teams with Docker and github

1. Build new image before each container run

In this first approach we build a new image with the requirements installed at build time.
From this image you can then run a container with the up to date requirements installed.
This means you will need to build a new docker image and run a container based on that image each time you do a pull from your groups github repository.

In your repository you will need your

  • code

  • a Dockerfile, and a

  • requirements.txt file

Your Docker file should look something like this:

Dockerfile

# the base image
FROM python:3.8-alpine

# copy all files from dir with Dockerfile and requirements.txt to /app folder in image
COPY . /app

# cd into /app folder with (in this case only) the requirements.txt
WORKDIR /app

# install python modules
RUN pip install -r requirements.txt

# Change into / as startingpoint of container
WORKDIR /

# when container runs it should start in a ash terminal
CMD ["ash"]

Your requirements.txt file should look something like this:

requirements.txt

certifi==2020.6.20
chardet==3.0.4
idna==2.10
requests==2.24.0
urllib3==1.25.10

Step by step (pulling)

  1. pull repository from github

    • git pull

  2. build a new image from dockerfile

    • docker build -t <<imagename>> .

  3. remove the dangling image (the image you just have overwritten, and that is now named <none>)

    • docker rmi $(docker images -f 'dangling=true' -q)

  4. run your container

    • docker run -it --rm -v ${PWD}:/docs <<imagename>>

Step by step (pushing)

  1. in you container with the new requirements installed (pip install <<module>>) do a:

    • pip freeze > requirements.txt

  2. push your code to gihub

  3. if or when you close your running container you will need to performe 2, 3, 4 from above

2. Install requirements in container at run

This second approach installs requirements in the container at container start instead of installing it in the image at build time.
This means that you only have to build the image once.

Your dockerfile has to change a bit.

Dockerfile

# the base image
FROM python:3.8-alpine

# copy all files from dir with Dockerfile and requirements.txt to /app folder in image
COPY . /app

# cd into /app folder with (in this case only) the requirements.txt
WORKDIR /app

# install python modules
RUN pip install -r requirements.txt

# Change into / as startingpoint of container
# WORKDIR /
# it is easiest to stay in the /app folder when you run the CMD below. (so thats why change to workdir is commented out.)

############################################
## Add this command. #######################
## This will run as the first thing when ###
## you run your container. #################

# install modules listed in requirements.txt, and start the ash terminal
CMD pip install -q -r requirements.txt & ash

Your docker run command will look like this:

docker run -it --rm -v ${PWD}:/app <<imagename>>

note: we use the /app folder instead of /docs

Which method is best?

Method number 2 are prefered during devolopment. This will eliminate the step of having to build a new image each time you need to run a container. On the other hand when the development phase is over, the first methods are prefered. This is because instalation of all requirements at container run is time consuming (even if there is no requirements updates) and it is unnessesary, since it can be done at image build instead.

[ ]: