I know this is not my typical crypto stuff. But I’m working on Spuds, a Prometheus client for Spacemesh, and I wanted to make it a docker image so I can just run it like I would cadvisor. Turns out it’s pretty easy to do! This is not necessarily a complete guide, but just something that I can reference in the future if I forget how I did all this.
You should have docker installed already as you will need to run a docker command. I have a tutorial on installing docker, portainer and prometheus that you can check out to catch up.
Make sure you generate a requirements.txt file while in your virtual environment, you can do that with:
pip freeze > requirements.txt
The first step is to navigate to the directory where your script is, and then create a docker file. I am using the CLI so the following commands will work if you are doing the same.
nano dockerfile
In my case, my entry point is ‘main.py’, and I’m using python 3.8, you can change to which ever version works with your script.
# Use an official Python runtime as a parent image
FROM python:3.8
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
I also create a .dockerignore file to make sure I don’t include anything I don’t need.
nano .dockerignore
These are some common things to exclude
__pycache__
*.pyc
.venv/
Now create the docker image. If you are not planning to upload this to docker hub, the username and app name dont’ matter too much, go ahead and fill them in how you want. Otherwise make sure you use the right names to correspond with your repo. Don’t forget the period at the end.
docker build -t <docker username>/<app name>:<tag> .
I created a repo on docker hub, so I’m going to push my docker image so others can easily use it. This isn’t required.
docker push hakehardware/spuds:v0.0.1
You can now view it on docker hub. Now let’s get it running inside docker! I am using portainer with stack files. Here is what my stackfile looks like:
spuds:
container_name: spuds
image: hakehardware/spuds:v0.0.2
command: ["python", "main.py", "--config", "/app/config/config.yml"]
volumes:
- /home/hakedev/nodes:/app/nodes # In your config.yml make sure you are pointing relative to this for config.mainnet.json
- /home/hakedev/spuds:/app/config # config.yml file for spuds
networks:
spacenet:
ipv4_address: 172.18.0.103
networks:
spacenet:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.0.0/16 # Define the desired subnet
gateway: 172.18.0.1 # Define the desired gateway
I’m naming my container ‘spuds’ and pulling down the v0.0.2 image. My script takes a config file which I’ve mounted to /app/config, and I specify this in the command section, along with python and my script name. I am also assigning an IP to this inside my spacenet network, with a static IP address so prometheus knows where to look for the client. I’ve included the section where I define the network as well, just for a complete look at how it’s done.
And that’s it! I really like being able to run my Python scripts as containers as I think it offers a lot of flexibility.