Skip to the content.

Docker

Terminology

Commands

List your local images

Delete images (many ways)

Clean up images

  1. docker images -q -f dangling=true
    • -q is for quiet mode
    • -f dangling=true filters the results to only show untagged images
  2. docker image prune > removes all dangling images.
    • You may also use docker image prune -a to remove all unused images (images not referenced by any containers)

List your running containers

Stop a running container

Remove a container

List volumes

Delete a volume

Remove unused volumes

Run a Docker image inside a container

Run a Docker image inside a container and override the entrypoint

Run a Docker image inside a container and map a port in the container to a port in the host machine

Create a Dockerfile with instructions to create a basic custom Docker image.

# set base image
FROM python:3.9

# set the working directory in the container
WORKDIR /app

# copy dependencies to the working directory
COPY requirements.txt .

# Install dependencies
RUN pip install -r requirements

# Copy all files within your project folder to the working directory of the image
COPY . /app

# command to run on container start
CMD ["python", "./main.py"]

Create a slightly more complex Dockerfile with pipenv dependencies and specific entrypoints.

# set base image
FROM python:3.9

# (pipenv) install pipenv
RUN pip install pipenv

# set the working directory in the container
WORKDIR /app

# (pipenv) copy dependencies to the working directory
COPY ["Pipfile", "Pipfile.lock", "./"]

# (pipenv) Install dependencies
# (pipenv) We don't need a virtualenv in Docker, so we can install dependencies to the system
RUN pipenv install --system --deploy

# Copy the model
COPY ["predict.py", "model.bin", "./"]

# Expose a port on the container
# Remember to map the port to a port in the host when running the container!
EXPOSE 9696

# Specify entrypoint
ENTRYPOINT ["gunicorn", "--bind=0.0.0.0:9696", "predict:app"]

Build an image based on a Dockerfile

Check how much storage space all of the images, containers and volumes are taking.

Docker compose

Example docker-compose.yaml file below. You may find a more elaborate example at https://docs.docker.com/compose/compose-application-model/

version: "3.9"
services:
  model-server:
    image: zoomcamp-10-model:v1
  gateway:
    image: zoomcamp-10-gateway:v2
    environment:
      - TF_SERVING_HOST=model-server:8500
      - MY_CUSTOM_VAR=${MY_CUSTOM_VAR}
    ports:
      - "9999:9696"
# Map syntax
environment:
   TF_SERVING_HOST: model-server:8500
   MY_CUSTOM_VAR: ${MY_CUSTOM_VAR}

Run the app. The command assumes that your compose file is named docker-compose.yml and is placed in the same directory that you’re invoking the command from.

docker-compose up

Run the app in detached mode.

docker-compose up -d

Shut down the app

docker-compose down

Shut down the app as well as any volumes, both named volumes declared in the compose file as well as anonymous volumes attached to containers.

docker-compose down -v

If you have multiple compose files with non-standard names and you want to specify which one you want to deploy, you may use -f path/to/compose_file.yml with both up and down.

Kubernetes

Kind

Create local cluster

kind create cluster

Delete local cluster

kind delete cluster

Load an image to the local cluster

kind load docker-image docker-image:tag

eksctl

Create a default cluster on EKS.

eksctl create cluster

Create a cluster with a config YAML file

eksctl create cluster -f eks-config.yaml

Example eks-config.yaml

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: mlzoomcamp-eks
  region: eu-west-1

nodeGroups:
  - name: ng-m5-xlarge
    instanceType: m5.xlarge
    desiredCapacity: 1

Delete a cluster

eksctl delete cluster -f eks-config.yaml

kubectl

kubectl command cheatsheet

Example deployment.yamlfile

apiVersion: apps/v1
kind: Deployment
metadata:
  name: <deployment-name>
spec:
  replicas: 1
  selector:
    matchLabels:
      app: <app-name>
  template:
    metadata:
      labels:
        app: <app-name>
    spec:
      containers:
      - name: <my-container>
        image: my-component-image:some-tag
        resources:
          limits:
            memory: "128Mi"
            cpu: "100m"
        ports:
        - containerPort: 9696
        env:
          - name: TF_SERVING_HOST
            value: <service-name>.<namespace>.svc.cluster.local:8500

Example service.yaml file.

apiVersion: v1
kind: Service
metadata:
  name: <service-name>
spec:
  type: LoadBalancer
  selector:
    app: <app-name>
  ports:
  - port: 80
    targetPort: 9696

Multipass

Tool to run Ubuntu VM’s easily with command-line interface.

List available instances

Create and launch a new instance using the latest LTS release

Access the instance shell

Mount a shared folder in the instance

Unmount all mounted folders of instance

Stop an instance

Start a previously created instance

Get info on a specific instance

Delete an instance (send it to the recycle bin)

Recover a deleted instance

Permanently delete all deleted instances