Dockerized Python-Node.js Environment with Tor and yt-dlp

 Dockerized Python-Node.js Environment with Tor and yt-dlp

This blog post will guide you through the steps to create and use a Docker container that combines Python, Node.js, Tor, and yt-dlp—a powerful tool for downloading videos. This setup is particularly useful for developing applications that require secure, anonymized video downloads via the Tor network.


Step 1: Setting Up the Dockerfile

Here’s the Dockerfile that we’ll be using:


FROM nikolaik/python-nodejs:python3.10-nodejs19


# Install system dependencies, including Tor

RUN apt-get update \

    && apt-get install -y --no-install-recommends \

       ffmpeg \

       wget \

       tor \

    && apt-get clean \

    && rm -rf /var/lib/apt/lists/*


# Download and install yt-dlp

RUN wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp \

    && chmod a+rx /usr/local/bin/yt-dlp \

    && yt-dlp -U


# Expose Tor SOCKS5 port

EXPOSE 9050 9051


# Configure Tor

RUN echo "SocksPort 0.0.0.0:9050" >> /etc/tor/torrc \

    && echo "ControlPort 9051" >> /etc/tor/torrc \

    && echo "CookieAuthentication 1" >> /etc/tor/torrc


# Copy application files

COPY . /app/

WORKDIR /app/

RUN pip3 install --no-cache-dir -U -r requirements.txt


# Start Tor and your application

CMD ["bash", "-c", "tor & bash start"]




These Docker commands are used to manage the lifecycle of Docker containers, from building an image to running, logging, stopping, and removing a container. Here's a detailed explanation of each command:


1. docker build -t choco .

Purpose: This command is used to build a Docker image from a Dockerfile.


-t choco: This flag assigns a name (or tag) to the built image, in this case, "choco".


. (dot): The dot specifies the build context, meaning it includes all the files in the current directory to build the image.


Example:


bash

Copy code

docker build -t choco .

This will create a Docker image named choco from the Dockerfile located in the current directory.


2. docker run -d -p 9050:9050 -p 9051:9051 --name choco --env-file .env choco

Purpose: This command runs a container from the choco image created earlier.


-d: Runs the container in detached mode, meaning it runs in the background.


-p 9050:9050 -p 9051:9051: Maps ports from the container to the host machine. In this case, container ports 9050 and 9051 are mapped to the same ports on the host. This is useful for accessing services inside the container, such as Tor.


--name choco: Assigns the name "choco" to the running container, making it easier to manage (e.g., stop, remove).


--env-file .env: Loads environment variables from the .env file into the container. This file typically contains key-value pairs like ENV_VAR=value that the container might need.


choco: Specifies the image name to run the container from.


Example:


bash

Copy code

docker run -d -p 9050:9050 -p 9051:9051 --name choco --env-file .env choco

This command starts a new container in the background using the choco image, with the name choco, maps ports 9050 and 9051, and loads environment variables from the .env file.


3. docker logs choco

Purpose: This command retrieves and displays the logs from the choco container.


choco: The name of the container whose logs you want to view.


Example:


bash

Copy code

docker logs choco

This command will display the log output of the choco container, which is helpful for debugging or monitoring the container's activity.


4. docker stop choco

Purpose: This command stops the running choco container.


choco: The name of the container you want to stop.


Example:


bash

Copy code

docker stop choco

This will stop the choco container, effectively halting its operations.


5. docker rm choco

Purpose: This command removes the choco container from Docker.


choco: The name of the container you want to remove.


Example:


bash

Copy code

docker rm choco

After stopping the container, this command will delete the choco container, freeing up system resources.


Note: You cannot remove a running container, so it must be stopped first using docker stop.

 info.



----AKG



Comments

Popular posts from this blog

Installation of docker in Linux