Welcome to our workshop on deploying machine learning models using Docker, hosted by the AI Service Center in Berlin-Brandenburg. Visit our website for more details about our offerings!
We can follow this workshop fully through Gitpod's VSCode, no need to install software locally, but we also provide the instructions for a local installation as an option.
This guide provides instructions for installing and setting up all the tools and environments you need to participate in the workshop. Familiarity with opening and using a terminal is required. Here's how you can start a terminal:
- On Windows: Search for
cmd
orCommand Prompt
in the Start menu. - On Mac: Open the
Terminal
app from the Utilities folder. - On Linux: Use your distribution's default terminal.
- Purpose of Git: Git is a version control system essential for managing code changes and collaboration.
- How to Install: Download and install Git by following the instructions on Git Guides.
- Verify Installation: In the terminal, run
git --version
. The output should display the Git version. You may need to restart your terminal before this command becomes accessible.
Please install Docker before attending the workshop. Detailed instructions are provided below.
- Purpose of Docker: Docker is key for creating and running containerized applications, ensuring consistency across environments.
- How to Install: Follow the installation instructions based on your OS:
- Verify Installation: In the terminal, run
docker --version
. The output should display the Docker version. Restarting your computer may be necessary before this command becomes accessible.
- What's an IDE? An Integrated Development Environment (IDE) is a platform for coding, editing, and debugging your code. It also serves as a graphical user interface for Git.
- Recommended IDEs:
- Visual Studio Code - Versatile and lightweight.
- PyCharm Community Edition - Tailored for Python development.
- Verify Installation: You can verify your installation by cloning a GitHub repository. Please refer to the following guides:
If you encounter installation issues, consult the respective software's documentation or contact us at [email protected] for assistance.
Once you've installed Git, Docker, and an IDE, you're all set for our workshop. We're excited to introduce you to the world of MLOps and Docker!
- (Optional) Look for the
hello-world
Docker image on Docker hub: https://hub.docker.com/ - Download the Docker image using the command:
docker pull hello-world
- List your Docker images with the command:
docker image ls
- Create a container using the
hello-world
image by running:docker run hello-world
- List your Docker containers using:
docker ps --all
- Remove your Docker container with the command:
docker rm [ID]
- Create a self-deleting container with the
hello-world
image using:docker run --rm hello-world
- Open
model/model_trainer.ipynb
. - Run all cells in the Jupyter notebook.
- Explain the code to your neighbour to make sure you understand it.
- Try changing the input text in the last cell. Do the answers make sense?
Task: Fill out the TODOs in the app/api.py
file
- Read the FastAPI documentation: https://fastapi.tiangolo.com/tutorial/response-model/#response_model-parameter
- Read about the request body: https://fastapi.tiangolo.com/tutorial/body/
- You can test if your API is correctly defined py running
python3 app/api.py
and openinghttp://0.0.0.0:8000
in your browser.
- Create a file with the name Dockerfile
- Find Python version 3.11 on Docker Hub and use it as a Base Image:
FROM your-base-image:1.0
- Change the working directory inside of the container:
WORKDIR /app
- Copy the
requirements.txt
file from the host file system into the docker container:COPY requirements.txt /to-container/
- Run a pip install for all the packages required:
RUN pip install -r requirements.txt
- Copy the rest of the application into the container:
COPY /from-host/ /to-container/
- Start the uvicorn server through the terminal command:
CMD uvicorn app.location:api_name --host 0.0.0.0
- Build and run the Docker container with Docker Compose:
docker compose up --build
- Problem: Container shuts down when terminal closes. Solution:
docker compose up -d
- Problem: Container doesn't restart when machine restarts. Solution: Add
restart:always
to thecompose.yaml
file. - Problem: Application has errors and I need to see the logs. Solution: Use
docker ps
to get the container ID and then usedocker logs [ID]
to read the logs. - Close the container using
docker compose down