The Astro CLI is intended to make it easier to develop with Apache Airflow, whether you’re developing on your local machine or deploying code to Astronomer. The following guidelines describe a few of the methods you can use to customize the Docker Image that gets pushed to Airflow every time you rebuild your image locally usingDocumentation Index
Fetch the complete documentation index at: https://astronomer-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
$ astro dev start or deploy to Astronomer using $ astro deploy.
More specifically, this doc includes instructions for how to:
- Add Python and OS-level packages
- Add dependencies
- Run commands on build
- Access the Airflow CLI
- Add environment variables locally
- Build from a private repository
The guidelines below assume that you’ve initialized a project on Astronomer via
$ astro dev init. If you haven’t done so already, refer to our“CLI Quickstart” doc.Add Python and OS-level dependencies
To add Python packages to an Airflow Deployment, add the packages to the Deploymentrequirements.txt file. To add OS-level packages to an Airflow Deployment, add them to the Deployment packages.txt file. The requirements.txt and packages.txt files were automatically generated after running astro dev init to initialize your local Astro project.
Add Python dependencies
To add all Python packages to an Airflow Deployment, add the packages to the Deploymentrequirements.txt file.
To pin a version of that package, use the following syntax:
requirements.txt:
Add OS-level dependencies
To add OS-level packages to an Airflow Deployment, add them to the Deploymentpackages.txt file.
Rebuild your image
Once you’ve saved those packages in your text editor or version control tool, rebuild your image by running:Confirm your package was installed (Optional)
If you addedpymongo to your requirements.txt file, for example, you can confirm that it was properly installed by running a $ docker exec command into your scheduler.
- Run
$ docker psto identify the 3 running docker containers on your machine - Grab the container ID of your scheduler container
- Run the following:
Add other dependencies
In the same way you can build Python and OS-level packages into your image, you’re free to build additional dependencies and files for your DAGs to use. In the example below, we’ll add a folder ofhelper_functions with a file (or set of files) that our Airflow DAGs can then use.
Add the folder into your project directory
Rebuild your image
Follow the instructions in the “Rebuild your Image” section above.Confirm your files were added (Optional)
Similar to thepymongo example above, you can confirm that helper.py was properly built into your image by running a $ docker exec command into your scheduler.
- Run
$ docker psto identify the 3 running docker containers on your machine - Grab the container ID of your scheduler container
- Run the following:
helper_functions folder has been built into your image.
Configure airflow_settings.yaml
When you first initialize a new Astro project on Astronomer, a file titledairflow_settings.yaml will be automatically generated. With this file you can configure and programmatically generate Airflow connections, Pools, and Variables when you’re developing locally.
For security reasons, the airflow_settings.yaml file is currently only for local development and should not be used for pushing up code to Astronomer via $ astro deploy. For the same reason, we’d recommend adding this file to your .gitignore.
Add Airflow connections, pools, variables
By default, theairflow_settings.yaml file includes the following template:
$ astro dev start.
Additional entries
If you want to add a second Connection/Pool/Variable, copy the existing fields and make a new entry like so:Run commands on build
If you’re interested in running any extra commands when your Airflow image builds, it can be added to yourDockerfile as a RUN command. These will run as the last step in the image build process.
For example, if you wanted to run ls when your image builds, your Dockerfile would look like this:
Docker Compose override
The Astro CLI is built on top of Docker Compose, a tool for defining and running multi-container Docker applications. To override the default CLI configurations, add adocker-compose.override.yml file to your Astro project directory. The values in this file override the default settings when you run $ astro dev start.
To add another volume mount for a directory named custom_dependencies, for example, add the following to your docker-compose.override.yml:
version: "3.1" and mimic the format of the source code file linked above.
When your image builds on $ astro dev start, any changes made within the custom_dependencies directory will be picked up automatically the same way they are with files in your dags directory:
The Astro CLI doesnotsupport overrides to Environment Variables. For more information on how to set, configure and customize those values, see“Environment Variables” doc.
Access to the Airflow CLI
You’re free to use native Airflow CLI commands on Astronomer when developing locally by wrapping them around docker commands. To add a connection, for example, you can run:Add environment variables locally
To import environment variables from a specific file, run$ astro dev start with an --env flag:
This feature is limited to local development only. Whatever
.env you use locally will not be bundled up when you deploy to Astronomer.For more detail on how to add environment variables both locally and on Astronomer, refer to our Environment Variables doc.Install Python packages from private sources
Python packages can be installed from public and private locations into your image. To install public packages listed on PyPI, follow the steps in Add Python and OS-level Packages. To install packages listed on private PyPI indices or a private git-based repository, you need to complete additional configuration in your project. Depending on where your private packages are stored, use one of the following setups to install your packages to an Astro project by customizing your Runtime image.- Private GitHub Repo
- Private PyPi Index
Install Python packages from a private GitHub repository
This topic provides instructions for building your Astro project with Python packages from a private GitHub repository. At a high level, this setup entails specifying your private packages inrequirements.txt, creating a custom Docker image that mounts a GitHub SSH key for your private GitHub repositories, and building your project with this Docker image.Although this setup is based on GitHub, the general steps can be completed with any hosted Git repository.The following setup has been validated only with a single SSH key. Due to the nature of
ssh-agent, you might need to modify this setup when using more than one SSH key per Docker image.Prerequisites
To install Python packages from a private GitHub repository on Astronomer Software, you need:- The Astro CLI.
- A Software project.
- Custom Python packages that are installable via pip.
- A private GitHub repository for each of your custom Python packages.
- A GitHub SSH Private Key authorized to access your private GitHub repositories.
If your organization enforces SAML single sign-on (SSO), you must first authorize your key to be used with that authentication method. For instructions, seeGitHub documentation.
Step 1: Specify the private repository in your project
To add a Python package from a private repository to your Software project, specify the repository’s SSH URL in your project’srequirements.txt file. This URL should be formatted as git+ssh://git@github.com/<your-github-organization-name>/<your-private-repository>.git.For example, to install the mypackage1 & mypackage2 from myorganization, as well as numpy v 1.22.1, you would add the following to requirements.txt:mypackage1 is both the name of the package and the name of the repository.Step 2. Create Dockerfile.build
-
In your Astro project, create a duplicate of your
Dockerfileand name itDockerfile.build. -
In
Dockerfile.build, addAS stageto theFROMline which specifies your Astronomer image. For example, if you use Astro Runtime 5.0.6, yourFROMline would be:
-
In
Dockerfile.buildafter theFROMline specifying your image, add the following configuration:In order, these commands:- Install any OS-level packages specified in
packages.txt. - Securely mount your GitHub SSH key at build time. This ensures that the key itself is not stored in the resulting Docker image filesystem or metadata.
- Install all Python-level packages in
requirements.txtfile, including those from a private GitHub repository.
- Install any OS-level packages specified in
If your repository is hosted somewhere other than GitHub, replace the domain in the
ssh-keyscancommand with the domain where the package is hosted.Step 3. Build a custom Docker image
-
Run the following command to create a new Docker image from your
Dockerfile.buildfile, making sure to replace<ssh-key>with your SSH private key file name and<your-image>with your Astronomer image:For example, if you havequay.io/astronomer/runtime:5.0.6-basein yourDockerfile.build, this command would be:
If your repository is hosted somewhere other than GitHub, replace the location of your SSH key in the
--sshflag.-
Replace the contents of your Software project’s
Dockerfilewith the following:For example, if your base image wasquay.io/astronomer/runtime:5.0.6-base, this line would be:
Add a CA certificate to an Astro Runtime image
If you need your Astro deployment to communicate securely with a remote service using a certificate signed by an untrusted or internal certificate authority (CA), you need to add the CA certificate to the trust store inside the image.-
In your Astro project
Dockerfile, add the following entry below the existingFROMstatement which specifies your Astro Runtime image version: -
Optional. Add additional
COPYstatements before theRUN update-ca-certificatesstanza for each CA certificate your organization is using for external access. - Save your changes and test them locally, or deploy them to a test Deployment.