Skip to main content

Web Server Sample

The Web Server sample shows you how to deploy a simple HTTP server inside an Atym container.

This sample uses Mongoose, a lightweight, cross-platform networking library for C/C++. Mongoose provides event-driven, non-blocking APIs for TCP, HTTP, WebSocket, MQTT, and more. It's widely used in embedded and IoT products—including on the International Space Station! With Mongoose, you can easily add robust networking features to your containerized applications.


Building the Container

Building the Web Server sample involves three main steps: compiling the application to WebAssembly, packaging it into an Atym container, and pushing it to the Atym Hub.

Building the Application

Navigate to the webserver sample in the Ocre SDK samples directory and create a build directory to compile the application to WebAssembly:

cd ocre-sdk/generic/webserver/
mkdir build && cd build
cmake ..
make

This creates a WASM module named webserver.wasm, which will be used by the atym build command in the next step.

Create the Container

Return to the project root directory and build the Atym container:

cd ..
atym build

If the container creation was successful, you should see a new directory, .atym, in your home directory (~/.atym). Inside, you'll find a directory named after your container image (e.g., webserver), containing the container contents. The image name is defined in your build.yaml file.

Push the Container to the Atym Hub

Push your container to the Atym Hub:

atym push webserver
info

We did not specify a "tag" (e.g. atym push webserver:tag), which will result in this image being tagged as latest.


Deploying the Container

This section shows you how to deploy the Web Server sample container to your device using the Atym CLI.

tip

This example uses deviceName for device identification, which is typically easier to remember and use. You can also use deviceUUID if preferred. To find your device name or UUID, use atym list devices or check the Atym Hub web interface.

Deploy to the Target Device

Deploy the container to your device with the following command, replacing deviceName with your device's name:

atym run webserver webserver -n deviceName

Where the first webserver is your chosen container name, and the next webserver is the IMAGE_REF[:TAG], which is the container image from your registry. In this example we did not specify a tag, and are therefore running the latest tag. This command also requires a device identifier (-n for deviceName, or -d for deviceUUID).

Verify Deployment

The Atym Hub will deploy your container to the device. You should see output in your console indicating the Web Server is running and ready to accept connections, similar to the following:

**********************************************
Web server listening on port: 8000
**********************************************
To connect:
1. Find this device's IP using: net iface
2. Open your web browser
3. Navigate to: http://<IP>:8000
**********************************************

Follow the instructions above to connect to your Web Server. When you visit the address in your browser, you should see a very basic HTML webpage served by your container!


Next Steps

Next, try tweaking the webpage content by editing the source code in src/main.c and rebuilding your container. Once you're comfortable, challenge yourself by adding WebSocket support or building a simple REST API to make your web server even more interactive!