Sub topic 7
Post-install steps on Linux
This chapter assumes you have installed Docker Engine on Ubuntu from the apt repository, as in the previous chapter. You should be comfortable with sudo, groups, and systemctl. By the end, you will run docker as a normal user and have the service start at boot.
Why the install is not finished yet
After the apt install, the daemon (dockerd) runs as root and listens on a Unix socket at /var/run/docker.sock. That socket is owned by root and by a group called docker. A normal user is in neither, so this fails:
docker psThe typical error is:
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sockRunning sudo docker ps works, but typing sudo every time is awkward. It also leads to files owned by root in your project directories. The post-install steps fix this.
Step 1: add your user to the docker group
The Ubuntu packages create the docker group during install. Add your account to it:
sudo usermod -aG docker $USERThe -a flag appends the group. Without it, -G replaces all your supplementary groups, which can remove you from sudo. Always use -aG together.
Step 2: apply the new group
Group membership is read when you log in. Your current shell does not know about the change yet. You have two options:
- Log out and log back in. This is the reliable choice, because every new process gets the new group.
- Run
newgrp dockerto start a new shell with the group active. This affects only that shell and its children.
newgrp docker
id -nGThe output of id -nG should include docker. If you are connected over SSH or use a terminal multiplexer such as tmux, existing sessions keep the old groups. Open a new session.
Step 3: enable the service at boot
On Ubuntu, the Docker packages usually start the service for you. Make this explicit so it also survives a reboot:
sudo systemctl enable --now docker.serviceenable creates the boot-time link, and --now starts the service immediately. Check the result:
systemctl is-enabled docker.service
systemctl is-active docker.serviceBoth commands should print their expected values: enabled and active.
Putting the steps together
The three steps together look like this:
sudo usermod -aG docker $USER
newgrp docker
sudo systemctl enable --now docker.serviceCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `permission denied` on the socket after `usermod` | Shell still has old groups | Log out and in, or run `newgrp docker` |
| `Cannot connect to the Docker daemon` | Service is not running | `sudo systemctl start docker.service` and read `journalctl -u docker` |
| `group 'docker' does not exist` | Package not installed, or a different install method | Install the packages, or create the group with `sudo groupadd docker` |
| You lost `sudo` rights after `usermod` | You omitted `-a` | Have another admin restore your groups, then use `-aG` |
Check your work
Run the following as your normal user, without sudo:
docker run hello-worldIf it prints the welcome message, the post-install steps worked. The next chapter covers verification in more detail.
Exercise
You ran sudo usermod -aG docker $USER in a terminal, but docker ps in that same terminal still reports a permission error on the socket. Give two ways to fix it without reinstalling anything, and say how you would confirm the fix.
Show answer
Log out and back in, or run newgrp docker to open a shell with the group active. Confirm with id -nG, which should list docker, and then run docker ps again without sudo. The shell failed because group membership is read at login, so a running shell keeps its old groups.
Exercise
Why is sudo usermod -G docker $USER (without -a) risky?
Show answer
-G sets the complete list of supplementary groups. Without -a, it replaces the existing list, so the user can be removed from groups such as sudo. Adding -a appends docker and keeps the other groups.