Transform image to edge service¶
This example guides you through the steps to publish an existing Docker® image as an edge service, create an associated deployment pattern, and register your edge nodes to run that deployment pattern.
Before you begin¶
Complete the prerequisite steps in Preparing to create an edge service. As a result, these environment variables should be set, these commands should be installed, and these files should exist:
echo "HZN_ORG_ID=$HZN_ORG_ID, HZN_EXCHANGE_USER_AUTH=$HZN_EXCHANGE_USER_AUTH, DOCKER_HUB_ID=$DOCKER_HUB_ID"
which git jq
ls ~/.hzn/keys/service.private.key ~/.hzn/keys/service.public.pem
cat /etc/default/horizon
Procedure¶
Note: See Conventions used in this document for more information about command syntax.
-
Create a project directory.
-
On your development host, change to your existing Docker project directory. If you don't have an existing Docker® project, but still want to continue with this example, use these commands to create a simple Dockerfile that can be used with the rest of this procedure:
cat << EndOfContent > Dockerfile
FROM alpine:latest
CMD while :; do echo "Hello, world."; sleep 3; done
EndOfContent
- Create edge service metadata for your project:
This command creates horizon/service.definition.json to describe your service and horizon/pattern.json to describe the deployment pattern. You can open these files and browse their content.
-
Build and test your service.
-
Build your docker image. The image name must match what is referenced in horizon/service.definition.json.
eval $(hzn util configconv -f horizon/hzn.json)
export ARCH=$(hzn architecture)
sudo docker build -t "${DOCKER_IMAGE_BASE}_$ARCH:$SERVICE_VERSION" .
unset DOCKER_IMAGE_BASE SERVICE_NAME SERVICE_VERSION
- Run this service container image in the Horizon simulated agent environment:
- Verify that your service container is running:
- View the environment variables that were passed to the container when it was started. (These are the same environment variables that the full agent passes to the service container.)
- View the service container log:
On Linux:
On macOS:
- Stop the service:
- Publish your service to Open Horizon. Now that you verified that your service code runs as expected in the simulated agent environment, publish the service to Horizon exchange so that it becomes available for deployment to your edge nodes.
The following publish command uses the horizon/service.definition.json file and your key pair to sign and publish your service to Horizon exchange. It also pushes your image to Docker® Hub.
- Publish a deployment pattern for the service. This deployment pattern can be used by edge nodes to cause Open Horizon to deploy the service to them:
-
Register your edge node to run your deployment pattern.
-
In the same way that you previously registered edge nodes with public deployment patterns from the IBM organization, register your edge node with the deployment pattern you published under your own organization:
- List the docker container edge service that has been started as a result:
- View the myservice edge service output:
- View the node, service, and pattern that you have created in the Open Horizon console. You can display the console URL with:
- Unregister your edge node and stop the myservice service:
What to do next¶
- Try the other edge service examples at Developing edge services with Open Horizon.