Authentication and API keys
Pulling packages from authenticated Anaconda channels requires an API key—a long-lived, scoped token tied to your Anaconda account that grants read access to the channels that are available to you. You can obtain an API key using either of the following methods:- CLI
- Web UI
Running
anaconda login automatically generates an API key for you to authenticate your conda client for automated build systems.View the API key by running anaconda auth api-key. For more information, see the anaconda-auth developer reference documentation.Writing the Dockerfile
The following steps walk through constructing a multi-stage Dockerfile that pulls packages from authenticated Anaconda channels and produces a minimal runtime image. Each step adds to the Dockerfile; the complete example at the end shows the finished result.The
--mount=type=secret syntax used in this guide requires Docker BuildKit, which is the default build backend since Docker 23.0. If you are using an older version of Docker, you might need to enable BuildKit by setting DOCKER_BUILDKIT=1 before running docker build.Choose a base image
Anaconda provides several base Docker images for you to use as starting points.Begin the Dockerfile by specifying a base image with conda pre-installed. Pin it to a specific version and assign it an alias so it can be referenced later in the Dockerfile:
Install authentication support
To authenticate to Anaconda channels during the build, install the
anaconda-registration package. This package provides plugins that automatically read Docker build secrets and pass them as authentication headers to the repository backend.Install it into the image’s base environment before running any conda commands that require authentication:Define and install your dependencies
-
Define your application’s dependencies in an environment.yml file and copy it into the image:
-
Mount your API key as a build secret and create the environment. The mount syntax depends on how you plan to pass the secret when you build the image:
- Environment variable
- .env file
This expects a secret namedANACONDA_AUTH_API_KEYto be passed todocker build.
Configure the runtime image
At this point, the Dockerfile produces an image with two conda environments: The final image contains only your runtime environment and application code—no conda installation, no
base (from the Miniconda image) and your custom runtime at /env. Shipping base is unnecessary and inflates the final image size.Adding a second stage to the Dockerfile solves this. Start from a minimal base image, copy only the runtime environment from the build stage, and then configure the entrypoint:The
COPY and ENTRYPOINT values above are specific to this example. Adjust them to match your application’s files and start up command.base environment, no build tools.Building the image
With the Dockerfile written, build the image by passing your API key as a build secret. The method here must match the secret mount syntax used in your Dockerfile:- Environment variable
- .env file
When you pass
--secret id=ANACONDA_AUTH_API_KEY to docker build, Docker automatically looks for an environment variable of the same name.Replace <IMAGE_NAME> with the name you want to give your Docker image.
The one-liner retrieves your key with
anaconda auth api-key and passes it directly into the build.Complete example
The following Dockerfile is based on the conda.Dockerfile example.Additional examples
The anaconda/docker-examples repository provides additional working examples, including conda-lock and.env file workflows. Contributions are welcome.