'How to take user input and let the user upload / submit a .json file to a Docker image

So I am pretty new to Docker but already really liking its functionalities and container / block mentality.

Long story short, lately I've been creating some scripts but I kept running into the same problem. When I would try running them, I created them on Linux, on Windows I would have to spend a lot of time setting things up.

Now here the beauty that is Docker comes in which allows me to ship ready-made images. Just fantastic!

Given my newbie status in Docker I am having some trouble doing certain things. I am currently reading the book "Docker in Action" and loving it but something like my problem has not (yet) been covered there.

Now I have the following code (this is not everything but the part I am struggling with):

import json
import sys
import random
import time

from web3 import Web3

def broadcast_transaction(sender, private_key, amount, contract):
    # Get the nonce. Prevents one from sending the transaction twice
    nonce = web3.eth.getTransactionCount(sender)
    to_address = web3.toChecksumAddress(contract)
    # Build a transaction in a dictionary
    tx = {
        'nonce': nonce,
        'to': to_address,
        'value': web3.toWei(amount, 'gwei'),
        'gas': 300000,
        'gasPrice': web3.toWei('5', 'gwei'),
        'chainId': 0x38
    }
    # sign the transaction
    signed_tx = web3.eth.account.sign_transaction(tx, private_key)
    # send transaction
    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    # get transaction hash
    tx_id = web3.toHex(tx_hash)
    print(f"Transaction: \n\n"
          f"From: {sender}\n"
          f"To: {to_address}\n"
          f"Status: successful\n"
          f"\n"
          f"TxID is: {tx_id}")


print("Please input the EXACT address of the receiver contract or address. \nPress ENTER after having pasted the "
      "contract / account address or type Q to quit.\n")

contract_address = str(input())


print("Please input the EXACT filename of the json file, including .json. \nPress ENTER after having pasted the "
      "filename or type Q to quit.\n")

json_filename = str(input())

if json_filename.lower() == "q":
    sys.exit()

delay = int(input("Please input the delay you want to set in WHOLE minutes.\nGOOD: 1 3 9 16 20\nWRONG: 1.5, 16.89, "
                  "218.2315\nPress ENTER after having typed in the input\n"))

delay = delay * 60

# Opening JSON file
f = open(json_filename)

# returns JSON object as
# a dictionary
data = json.load(f)

So as you can see I am taking in multiple inputs. When running the script I have to:

  1. Provide pubkey
  2. Provide private key
  3. Amount is hardcoded
  4. Receiver pubkey
  5. As json from which to load addresses.

Now to summarize my questions:

  1. Can I store the pub- and private-key inside a .env? Does .env work with Docker / is it advisable. I would have to build for every change for the sender.

  2. How can I make it so that the user can still provide the address and filename without having to hardcode it?

  3. How do I get a .json that changes for every run of the script into the image? Since I am shipping the image without the .json (.json is provided by client).

Now I have one idea, which is to basically create a Django webserver that takes the inputs through a website which would also be a cool personal project but I do wonder what more experienced and professional Docker users would do in this situation.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source