docker-airflow icon indicating copy to clipboard operation
docker-airflow copied to clipboard

pre-configured airflow "Connections"

Open ToGoBananas opened this issue 9 years ago • 13 comments

Is there a way to create docker image with pre-configured airflow "Connections"?

ToGoBananas avatar Mar 23 '17 09:03 ToGoBananas

The airflow CLI allows adding connections, see https://airflow.incubator.apache.org/cli.html

You could add your custom connection statements (airflow connections -a...) either to the entrypoint.sh and rebuild the image, or you can inherit an image FROM puckel/docker-airflow and add a RUN statement in there.

Sorry if I misunderstood your question

mendhak avatar Mar 24 '17 16:03 mendhak

@mendhak thanks for advice, with CLI we can pass only conn_uri param, but I want to create Connection based on conn_type and some extras maybe you know the proper way to build conn_uri?

ToGoBananas avatar Mar 29 '17 14:03 ToGoBananas

I decided to insert connections directly in databse

ToGoBananas avatar Mar 30 '17 06:03 ToGoBananas

Hey @ToGoBananas can you share sample statements you used to insert connections directly?

With the CLI I've added variables and connections like this:

sudo docker-compose exec webserver airflow variables -i variables.json

sudo docker-compose exec webserver airflow connections -a --conn_id examplessh --conn_uri ssh://user:pass@serverip --conn_extra '{"key_file":"/usr/local/airflow/id_rsa", "no_host_key_check":true}'

sudo docker-compose exec webserver airflow connections -a --conn_id examples3 --conn_uri s3://my-test-bucket --conn_extra '{"aws_access_key_id":"AAAAAAA", "aws_secret_access_key":"bbbbbbb", "bucket_name":"mybucket"}'


mendhak avatar Mar 31 '17 15:03 mendhak

@mendhak do you know how to add a connection for google cloud with CLI directly?

yu-iskw avatar Jun 15 '17 00:06 yu-iskw

Based on the types listed here and the code here it seems like everything needs to go into extras but the UI shows it in a different form through some behind-the-scenes magic.

I think you should try

sudo docker-compose exec webserver airflow connections -a --conn_id googletest --conn_uri google_cloud_platform://dummyvalue --conn_extra '{ 'key_path':'....','scope':'....' }'

Main thing is getting the schema right (google_cloud_platform://) and after that just a matter of putting in the rest of the values for GCP into extras

You may have to experiment with this a few times.

mendhak avatar Jun 15 '17 05:06 mendhak

@mendhak Thank you the instruction. I will give it a try!

yu-iskw avatar Jun 15 '17 16:06 yu-iskw

@mendhak I have inspected the source code. It seems that airflow connections has a bug about the feature to parse --conn_uri. I have reported that to airflow community. Thank you for your help.

https://issues.apache.org/jira/browse/AIRFLOW-1330

yu-iskw avatar Jun 21 '17 18:06 yu-iskw

update for anyone coming across this thread:

Add to entrypoint.sh after initdb:

airflow connections --add --conn_id=gcp --conn_type=google_cloud_platform --conn_extra='{ "extra__google_cloud_platform__key_path":"/usr/local/airflow/secrets/gcp_key.json", "extra__google_cloud_platform__project": "gcp-project", "extra__google_cloud_platform__scope": "https://www.googleapis.com/auth/cloud-platform"}'

ayoungprogrammer avatar Jan 20 '18 06:01 ayoungprogrammer

You can also add connections from python using the airflow libraries. I run this from entrypoint.sh:

https://gist.github.com/jnalley/80cbfbf2619c848e4d1d1efeec0c836c

jnalley avatar May 22 '18 02:05 jnalley

@jnalley Can you please share your entrypoint.sh file as well. I am following the same approach but not sure how do I execute setup_connection.py from entrypoint.sh file. Thank you!

sarika-pst avatar Dec 13 '19 06:12 sarika-pst

@aicdp1010 I added the entrypoint.sh I was using here:

https://gist.github.com/jnalley/3831d3104cb008adceb86b996bd499c0

My Dockerfile also has:

COPY scripts /usr/local/airflow/

So, I just put all the .py scripts under the scripts directory (along with entrypoint.sh).

jnalley avatar Dec 13 '19 20:12 jnalley

In 2022 to add a connection to the local docker registry

airflow connections add 'docker_default' \
      --conn-type 'docker' \
      --conn-login 'root' \
      --conn-host '127.0.0.1' \
      --conn-port '5000'

gudata avatar Jun 30 '22 07:06 gudata