Working with Docker images
Dockerfile
When making an application, you need to create a Dockerfile to build an image that contains all your project dependencies and files. In this way, you can deploy this image in any environment that has Docker (or other engines) installed, like our Kubernetes cluster, where they can start a container based on your image.
You can get more information about Dockerfile syntax and best practices in the official documentation.
All images should be based on others (which can be found on Docker Hub) to avoid reinventing the wheel. For example, if you are developing a Python application, you can use the official Python image as a base image. This way, you don't need to install Python in your image, and you can use the official image as a base to build your application.
Even you want to install Python from scratch, you also need to base your image on other one with an operating system. For example, you can use the official Ubuntu image as a base image to install Python and other dependencies.
As an example, here is a simple Dockerfile that uses the official Python image as a base image for running a Python script:
# Use the official Python image as a base image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Run script.py when the container launches
CMD ["python", "script.py"]
Note: You should try to find some already existing Dockerfiles depending on the technology you are using.
Registry
Docker Hub
Setup
-
Create an account on Docker Hub
-
Login with your username and password
docker login
Deploy
-
Build the image
docker build -t username/example:latest . -
Push the image
docker push username/example:latest
Usage
docker pull username/example:latest
ghcr.io
Setup
-
Create token on GitHub with following permissions:
read:packageswrite:packagesdelete:packages
-
Login with your username and token
docker login ghcr.io
Deploy
-
Add the following label to the Dockerfile to link the image to the repository
(more lines)LABEL org.opencontainers.image.source=https://github.com/agrospAI/example -
Build the image
docker build -t ghcr.io/agrospAI/example:latest . -
Push the image
docker push ghcr.io/agrospAI/example:latest
Usage
docker pull ghcr.io/agrospAI/example:latest
Harbor (our private registry)
Setup
-
Request access to the registry to an administrator
-
Login with your username and password
docker login registry.agrospai.udl.cat
Deploy
-
Build the image
docker build -t registry.agrospai.udl.cat/agrospai/example:latest . -
Push the image
docker push registry.agrospai.udl.cat/agrospai/example:latest
Usage
docker pull registry.agrospai.udl.cat/agrospai/example:latest