Skip to content

Commit 4604d97

Browse files
committed
Optimized the backend Docker build logic.
1 parent 52df47c commit 4604d97

File tree

7 files changed

+62
-34
lines changed

7 files changed

+62
-34
lines changed

e2b-setup-env-existing-vpc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ Resources:
749749
snap install go --classic
750750
- |
751751
# Install essential development and deployment tools
752-
apt install unzip docker.io make jq postgresql-client-common -y
752+
apt install unzip docker.io make jq postgresql-client-common git -y
753753
- |
754754
# Install PostgreSQL client for database management
755755
apt install postgresql-client -y

e2b-setup-env.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ Resources:
873873
snap install go --classic
874874
- |
875875
# Install essential development and deployment tools
876-
apt install unzip docker.io make jq postgresql-client-common -y
876+
apt install unzip docker.io make jq postgresql-client-common git -y
877877
- |
878878
# Install PostgreSQL client for database management
879879
apt install postgresql-client -y

infra-iac/packer/main.pkr.hcl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ build {
6262
"sudo systemctl stop apt-daily.service apt-daily-upgrade.service unattended-upgrades.service || true",
6363
"sudo systemctl kill apt-daily.service apt-daily-upgrade.service unattended-upgrades.service || true",
6464
"sudo systemctl disable apt-daily.timer apt-daily-upgrade.timer || true",
65+
"sudo systemctl mask apt-daily.service apt-daily-upgrade.service unattended-upgrades.service || true",
66+
"sudo killall -9 apt-get apt dpkg unattended-upgr 2>/dev/null || true",
6567
"echo 'Waiting for apt/dpkg locks to be released...'",
66-
"for i in $(seq 1 60); do if sudo fuser /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock >/dev/null 2>&1; then echo \"Lock held, waiting... ($i/60)\"; sleep 5; else echo 'Locks released.'; break; fi; done",
67-
"sleep 2"
68+
"for i in $(seq 1 60); do if sudo fuser /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock /var/cache/apt/archives/lock >/dev/null 2>&1; then echo \"Lock held, waiting... ($i/60)\"; sleep 5; else echo 'Locks released.'; break; fi; done",
69+
"sleep 5"
6870
]
6971
}
7072

@@ -74,6 +76,7 @@ build {
7476
"DEBCONF_NONINTERACTIVE_SEEN=true"
7577
]
7678
inline = [
79+
"for i in $(seq 1 30); do if sudo fuser /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock /var/cache/apt/archives/lock >/dev/null 2>&1; then echo \"Apt lock held, waiting... ($i/30)\"; sleep 5; else break; fi; done",
7780
"sudo -E apt-get clean",
7881
"sudo -E apt-get update -y",
7982
"sudo -E apt-get upgrade -y",

infra-iac/terraform/scripts/start-api.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ net.core.netdev_max_backlog = 65535
3636
3737
# Increase maximum number of TCP sockets
3838
net.ipv4.tcp_max_syn_backlog = 65535
39+
40+
# Reserve static service ports from being used as ephemeral ports
41+
net.ipv4.ip_local_reserved_ports = 50001
3942
EOF
4043
sudo sysctl -p
4144

infra-iac/terraform/scripts/start-build-cluster.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ net.core.netdev_max_backlog = 65535
2727
2828
# Increase maximum number of TCP sockets
2929
net.ipv4.tcp_max_syn_backlog = 65535
30+
31+
# Reserve static service ports from being used as ephemeral ports
32+
net.ipv4.ip_local_reserved_ports = 44313,50001
3033
EOF
3134
sudo sysctl -p
3235

infra-iac/terraform/scripts/start-client.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ net.ipv4.tcp_max_syn_backlog = 65535
195195
# Increase the maximum number of memory map areas
196196
vm.max_map_count=1048576
197197
198+
# Reserve static service ports from being used as ephemeral ports
199+
net.ipv4.ip_local_reserved_ports = 44313,50001
200+
198201
EOF
199202
sudo sysctl -p
200203

packages/shared/pkg/artifacts-registry/registry_aws.go

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,27 @@ func (g *AWSArtifactsRegistry) CopyImage(ctx context.Context, sourceRef string,
121121
return fmt.Errorf("failed to parse source image reference '%s': %w", sourceRef, err)
122122
}
123123

124-
// 2. Get ECR auth
125-
auth, err := g.getAuthToken(ctx)
126-
if err != nil {
127-
return fmt.Errorf("failed to get ECR auth token: %w", err)
128-
}
129-
130-
// 3. Fetch source image
131-
img, err := remote.Image(src, remote.WithAuth(auth))
132-
if err != nil {
133-
return fmt.Errorf("failed to fetch source image '%s': %w", sourceRef, err)
124+
// 2. Fetch source image — use auth only for private ECR, anonymous for public sources
125+
var img containerregistry.Image
126+
if isPrivateECR(sourceRef) {
127+
auth, err := g.getAuthToken(ctx)
128+
if err != nil {
129+
return fmt.Errorf("failed to get ECR auth token: %w", err)
130+
}
131+
img, err = remote.Image(src, remote.WithAuth(auth))
132+
if err != nil {
133+
return fmt.Errorf("failed to fetch source image '%s': %w", sourceRef, err)
134+
}
135+
} else {
136+
// Public images (Docker Hub, public.ecr.aws, etc.) — anonymous pull
137+
var fetchErr error
138+
img, fetchErr = remote.Image(src)
139+
if fetchErr != nil {
140+
return fmt.Errorf("failed to fetch source image '%s': %w", sourceRef, fetchErr)
141+
}
134142
}
135143

136-
// 4. Ensure target ECR repository exists
144+
// 3. Ensure target ECR repository exists
137145
targetRepoName := fmt.Sprintf("%s/%s", g.repositoryName, templateId)
138146
if err := g.ensureRepository(ctx, targetRepoName); err != nil {
139147
return fmt.Errorf("failed to ensure target repository: %w", err)
@@ -150,8 +158,12 @@ func (g *AWSArtifactsRegistry) CopyImage(ctx context.Context, sourceRef string,
150158
return fmt.Errorf("failed to parse target reference '%s': %w", targetTag, err)
151159
}
152160

153-
// 6. Write image to target
154-
if err := remote.Write(dst, img, remote.WithAuth(auth)); err != nil {
161+
// 6. Write image to target (always use ECR auth for the destination)
162+
pushAuth, err := g.getAuthToken(ctx)
163+
if err != nil {
164+
return fmt.Errorf("failed to get ECR auth token for push: %w", err)
165+
}
166+
if err := remote.Write(dst, img, remote.WithAuth(pushAuth)); err != nil {
155167
return fmt.Errorf("failed to write image to '%s': %w", targetTag, err)
156168
}
157169

@@ -177,30 +189,34 @@ func (g *AWSArtifactsRegistry) ensureRepository(ctx context.Context, repoName st
177189
return nil
178190
}
179191

180-
// resolveSourceRef resolves a short image name (e.g. "e2bdev/desktop") to a full
181-
// ECR URI by prepending the registry domain. If the reference already contains a
192+
// resolveSourceRef resolves image references. If the reference already contains a
182193
// registry domain (detected by a "." in the first path component), it is returned as-is.
194+
// Short names without a domain are treated as Docker Hub images.
183195
func (g *AWSArtifactsRegistry) resolveSourceRef(ctx context.Context, sourceRef string) (string, error) {
184196
parts := strings.SplitN(sourceRef, "/", 2)
185-
if strings.Contains(parts[0], ".") {
186-
// Already has a registry domain
197+
// Strip tag/digest before checking for domain dots
198+
host := strings.SplitN(parts[0], ":", 2)[0]
199+
if strings.Contains(host, ".") {
200+
// Already has a registry domain (e.g. public.ecr.aws/..., 918xxx.dkr.ecr.../...)
187201
return sourceRef, nil
188202
}
189203

190-
// Get ECR registry URL from auth token
191-
res, err := g.client.GetAuthorizationToken(ctx, &ecr.GetAuthorizationTokenInput{})
192-
if err != nil {
193-
return "", fmt.Errorf("failed to get ECR registry URL: %w", err)
194-
}
195-
if len(res.AuthorizationData) == 0 || res.AuthorizationData[0].ProxyEndpoint == nil {
196-
return "", fmt.Errorf("no ECR proxy endpoint found")
204+
// No domain → Docker Hub image
205+
if len(parts) == 1 {
206+
// Official image like "ubuntu:22.04" → "docker.io/library/ubuntu:22.04"
207+
nameAndTag := strings.SplitN(sourceRef, ":", 2)
208+
if len(nameAndTag) == 2 {
209+
return fmt.Sprintf("docker.io/library/%s:%s", nameAndTag[0], nameAndTag[1]), nil
210+
}
211+
return fmt.Sprintf("docker.io/library/%s:latest", sourceRef), nil
197212
}
213+
// User image like "myuser/myrepo:tag" → "docker.io/myuser/myrepo:tag"
214+
return fmt.Sprintf("docker.io/%s", sourceRef), nil
215+
}
198216

199-
// ProxyEndpoint is "https://918380168589.dkr.ecr.us-west-2.amazonaws.com"
200-
registryURL := strings.TrimPrefix(*res.AuthorizationData[0].ProxyEndpoint, "https://")
201-
registryURL = strings.TrimPrefix(registryURL, "http://")
202-
203-
return fmt.Sprintf("%s/%s", registryURL, sourceRef), nil
217+
// isPrivateECR checks if the image reference points to a private ECR registry.
218+
func isPrivateECR(ref string) bool {
219+
return strings.Contains(ref, ".dkr.ecr.") && strings.Contains(ref, ".amazonaws.com")
204220
}
205221

206222
func (g *AWSArtifactsRegistry) getAuthToken(ctx context.Context) (*authn.Basic, error) {
@@ -232,4 +248,4 @@ func (g *AWSArtifactsRegistry) getAuthToken(ctx context.Context) (*authn.Basic,
232248
Username: username,
233249
Password: password,
234250
}, nil
235-
}
251+
}

0 commit comments

Comments
 (0)