侧边栏壁纸
博主头像
996 Worker's Blog博主等级

祇園精舎の鐘の聲, 諸行無常の響き有り。

  • 累计撰写 212 篇文章
  • 累计创建 53 个标签
  • 累计收到 22 条评论

目 录CONTENT

文章目录

Resolving `dial unix /var/run/docker.sock: connect: permission denied` Issue on AWS Cloud9 IDE

996Worker
2023-11-16 / 0 评论 / 0 点赞 / 13 阅读 / 3426 字

Introduction

When working with Cloud9 IDE on an AWS EC2 instance, you might encounter a permission denied error (dial unix /var/run/docker.sock: connect: permission denied) while trying to access the Docker daemon socket. This blog post provides a solution to resolve this issue without restarting services or altering group memberships.

Problem

When I try to run a Lambda function in Cloud9 IDE with SAM, the error appeared as this:

2023-11-16 15:36:51 [INFO]: Starting Build inside a container

2023-11-16 15:36:51 [INFO]: Building codeuri: /home/ec2-user/test-aws-config runtime: python3.10 metadata: {} architecture: x86_64 functions: testawsconfig

2023-11-16 15:36:51 [INFO]: 
Build Failed

2023-11-16 15:36:51 [INFO]: Error: Docker is unreachable. Docker needs to be running to build inside a container.

2023-11-16 15:39:39 [INFO]: Preparing to debug locally: Lambda "lambda_function.lambda_handler"
2023-11-16 15:39:39 [INFO]: Building SAM application...
2023-11-16 15:39:41 [INFO]: Command: (not started) [/usr/local/bin/sam build --build-dir /tmp/aws-toolkit-vscode/vsctkEEjHOc/output --template /tmp/aws-toolkit-vscode/vsctkEEjHOc/app___vsctk___template.yaml --base-dir /home/ec2-user/test-aws-config --use-container --manifest /tmp/aws-toolkit-vscode/vsctkEEjHOc/debug-requirements.txt]
2023-11-16 15:39:45 [INFO]: Starting Build inside a container

2023-11-16 15:39:45 [INFO]: Building codeuri: /home/ec2-user/test-aws-config runtime: python3.10 metadata: {} architecture: x86_64 functions: testawsconfig

2023-11-16 15:39:45 [INFO]: 
Build Failed

2023-11-16 15:39:46 [INFO]: Error: Docker is unreachable. Docker needs to be running to build inside a container.

And also, if I run docker ps in terminal, the error info occured:

dial unix /var/run/docker.sock: connect: permission denied

This indicates that Cloud9 IDE user does not have the necessary permissions to access the Docker socket.

Solution: Using setfacl

The setfacl command can be used to modify the access control list for the Docker socket, granting specific permissions to a user.

This method is secure and doesn't require a system restart.

Step 1: Apply the setfacl Command

Run the following command in your terminal:

sudo setfacl --modify user:ec2-user:rw /var/run/docker.sock

This command grants the ec2-user read and write access to the Docker socket.

Step 2: Test the Configuration

After applying the command, test it by running a Docker command, such as:

docker ps

If the command executes successfully without permission errors, the issue is resolved.

Conclusion

Using setfacl to adjust file permissions is an efficient and secure way to resolve Docker permission issues on AWS EC2 instances. This approach is especially useful as it avoids altering group memberships or user ownerships and doesn't require system restarts.

0

评论区