On this page
- How do you check that Docker is ready?
- What do the main docker run options mean?
- Why publish ports on 127.0.0.1?
- How do you list and inspect containers?
- How do you run commands inside a container?
- How do you stop, start, restart and remove containers?
- Which restart policy fits a service?
- How do you limit memory and CPU?
- Questions
How do you check that Docker is ready?
Run a version check and the tiny test image; if both work, the engine is installed and your user can talk to it. A fresh server needs the engine first, which the Docker Engine tutorial installs, or you can deploy the Docker image from the images page.
docker version
sudo systemctl is-active docker
docker run --rm hello-worldIf you see a permission error on /var/run/docker.sock, either prefix the commands with sudo or add your user to the docker group and log in again. That group grants root-equivalent power, so give it only to trusted accounts.
What do the main docker run options mean?
A docker run line creates a container from an image and starts it. The options you will use most often are:
-d: run in the background and print the container id.--name web: a fixed name, so later commands do not need the id.-p 127.0.0.1:8080:80: host address and port, then container port.-e KEY=valueor--env-file app.env: environment variables.-v name:/path: persistent storage, explained in the volumes guide.--restart unless-stopped: come back after crashes and reboots.--rm: delete the container when it exits, handy for one-off tasks.-it: interactive terminal, for shells and tools.
docker run -d --name web --restart unless-stopped \
-p 127.0.0.1:8080:80 \
-v /srv/web/html:/usr/share/nginx/html:ro \
nginx:stable
curl -I http://127.0.0.1:8080Options go before the image name; anything after it is passed to the container as its command, for example docker run --rm alpine:3.20 cat /etc/os-release.
Why publish ports on 127.0.0.1?
A port published without an address listens on every interface, and Docker inserts its own forwarding rules that are checked before your ufw or firewalld policy. In practice -p 8080:80 is open to the whole internet even if the firewall claims otherwise. Binding to 127.0.0.1 means only processes on the server can connect, which is right for anything that sits behind Nginx or Caddy:
sudo ss -tlnp | grep docker-proxy
docker port webFor IPv6, add a second mapping such as -p "[::1]:8080:80" if the proxy connects over the IPv6 loopback. Publish a public port directly only when the service is meant to be reachable, such as a game server, and protect it in the application. A ready HTTPS front end is described in the Caddy HTTPS tutorial, and general firewall setup in the firewall tutorial.
How do you list and inspect containers?
docker ps shows running containers; -a includes stopped ones with their exit codes. Custom formats keep the output readable on a narrow terminal:
docker ps
docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
docker inspect web --format '{{.State.Status}} {{.RestartCount}}'
docker stats --no-stream
docker top webdocker stats prints live CPU, memory and network use per container, a quick way to see which one is busy. For history and log handling, read the Docker logs guide.
How do you run commands inside a container?
Use docker exec to start an extra process in a running container. For a shell, add -it and choose the shell the image has (bash on Debian based images, sh on Alpine and many slim ones):
docker exec -it web sh
docker exec web nginx -t
docker exec -u root -it web sh
docker cp web:/etc/nginx/nginx.conf ./nginx.confChanges made this way live only in that container and vanish when it is recreated, so treat exec as a tool for checking and debugging. Lasting changes go into a mounted file or into the image, see building images.
How do you stop, start, restart and remove containers?
docker stop sends SIGTERM, waits ten seconds by default, then forces the process down. Give slow services more time with -t:
docker stop web
docker stop -t 30 db
docker start web
docker restart web
docker rm web # only a stopped container
docker rm -f web # stop and remove at onceRemoving a container deletes its writable layer but not named volumes or bind mounts. To change options such as ports or variables, remove the container and run it again with the new line; there is no edit command for those settings. Writing the line into a Compose file saves you from retyping it.
Which restart policy fits a service?
The policy decides what Docker does when the process exits or the server boots:
no: default, never restart.on-failure[:N]: restart only after a non-zero exit, up to N times.always: restart on every exit, and start at boot even if you stopped it manually before.unless-stopped: like always, but a manual stop is remembered across reboots.
docker update --restart unless-stopped web
docker inspect web --format '{{.HostConfig.RestartPolicy.Name}}'
sudo systemctl enable dockerThe docker update command changes the policy in place without recreating the container. Boot-time restarts need the Docker service itself to start at boot, hence the last command.
How do you limit memory and CPU?
Set caps so one misbehaving container cannot starve the rest of the server. Limits can be given at run time or changed later:
docker run -d --name worker --memory 512m --cpus 1.0 myimage:1.0
docker update --memory 768m --memory-swap 768m --cpus 1.5 worker
docker stats --no-stream workerA container that goes over its memory limit is killed by the kernel and, with a restart policy, started again; docker inspect worker --format '{{.State.OOMKilled}}' tells you whether that happened. If the whole server is short of resources, a bigger plan is the real fix, see moving to a larger plan.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Run and manage containers
What is the difference between docker run and docker start?
docker run creates a new container from an image. docker start starts an existing, stopped container with the settings it was created with.
Why does my container exit right away?
The main process finished or crashed. Read docker logs NAME and the exit code in docker ps -a. Images that run a shell exit at once without -it.
How do I reach a container from another container?
Put both on the same user-defined network (docker network create appnet, then --network appnet) and use the container name as the host name.
Do containers survive a server restart?
Containers with always or unless-stopped start again when Docker starts at boot. Others stay stopped until you start them.
Is publishing on 0.0.0.0 ever fine?
Yes, for services meant to be public, such as a game server port. Make sure the application itself is protected, because the host firewall does not filter Docker published ports by default.
How do I see which image version a container runs?
Run docker inspect NAME --format '{{.Config.Image}}' for the tag and docker inspect NAME --format '{{.Image}}' for the exact image id.
More in Containers and PaaS
- Docker Compose stacks
- Docker volumes and bind mounts
- Read and limit Docker logs
- Free disk space used by Docker
- Build, tag and push Docker images
- Podman and rootless containers
- Docker Swarm across servers
- Automatic updates with Watchtower
- Portainer CE web UI
- Dokploy self-hosted PaaS
- CapRover PaaS
- Dokku git push deploys
- Coolify on your own server