Repository: cortexlabs/cortex (language: go)

Issue:
./cortex/dev/export_images.sh: 15: set: Illegal option -o pipefail
### Version
0.42.0

### Description
`export_images.sh` scripts fails with following error message

./cortex/dev/export_images.sh: 15: set: Illegal option -o pipefail

### Configuration
```yaml
cluster_name: cortex
region: us-east-1
availability_zones:  # default: 3 random availability zones in your region, e.g. [us-east-1a, us-east-1b, us-east-1c]
  - us-east-1a
  - us-east-1b
  - us-east-1c

tags:  # <string>: <string> map of key/value pairs
  service: "cleanup"


node_groups:
  - name: ng-cpu # name of the node group
    instance_type: m5.large # instance type
    min_instances: 1 # minimum number of instances
    max_instances: 5 # maximum number of instances
    priority: 100 # priority of the node group; the higher the value, the higher the priority [1-100]
    instance_volume_size: 50 # disk storage size per instance (GB)
    instance_volume_type: gp3 # instance volume type [gp2 | gp3 | io1 | st1 | sc1]
    # instance_volume_iops: 3000 # instance volume iops (only applicable to io1/gp3)
    # instance_volume_throughput: 125 # instance volume throughput (only applicable to gp3)
    spot: true # whether to use spot instances
    spot_config:
      instance_distribution: [m5a.large, m5d.large, m5n.large, m5ad.large, m5dn.large, m4.large, t3.large, t3a.large, t2.large]
      on_demand_base_capacity: 1
      on_demand_percentage_above_base_capacity: 0

subnet_visibility: private
nat_gateway: single

api_load_balancer_type: nlb
api_load_balancer_scheme: internet-facing
operator_load_balancer_scheme: internet-facing

api_load_balancer_cidr_white_list: [0.0.0.0/0]
operator_load_balancer_cidr_white_list: [0.0.0.0/0]

# SSL certificate ARN (only necessary when using a custom domain)
# ssl_certificate_arn:

# list of IAM policies to attach to your Cortex APIs
iam_policy_arns: ["arn:aws:iam::aws:policy/AmazonS3FullAccess"]

# primary CIDR block for the cluster's VPC
vpc_cidr: 192.168.0.0/16

# instance type for prometheus (use an instance with more memory for clusters exceeding 300 nodes or 300 pods)
prometheus_instance_type: "t3.small"
```

### Steps to reproduce

1. export CORTEX_VERSION=0.42.0
2. git clone --depth 1 --branch v$CORTEX_VERSION https://github.com/cortexlabs/cortex.git
3. ./cortex/dev/export_images.sh us-east-1 $AWS_ACCOUNT_ID # (I've set my account id as environment variable)

### Expected behavior
pull and push container images to my private ECR

### Actual behavior
./cortex/dev/export_images.sh: 15: set: Illegal option -o pipefail

### Screenshots
![Screenshot from 2022-01-25 14-18-09](https://user-images.githubusercontent.com/67038286/150915740-32fa9554-a18e-4ced-9ce2-3d9f259adf9f.png)

(optional)

### Stack traces

(error output from CloudWatch Insights or from a random pod `cortex logs <api name>`)

```text
<paste stack traces here>
```

### Additional context

(optional)

### Suggested solution

(optional)

skip creating ECR repository if already exists when exporting images to private ECR
### Description
skip creating ECR repository if already exists when exporting images to private ECR

### Motivation
to provide idempotency for the script

### Additional context

(optional)



Discussion / hints:

Fix patch:
diff --git a/dev/export_images.sh b/dev/export_images.sh
index 59d90d5c3b..ae897d7dd3 100755
--- a/dev/export_images.sh
+++ b/dev/export_images.sh
@@ -1,3 +1,5 @@
+#!/bin/bash
+
 # Copyright 2022 Cortex Labs, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,13 +32,23 @@ source_registry=quay.io/cortexlabs  # this can also be docker.io/cortexlabs
 
 destination_ecr_prefix="cortexlabs"
 destination_registry="${aws_account_id}.dkr.ecr.${ecr_region}.amazonaws.com/${destination_ecr_prefix}"
-aws ecr get-login-password --region $ecr_region | docker login --username AWS --password-stdin $destination_registry
+
+if [[ -f $HOME/.docker/config.json && $(cat $HOME/.docker/config.json | grep "ecr-login" | wc -l) -ne 0 ]]; then
+    echo "skipping docker login because you are using ecr-login with Amazon ECR Docker Credential Helper"
+else
+    aws ecr get-login-password --region $ecr_region | docker login --username AWS --password-stdin $destination_registry
+fi
 
 source $ROOT/build/images.sh
 
 # create the image repositories
 for image in "${all_images[@]}"; do
-    aws ecr create-repository --repository-name=$destination_ecr_prefix/$image --region=$ecr_region || true
+    repository_name=$destination_ecr_prefix/$image
+    if aws ecr describe-repositories --repository-names=$repository_name --region=$ecr_region >/dev/null 2>&1; then
+        echo "repository '$repository_name' already exists"
+    else
+        aws ecr create-repository --repository-name=$repository_name --region=$ecr_region | cat
+    fi
 done
 echo
 
diff --git a/docs/clusters/advanced/self-hosted-images.md b/docs/clusters/advanced/self-hosted-images.md
index 80ebca0700..cc84f54598 100644
--- a/docs/clusters/advanced/self-hosted-images.md
+++ b/docs/clusters/advanced/self-hosted-images.md
@@ -27,6 +27,8 @@ git clone --depth 1 --branch v$CORTEX_VERSION https://github.com/cortexlabs/cort
 
 Run the script below to export images to ECR in the same region and account as your cluster.
 
+The script will automatically create ECR Repositories with prefix `cortexlabs` if they don't already exist.
+
 Feel free to modify the script if you would like to export the images to a different registry such as a private docker hub.
 
 ```bash


Test patch: