The error you're encountering indicates that your current user lacks the necessary permissions to interact with the Docker daemon socket at /var/run/docker.sock. This socket is used for communication between the Docker client and the Docker daemon. By default, access to this socket is limited to the root user and members of the docker group for security reasons.
Here's how you can resolve this issue:
Option 1: Run the Command with sudo
You can execute your Docker command with sudo to run it with administrative privileges:
sudo docker compose up --build -d
However, using sudo with Docker commands can lead to permission issues, especially with files and directories created by your containers (e.g., when mounting volumes). These files might end up owned by root, causing access problems for your user account.
Option 2: Add Your User to the docker Group
A better long-term solution is to add your user to the docker group, which grants permission to use Docker without sudo.
Step-by-Step Instructions:
-
Create the docker Group (if it doesn't exist):
sudo groupadd docker
This command creates the docker group. It may return an error if the group already exists, which you can safely ignore.
-
Add Your User to the docker Group:
sudo usermod -aG docker $USER
This command appends your user ($USER) to the docker group.
-
Apply the New Group Membership:
You need to log out and log back in for the group membership changes to take effect. Alternatively, you can run:
newgrp docker
This command refreshes your group memberships without logging out.
-
Verify Docker Access:
Test that you can run Docker commands without sudo:
docker run hello-world
This should download and run the hello-world image, confirming you have the necessary permissions.
-
Retry Your Original Command:
Now, try running your Docker Compose command again:
docker compose up --build -d
Additional Information
-
Docker Compose Command Syntax:
Make sure you're using the correct Docker Compose command. With recent versions of Docker (Docker Engine v20.10.17 and later), Docker Compose is included as a Docker CLI plugin, and the correct syntax is docker compose (note the space). If you have the standalone Docker Compose installed (typically version 1.x), you might need to use docker-compose with a hyphen.
For Docker plugin (Docker Compose v2+):
docker compose up --build -d
For standalone Docker Compose (Docker Compose v1):
docker-compose up --build -d
-
Ensuring Correct Installation:
If you encounter issues with the docker compose command, ensure that you have Docker Compose installed. You can check your Docker Compose version with:
docker compose version
Or for the standalone version:
docker-compose version
-
Docker Documentation:
For more details, refer to the official Docker documentation on Post-installation steps for Linux.
By adding your user to the docker group, you should be able to run Docker commands without sudo, and the permission denied error should be resolved.
If you continue to experience issues, please let me know, and I can assist further.