Docker Setup Ubuntu

I checked this post. Here’s what’s on it right now, and what needs fixing:

Current state:- It’s a chunk of raw Docker commands — install, access container, install web service, and a small “Information” command reference. No intro, no explanation of what each command does, no context on why you’d use Docker or when to pick one method over another. This is the same “thin content” issue as before — commands without explanation.

What you should do:- Rewrite it with actual explanation around the commands. Here’s ready-to-paste content:

How to Set Up Docker on Ubuntu

Docker lets you run applications inside lightweight, isolated containers — useful for testing software, running web servers, or deploying apps without messing with your main system. This guide covers installing Docker, managing containers, and useful day-to-day commands.

Step 1: Install Docker

sudo apt install docker.io
sudo systemctl start docker.service
sudo systemctl enable docker.service

This installs Docker from Ubuntu’s default repository and starts the service, setting it to launch automatically on boot.

Step 2: Access a Running Container

sudo docker exec -it container_name /bin/bash

This opens a terminal session inside a running container, letting you run commands as if you were logged into that isolated environment directly. Replace `container_name` with your actual container’s name or ID (find it with `docker ps`).

Step 3 (Alternative): Install Docker via Official Repository

For the latest stable Docker version instead of Ubuntu’s default package, add Docker’s official repository:

sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

This method is recommended if you need newer Docker features not yet available in Ubuntu’s default package.

Example: Run a Web Server in Docker

sudo docker run -dit –name linux-guru -p 8080:80 -v /home/user/website/:/usr/local/apache2/htdocs/ httpd:2.4

This runs an Apache web server container in the background, mapping port 8080 on your host to port 80 in the container, and linking a local folder to serve your website files from.

Useful Docker Commands

Command | What it does

sudo docker compose pull` | Downloads the latest images defined in your compose file |
sudo docker compose up -d` | Starts containers in the background |
sudo docker-compose ps` | Shows status of running containers |
sudo docker rm container_name` | Removes a stopped container |
docker stop $(docker ps -a -q)` | Stops all running containers |
docker rm $(docker ps -a -q)` | Deletes all stopped containers |

Common issues:-

If `docker.io` install fails, run `sudo apt update` first
“Permission denied” errors usually mean you need `sudo`, or need to add your user to the `docker` group: `sudo usermod -aG docker $USER` (then log out and back in)