Using Docker
Docker best practices recommend having only one running process in a container. This is what we are going to achieve. We will need to write a Dockerfile
containing instructions to build a fully functional Zenaton Agent container.
In our container, we will need:
- A base image being one of the supported Zenaton platforms.
- The necessary binaries to be able to run our application code.
- A Zenaton Agent installation.
We will install everything in our container, and start the Zenaton Agent when the container is started.
FROM node:stretch
RUN apt-get update && apt-get install -y curl
# copy your files into the container
COPY src/ /app/
COPY package.json yarn.lock ./app/
COPY start_agent.sh boot.js client.js ./app/
RUN ["chmod", "+x", "/app/start_agent.sh"]
# Change the working directory to the one we just created
WORKDIR /app
# Install application dependencies
RUN yarn
# Install Zenaton
RUN curl https://install.zenaton.com | sh
# Set the PORT environment variable to be used by the Agent
ENV PORT 4001
# Document the fact that the container will expose port 4001
EXPOSE 4001
# Set entrypoint to the agent start script
ENTRYPOINT ["./start_agent.sh"]
And the content of the start_agent.sh
script, which will start the Agent, run the listen command, and tail the zenaton.out file to make sure the container stays alive:
#!/bin/sh
set -e
# Start zenaton worker
zenaton start
zenaton listen --boot=boot.js
# Print zenaton output to stdout
touch zenaton.out
tail -f zenaton.out
If you want an Agent running in client mode, don't forget to add the --client
option to the listen command in this script.
We can now build the docker image:
docker build -t zenaton-agent-node .
The last thing you need to know is that by default, Zenaton libraries assume the Agent is located on the same server as your web application. So it will try to make HTTP requests to http://localhost:4001/
. When using a separate container for your Agent, you won't have anything responding on this hostname and port. In your Web App containers, make sure you set the ZENATON_WORKER_URL
environment variable to an ip/hostname and port of the Agent container that must be reachable from your application container, e.g. http://zenaton-agent:4001
.
In this example we did not generate a .env file inside the container. So when we run the container, we will need to supply the App ID, Api Token and App Env as environment variables for the Agent to be able to authenticate itself.
docker run --init --rm -e ZENATON_APP_ID=<your app id> -e ZENATON_API_TOKEN=<your api token> -e ZENATON_APP_ENV=production zenaton-agent-node
Alternatively, you could create an env.list
file that will supply the environment variables to Docker:
ZENATON_APP_ID=<your app id>
ZENATON_API_TOKEN=<your api token>
ZENATON_APP_ENV=production
In this case you would run the container like this:
docker run --init --rm --env-file env.list zenaton-agent-node
That's it! Your Agent container is now up and running.