To isolate more our project file from our system machine, there is an option named Docker. With Docker you are able to pack all your project is a system that you want and run it in any system machine. For example if you want Ubuntu 20.4 you can have it in a mac or windows machine or other operating systems.
To get started with Docker for the churn prediction project you can follow the instructions below.
sudo apt-get install docker.io
To run docker without sudo
, follow this instruction.
To install the Docker you can just follow the instruction by Andrew Lock in this link: https://andrewlock.net/installing-docker-desktop-for-windows/
Follow the steps in the Docker docs.
- Once our project was packed in a Docker container, we're able to run our project on any machine.
- First we have to make a Docker image. In Docker image file there are settings and dependecies we have in our project. To find Docker images that you need you can simply search the Docker website.
Here a Dockerfile (There should be no comments in Dockerfile, so remove the comments when you copy)
# First install the python 3.8, the slim version uses less space
FROM python:3.8.12-slim
# Install pipenv library in Docker
RUN pip install pipenv
# create a directory in Docker named app and we're using it as work directory
WORKDIR /app
# Copy the Pip files into our working derectory
COPY ["Pipfile", "Pipfile.lock", "./"]
# install the pipenv dependencies for the project and deploy them.
RUN pipenv install --deploy --system
# Copy any python files and the model we had to the working directory of Docker
COPY ["*.py", "churn-model.bin", "./"]
# We need to expose the 9696 port because we're not able to communicate with Docker outside it
EXPOSE 9696
# If we run the Docker image, we want our churn app to be running
ENTRYPOINT ["gunicorn", "--bind", "0.0.0.0:9696", "churn_serving:app"]
The flags --deploy
and --system
makes sure that we install the dependencies directly inside the Docker container without creating an additional virtual environment (which pipenv
does by default).
If we don't put the last line ENTRYPOINT
, we will be in a python shell.
Note that for the entrypoint, we put our commands in double quotes.
After creating the Dockerfile, we need to build it:
docker build -t churn-prediction .
To run it, execute the command below:
docker run -it -p 9696:9696 churn-prediction:latest
Flag explanations:
-t
: is used for specifying the tag name "churn-prediction".-it
: in order for Docker to allow us access to the terminal.--rm
: allows us to remove the image from the system after we're done.-p
: to map the 9696 port of the Docker to 9696 port of our machine. (first 9696 is the port number of our machine and the last one is Docker container port.)--entrypoint=bash
: After running Docker, we will now be able to communicate with the container using bash (as you would normally do with the Terminal). Default ispython
.
At last you've deployed your prediction app inside a Docker continer. Congratulations 🥳
The notes are written by the community. If you see an error here, please create a PR with a fix. |