Skip to content

Crop

Goal

Create a container and run the crop step in the container image tagged and built in the previous step.

Lab

This step has a dedicated lab at practice-labs/2-Containers/crop.ipynb.

Container

Each step has its own recipe to build the container image.

The crop step container image recipe is:

crop/Dockerfile
FROM docker.io/library/python:3.12-slim@sha256:a6e34c598f2467ed0e9a8d349809fcd8b5c603269512df273a0bb1784edc11b1 AS builder
WORKDIR /src
COPY pyproject.toml README.md LICENSE.md ./
COPY src/ ./src/
RUN python -m pip wheel --no-cache-dir --no-deps --wheel-dir /dist .

FROM docker.io/library/python:3.12-slim@sha256:a6e34c598f2467ed0e9a8d349809fcd8b5c603269512df273a0bb1784edc11b1 AS runtime
RUN apt-get update && \
    apt-get install -y --no-install-recommends libexpat1 && \
    rm -rf /var/lib/apt/lists/* && \
    groupadd --gid 2000 neo && \
    useradd --uid 2000 --gid 2000 --create-home neo && \
    python -m venv /app/venv && \
    mkdir -p /work && chown 2000:2000 /work
ENV VIRTUAL_ENV=/app/venv
ENV PATH="/app/venv/bin:$PATH"
COPY --from=builder /dist/ /tmp/wheels/
RUN pip install --no-cache-dir /tmp/wheels/*.whl && \
    crop --help && \
    rm -rf /tmp/wheels
USER 2000:2000
WORKDIR /work
ENTRYPOINT []

Build the container image with:

terminal
1
2
3
export WORKSPACE=/workspace/mastering-app-package

podman build --format docker -t localhost/crop:latest ${WORKSPACE}/water-bodies/command-line-tools/crop

How to run a step in a container

We'll use podman container engine (docker is also fine).

The command to run the crop step in the container is:

podman run \
    -i \
    --userns=keep-id \
    --mount=type=bind,source=/workspace/mastering-app-package/runs,target=/runs \
    --workdir=/runs \
    --read-only=true \
    --user="$(id -u):$(id -g)" \
    --rm \
    --env=HOME=/runs \
    localhost/crop:latest \
    crop \
    --aoi='{"type":"Polygon","coordinates":[[[-121.399,39.834],[-120.74,39.834],[-120.74,40.472],[-121.399,40.472],[-121.399,39.834]]]}' \
    --band \
    green \
    --epsg \
    "4326" \
    --input-item \
    https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_10TFK_20210713_0_L2A

Let's break down what this command does:

  • podman run: This is the command to run a container.
  • -i: This flag makes the container interactive, allowing you to interact with it via the terminal.
  • --userns=keep-id: It instructs podman to keep the user namespace ID. --mount=type=bind,source=/workspace/mastering-app-package/runs,target=/runs: This option mounts a directory from the host system to the container. In this case, it mounts the /workspace/mastering-app-package/runs directory on the host to the /runs directory inside the container.
  • --workdir=/runs: Sets the working directory inside the container to /runs.
  • --read-only=true: Makes the file system inside the container read-only, meaning you can't write or modify files inside the container.
  • --user="$(id -u):$(id -g)": Specifies the user and group IDs to be used within the container.
  • --rm: This flag tells podman to remove the container after it has finished running.
  • --env=HOME=/runs: Sets the HOME environment variable inside the container to /runs.
  • localhost/crop:latest: This is the name of the container image that you want to run. It's pulling the image from the local container registry with the name "crop" and the "latest" tag.
  • crop: Runs the installed console command.
  • --aoi "-121.399,39.834,-120.74,40.472": This provides command-line arguments to the Python module. It specifies the area of interest (AOI) as a bounding box.
  • --band green: Specifies the band to be extracted from the Sentinel-2 acquisition. In this case, it's the "green" band.
  • --epsg "4326": Specifies the EPSG code, which defines the coordinate system used for the aoi command-line argument.
  • --input-item ...: Specifies the input STAC item URL. This particular URL points to a Sentinel-2 image hosted on AWS Earth Search.

Steps

Clean-up the /workspace/mastering-app-package/runs folder:

rm -fr /workspace/mastering-app-package/runs/*

Run the command to run the crop step with the green band in the container with:

sh ${WORKSPACE}/scripts/podman-crop-green.sh

Run the command to run the crop step with the nir band in the container with:

sh -x ${WORKSPACE}/scripts/podman-crop-nir.sh

Expected outcome

The folder /workspace/mastering-app-package/runs contains:

(base) jovyan@coder-mrossi:~/runs$ tree .
.
├── crop_green.tif
└── crop_nir.tif

0 directories, 2 files