Vincent Rubingh
08/14/2024, 8:16 AMMarvin
08/14/2024, 8:17 AMpython
from prefect.infrastructure.docker import DockerContainer
docker_block = DockerContainer(
image="your-docker-image",
auto_remove=True,
stream_output=True
).save("my-docker-block")
Replace "your-docker-image"
with the appropriate image name.
#### 2. Define Your Deployment
Next, define your deployment with the DockerBlock and deploy it. Here's an example:
python
from prefect.deployments import Deployment
from prefect.infrastructure.docker import DockerContainer
docker_block = DockerContainer.load("my-docker-block")
deployment = Deployment.build_from_flow(
flow=my_flow,
name="my-docker-deployment",
infrastructure=docker_block,
)
deployment.apply()
Replace my_flow
with your flow.
#### 3. Use Tailscale for Secure Connections
For secure connections, use Tailscale or any VPN solution that you prefer. Here’s a basic outline:
1. Install Tailscale on your machine and the server where the Docker containers will be run.
2. Authenticate Tailscale on both ends and ensure they are connected via Tailscale.
3. Configure Docker to use the Tailscale IP addresses instead of exposing ports directly.
#### Tailscale Setup
On your server and local machine:
- Install Tailscale: Follow Tailscale's installation guide.
- Authenticate and Connect: Use tailscale up
to authenticate and connect.
#### Example run command with Tailscale IP
If your Tailscale IP is 100.x.x.x
, ensure your Docker configuration and Prefect Block are set to this IP without opening ports publicly.
#### Links for More Information:
Here are some useful links for deep dive into the setup steps:
- Prefect Documentation on Docker Infrastructure
- Tailscale Documentation
Let's fetch some more specific Prefect documentation and example code for the DockerBlock setup to ensure you have the latest and accurate information.
### Resources and Example Code
#### 1. Docker Block and Deployment
You can create a DockerBlock to manage credentials for interacting with a Docker registry as shown in this documentation. Here is an example to setup DockerBlock and using it in a deployment:
##### Docker Block Setup:
python
from prefect.infrastructure.docker import DockerContainer
docker_block = DockerContainer(
image="your-docker-image",
auto_remove=True,
stream_output=True
).save("my-docker-block")
You may also need to include registry credentials as shown below:
python
from prefect_docker import DockerHost, DockerRegistryCredentials
docker_host = DockerHost()
docker_registry_credentials = DockerRegistryCredentials(
username="my_username",
password="my_password",
registry_url="<http://registry.hub.docker.com|registry.hub.docker.com>",
)
with docker_host.get_client() as client:
docker_registry_credentials.login(client)
#### 2. Creating Deployment:
Use the DockerBlock in a deployment:
python
from prefect.deployments import Deployment
from prefect.infrastructure.docker import DockerContainer
docker_block = DockerContainer.load("my-docker-block")
deployment = Deployment.build_from_flow(
flow=my_flow,
name="my-docker-deployment",
infrastructure=docker_block,
)
deployment.apply()
Marvin
08/14/2024, 8:17 AMtailscale up
to authenticate and connect.
You'll need to set up your Docker connections to use Tailscale's IP addresses ensuring that you're not exposing ports publicly.
### Additional Information:
- Example flow with deployment using Docker infrastructure can be referenced from this example.
For those who want to dive deeper, please explore the links provided in each section for the most detailed and up-to-date information. Happy securing and deploying!Vincent Rubingh
08/14/2024, 1:50 PMMarvin
08/14/2024, 1:50 PMI'm getting a long error when trying to launch a countainer from my local workerI have this pseudo code: import ast from prefect import flow, task from prefect.infrastructure.container import DockerContainer # from prefect_docker.containers import get_docker_container_logs, remove_docker_container from prefect.artifacts import create_markdown_artifact from prefect.runner.storage import GitRepository from prefect.blocks.system import Secret @flow(name="Jora Job ETL Flow") def start_flow(): run_job_parser() # run_sentence_splitter() test_task() @task(name="Parse Jora Jobs", retries=0, log_prints=True) def run_job_parser(): docker_container_block = DockerContainer.load("mediariot-docker-parser") container_result = docker_container_block.run() return {} @task(name="Parse Jobs Next", retries=0) def test_task(): print("This is a test task") if name == "__main__": start_flow.from_source( source=GitRepository( url="https://github.com/rubinghv/jobify.git", branch="dev", credentials={ "access_token": Secret.load("github-access-token") } ), entrypoint="flows/job_etl.py:start_flow" ).deploy( name="mediariot-deployment", work_pool_name="mediariot-docker-pool", ) Then a service running a worker on my own server
Vincent Rubingh
08/14/2024, 1:53 PMMarvin
08/14/2024, 1:53 PMthis error: Encountered exception during execution:Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 789, in urlopen response = self._make_request( File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 495, in _make_request conn.request( File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 398, in request self.endheaders() File "/usr/local/lib/python3.10/http/client.py", line 1278, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/local/lib/python3.10/http/client.py", line 1038, in _send_output self.send(msg) File "/usr/local/lib/python3.10/http/client.py", line 976, in send self.connect() File "/usr/local/lib/python3.10/site-packages/docker/transport/unixconn.py", line 26, in connect sock.connect(self.unix_socket) FileNotFoundError: [Errno 2] No such file or directory During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/requests