Search code examples
dockertor

Several tors services in one docker container


I have dockerfile with run tor in docker:

FROM alpine:latest
RUN apk update && apk upgrade && \
    apk add tor curl && \
    rm /var/cache/apk/* && \
    cp /etc/tor/torrc.sample /etc/tor/torrc && \
    echo "SocksPort 0.0.0.0:9050" > /etc/tor/torrc
EXPOSE 9050
USER tor
CMD /usr/bin/tor -f /etc/tor/torrc

It works. I want to run several tors in one dockerfile and open different ports (9051,9052, etc). I can create docker-compose.yml in which for every port create one docker, but it isn't a good solution in my opinion.

May be anybody know how run several tors and publish theirs ports from docker?


Solution

  • For me help this dockerfile:

    FROM alpine:latest
    RUN apk update && apk upgrade && \
        apk add tor curl bash && \
        rm /var/cache/apk/* && \
        cp /etc/tor/torrc.sample /etc/tor/torrc
    
    EXPOSE 9050-9060
    
    ADD start.sh /usr/local/bin/start.sh
    RUN chmod +x /usr/local/bin/start.sh
    
    RUN echo | sed -i 's/\r$//' /usr/local/bin/start.sh
    
    CMD /usr/local/bin/start.sh
    

    And script start.sh:

    #!/bin/bash
    #making script to stop on 1st error
    set -e
    
    # Original script from
    # http://blog.databigbang.com/distributed-scraping-with-multiple-tor-circuits/
    
    # if defined TOR_INSTANCE env variable sets the number of tor instances (default 10)
    TOR_INSTANCES=${TOR_INSTANCES:=10 }
    
    # if defined TOR_OPTIONSE env variable can be used to add options to TOR
    TOR_OPTIONS=${TOR_OPTIONS:=''}
    
    base_socks_port=9050
    base_control_port=11000
    dir_data="/tmp/multitor.$$"
    
    # Create data directory if it doesn't exist
    if [ ! -d $dir_data ]; then
            mkdir $dir_data
    fi
    
    
    if [ ! $TOR_INSTANCES ] || [ $TOR_INSTANCES -lt 1 ]; then
        echo "Please supply an instance count"
        exit 1
    fi
    
    for i in $(seq $TOR_INSTANCES)
    do
            j=$((i+1))
    
            socks_port=$((base_socks_port+i))
            control_port=$((base_control_port+i))
    
            if [ ! -d "$dir_data/tor$i" ]; then
                    echo "Creating directory $dir_data/tor$i"
                    mkdir "$dir_data/tor$i" && chmod -R 700 "$dir_data/tor$i"
            fi
    
            # Take into account that authentication for the control port is disabled. Must be used in secure and controlled environments
            echo "Running: tor --RunAsDaemon 1 --CookieAuthentication 0 --HashedControlPassword \"\" --ControlPort 0.0.0.0:$control_port --PidFile tor$i.pid --SocksPort 0.0.0.0:$socks_port --DataDirectory $dir_data/tor$i  -f /etc/tor/torrc"
    
            tor --RunAsDaemon 1 --CookieAuthentication 0 --HashedControlPassword "" --PidFile $dir_data/tor$i/tor$i.pid --SocksPort 0.0.0.0:$socks_port --DataDirectory $dir_data/tor$i
    done
    
    # So that the container doesn't shut down, sleep this thread
    sleep infinity
    

    Build and start:

    docker build -t torone ./

    docker run -d -e "TOR_INSTANCES=10" -p 9050-9060:9050-9060 --rm --name torone torone

    TOR_INSTANCES - contains how many tors processes want to start.