Skip to content

Building Containers

HCC2 Applications are deployed using Docker containers. The following sections give a brief overview of this process. For more detailed descriptions of Docker, please refer to Docker.com

Example Dockerfile

In your development environment run the following command:

cd $HOME/edgesdkexample

Edit the file Dockerfile. You will need to configure this file to accommodate your specific app requirements.

This following Dockerfile has been built for this specific app example.

# Build stage
FROM qratehcc2sdk.azurecr.io/python:3.12-slim-2024-04-19-debugbuild AS builder-image  

WORKDIR /app

# Add App packages (needed to be done here because this stage has pip) 
COPY requirements.txt .
COPY packages ./packages

# Add Python packages
RUN pip3 install -r requirements.txt && pip3 install typing-extensions --upgrade
RUN pip3 install packages/hcc2sdk-0.1.0-py3-none-any.whl

# Uninstall pip (pip is installed into /usr/local/lib/python3.12/site-packages/pip 
# & is not needed on the target)
RUN pip3 uninstall -y pip

# Install packages needed by pip packages
RUN apt-get update && apt-get install -y zlib1g-dev libssl-dev

# Run stage
FROM qratehcc2sdk.azurecr.io/static-debian12:262ae336-distroless-py-2024-04-27-prod AS runner-image

# Determine chipset architecture for copying python
ARG CHIPSET_ARCH=x86_64-linux-gnu

# Copy python from builder
COPY --from=builder-image /usr/local/lib/ /usr/local/lib/
COPY --from=builder-image /usr/local/bin/python3.12 /usr/local/bin/python3.12
COPY --from=builder-image /etc/ld.so.cache /etc/ld.so.cache

# Copy pip's dependencies (this will could change, depending on pip package requirements)
COPY --from=builder-image /lib64/ld-linux-x86-64.so.2 /lib64/
COPY --from=builder-image /lib/${CHIPSET_ARCH}/libc.so.6 /lib/${CHIPSET_ARCH}/
COPY --from=builder-image /lib/${CHIPSET_ARCH}/libm.so.6 /lib/${CHIPSET_ARCH}/
COPY --from=builder-image /lib/${CHIPSET_ARCH}/libssl.so.3 /lib/${CHIPSET_ARCH}/libssl.so.3
COPY --from=builder-image /lib/${CHIPSET_ARCH}/libcrypto.so.3 /lib/${CHIPSET_ARCH}/libcrypto.so.3
COPY --from=builder-image /lib/${CHIPSET_ARCH}/libz.so.1 /lib/${CHIPSET_ARCH}/libz.so.1

# Copy installed Python packages from builder stage
COPY --from=builder-image /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages

# Add App code and configuration
WORKDIR /app

COPY *.py .
COPY appconfig ./appconfig


# Run the App entrypoint module
CMD ["/usr/local/bin/python3.12", "main.py"]

Creating Images locally (Docker Desktop)

The Docker build command is used to build a Docker image. If a filename is not provided the Dockerfile for current path is used:

docker build . -t <<name>>/<<app name>>:<<tag>>

To use an alternative Docker file, enter the following:

docker build . -f <<filename>> -t <<name>>/<<app name>>:<<tag>>

where:

  • <<app name>> is your app name (on this example, pyapp). Make sure yours is unique, e.g. <initials>_<app>_<version>
  • <<tag>> is the tag you assign to this container and is used in later stages.
  • <<filename>> is the name of the Docker file, The default is 'Dockerfile'.
  • --no-cache can be added to the Docker build command if you are recreating the image several times. It ensures that Docker does not use any cached layers during the build process, ensuring that each build step retrieves the latest versions of dependencies and avoids potential inconsistencies.

Tip

To view a list of local images, enter:

sudo docker image ls

Note

Docker images should also appear on the Docker for Windows images page.

Building From Visual Studio 2022

It is possible to build your Docker image from Windows, using Visual Studio should you so desire. For this option the prerequisites are:

  • Visual Studio
  • WSL v2, with Ubuntu
  • Docker Desktop

To build a Visual Studio HCC2 application:

  1. Build and compile your C# application, testing as required with a Modbus server simulator (to replicate a HCC2) or physical HCC2 device.

  2. Add Docker support to your project OR build your own Dockerfile.

  3. From WSL, navigate to your project folder (found in the /mnt/<<windows path>>)

  4. Run the Docker build command as described above.

    sudo docker build . -t <<name>>/<<app name>>:<<tag>>
    

Tip

For far better performance, keep your code inside WSL's filesystem instead of in the Windows filesystem as shown above.