> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Docker Commands Cheat Sheet for Linux Servers

> Docker commands cheat sheet: run, list, stop and remove containers, read logs, open a shell, manage images and volumes, and check disk use, with examples.

The Docker commands you need most on a server are `docker run -d` to start a container, `docker ps -a` to list containers, `docker logs -f <name>` to read output, `docker exec -it <name> sh` to open a shell, `docker stop` and `docker rm` to stop and remove, and `docker system df` to see disk use. Each table below gives the command, what it does and an example.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | Reference | Any Linux server with Docker Engine | [Docker installed](https://help.freakhosting.com/portals/other/how-to-install-docker.md) |

## Before you start

* Run the commands as a user in the `docker` group, or put `sudo` in front.
* You can use a container's **name** (for example `web`) or the first few characters of its **ID** wherever a command needs `<name>`.
* An **image** is the read-only template. A **container** is a running (or stopped) copy of an image. A **volume** is storage that survives when you replace the container.

## How to run a container

```bash theme={null}
docker run -d --name web --restart unless-stopped -p 8080:80 -v web-data:/usr/share/nginx/html nginx:latest
```

| Flag | Meaning |
| - | - |
| `-d` | Run in the background (detached) |
| `--name web` | Give the container a fixed name |
| `--restart unless-stopped` | Start again after a crash or reboot, unless you stopped it. The default is `no`. |
| `-p 8080:80` | Publish container port 80 on server port 8080 |
| `-p 127.0.0.1:8080:80` | Publish on localhost only, for use behind a reverse proxy |
| `-v web-data:/path` | Mount the named volume `web-data` at `/path` in the container |
| `-v /srv/web:/path` | Mount a folder from the server (bind mount) |
| `-e TZ=Europe/London` | Set an environment variable |
| `--env-file .env` | Load environment variables from a file |
| `--memory 512m --cpus 1` | Limit memory and CPU |
| `--rm` | Delete the container when it exits (for one-off tasks) |

> [!WARNING]
>
> Ports published with `-p` bypass `ufw` and `firewalld`. Anything you publish on `0.0.0.0` is reachable from the internet. Use `127.0.0.1:` for admin tools and databases.

## Container commands

| Task | Command |
| - | - |
| List running containers | `docker ps` |
| List all containers, including stopped | `docker ps -a` |
| Stop a container | `docker stop web` |
| Start a stopped container | `docker start web` |
| Restart a container | `docker restart web` |
| Remove a stopped container | `docker rm web` |
| Stop and remove in one step | `docker rm -f web` |
| Live CPU, memory and network use | `docker stats` (one snapshot: `docker stats --no-stream`) |
| Full details as JSON | `docker inspect web` |
| One field, for example the status | `docker inspect -f '{{.State.Status}}' web` |
| Change the restart policy | `docker update --restart unless-stopped web` |

Example output of `docker ps`:

```text theme={null}
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                    NAMES
730560ce8d25   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   127.0.0.1:8080->80/tcp   web
```

## How to read container logs

| Task | Command |
| - | - |
| Show all logs | `docker logs web` |
| Follow new lines (Ctrl+C to stop) | `docker logs -f web` |
| Last 100 lines | `docker logs --tail 100 web` |
| Logs from the last hour | `docker logs --since 1h web` |

## How to open a shell inside a container

```bash theme={null}
docker exec -it web sh
```

Use `bash` instead of `sh` if the image has it. Type `exit` to leave; the container keeps running. To run a single command without a shell:

```bash theme={null}
docker exec web cat /etc/os-release
```

## Image commands

| Task | Command |
| - | - |
| List images | `docker images` |
| Download or update an image | `docker pull nginx:latest` |
| Remove an image | `docker rmi nginx:latest` |
| Remove dangling (untagged) images | `docker image prune` |
| Remove all images no container uses | `docker image prune -a` |
| Build an image from a Dockerfile | `docker build -t myapp:1.0 .` |

## How to update a container to a new image

A container never changes its image. To update, pull the new image and create a new container with the same options:

```bash theme={null}
docker pull nginx:latest
docker stop web
docker rm web
docker run -d --name web --restart unless-stopped -p 8080:80 -v web-data:/usr/share/nginx/html nginx:latest
```

Data in volumes stays. Data written inside the container (not in a volume) is lost. [Docker Compose](https://help.freakhosting.com/portals/other/how-to-use-docker-compose.md) makes this a two-command job.

## Volume and network commands

| Task | Command |
| - | - |
| List volumes | `docker volume ls` |
| Create a volume | `docker volume create web-data` |
| Show where a volume is stored | `docker volume inspect web-data` |
| Remove a volume | `docker volume rm web-data` |
| List networks | `docker network ls` |
| Create a network | `docker network create appnet` |
| Connect a container to a network | `docker network connect appnet web` |

Containers on the same user-defined network reach each other by container name.

## Disk space and clean-up

| Task | Command |
| - | - |
| Summary of disk use | `docker system df` |
| Per image, container and volume | `docker system df -v` |
| Remove stopped containers | `docker container prune` |
| Remove stopped containers, unused networks, dangling images and build cache | `docker system prune` |

Example output of `docker system df`:

```text theme={null}
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         2         1.2GB     640MB (53%)
Containers      2         2         12kB      0B (0%)
Local Volumes   3         2         350MB     40MB (11%)
Build Cache     0         0         0B        0B
```

> [!WARNING]
>
> `docker volume prune -a` and `docker rm -v` delete volume data, including databases. See [clean up Docker disk space](https://help.freakhosting.com/portals/other/how-to-clean-up-docker-disk-space.md) for what each prune command removes.

## Frequently asked questions

### What is the difference between docker stop and docker kill?

`docker stop` asks the main process to shut down and waits, by default 10 seconds, before forcing it. `docker kill` ends it at once, which can corrupt data that is being written.

### Why does my container exit straight away?

The main process ended or crashed. Run `docker ps -a` to see the exit code and `docker logs <name>` to see the error.

### How do I copy a file into or out of a container?

Use `docker cp`, for example `docker cp web:/etc/nginx/nginx.conf ./nginx.conf` or `docker cp ./index.html web:/usr/share/nginx/html/`.

### How do I see which ports a container publishes?

Run `docker port web`. It prints lines such as `80/tcp -> 127.0.0.1:8080`.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Install Docker](https://help.freakhosting.com/portals/other/how-to-install-docker.md)
>
> Docker Engine from the official repository.

> [card] [Docker Compose tutorial](https://help.freakhosting.com/portals/other/how-to-use-docker-compose.md)
>
> Define containers in one file.

> [card] [Clean up Docker disk space](https://help.freakhosting.com/portals/other/how-to-clean-up-docker-disk-space.md)
>
> What each prune command deletes.

> [card] [Install Portainer](https://help.freakhosting.com/portals/other/how-to-install-portainer.md)
>
> The same tasks in a web UI.

***

> [!NOTE]
>
> **Checked against the official [Docker CLI reference](https://docs.docker.com/reference/cli/docker/)** on 4 November 2026; commands tested on Ubuntu 24.04 LTS with Docker Engine 29.9.2. Output can differ slightly between Docker versions.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.