Working in teams with Docker and github
1. Build new image before each container run
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)
pull repository from github
git pull
build a new image from dockerfile
docker build -t <<imagename>> .
remove the dangling image (the image you just have overwritten, and that is now named <none>)
docker rmi $(docker images -f 'dangling=true' -q)
run your container
docker run -it --rm -v ${PWD}:/docs <<imagename>>
Step by step (pushing)
in you container with the new requirements installed (pip install <<module>>) do a:
pip freeze > requirements.txt
push your code to gihub
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
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.
[ ]: