Run your first Docker container (docker run)
A container is a running instance of an image; docker run pulls the image if needed and starts the container.
- Updated Nov 27, 2026
- Markdown
Start a web server in a container, check it, read its logs, then stop and remove it.
- Docker installed
Run a container with docker run IMAGE: Docker pulls the image if it is missing, creates a container from it and starts the image’s main process. Add -d to keep it in the background and -p HOST:CONTAINER to reach it from the host. Most first-day problems come from an exited container, a taken name or a busy port, and all three are easy to read once you know what the command did.
How it works#
An image is a read-only bundle of files plus a default command. docker run does three things in one go. If the image is not on the machine, it pulls it from a registry (Docker Hub by default). It then creates a container: a thin writable layer on top of the image, plus an isolated view of processes, network and filesystem. Finally it starts the image’s default command inside that container.
A container is not a small virtual machine. It is one normal process (and its children) on the host’s kernel, fenced in by the kernel’s namespaces and resource limits. When that main process exits, the container stops. That is why a container running nginx stays up (nginx keeps running) while one running echo hello stops instantly.
Containers have their own network address, so a server listening on port 80 inside is not reachable from the host by default. -p HOST:CONTAINER tells Docker to forward traffic from a host port to a container port. Stopping a container keeps it on disk (with its writable layer and logs) until you remove it. Removing a container never removes the image.
A worked example, start to finish#
Run a container that does one thing and exits:
docker run --name hi alpine echo hello
docker ps -a
docker rm hiThe first command pulls alpine if needed, creates a container named hi, runs echo hello inside it and prints hello. The process finished, so the container stopped. docker ps -a lists it with a status like Exited (0) ... ago, where 0 is the exit code of echo. docker rm hi deletes the stopped container, and the alpine image stays on disk.
Now compare nginx. Its main process is a server that never finishes, so the container keeps running until you stop it. Same command, different main process, different lifetime.
Step 1: Start a web server#
docker run -d --name web -p 8080:80 nginx-d(detached) runs it in the background and prints the container ID.--name webgives it a name you can use in later commands. Names must be unique on the host.-p 8080:80sends host port 8080 to container port 80. The order is host first, container second.nginxis the image. With no tag, Docker useslatest.
What you should see: on the first run, lines such as “Unable to find image ‘nginx:latest’ locally” and “Pulling from library/nginx”, then a long container ID.
Step 2: Check that it answers#
curl http://localhost:8080What you should see: the nginx welcome page as HTML, including the title “Welcome to nginx!”.
Step 3: Look around#
docker ps
docker ps -a
docker logs webdocker ps lists running containers. -a also lists stopped ones. docker logs prints whatever the main process wrote to standard output and error. Add -f to follow new lines, and --tail 50 to see only the last 50.
What you should see: web with status “Up …” and ports like 0.0.0.0:8080->80/tcp, then nginx access lines in the logs after your curl.
Step 4: Stop and remove it#
docker stop web
docker rm webdocker stop asks the main process to exit with a termination signal, and force-kills it if it has not exited after a grace period (10 seconds by default). docker rm deletes the stopped container. The image stays on disk, so the next docker run is fast. List images with docker images.
What you should see: each command prints the container name.
Check it worked#
After step 1, docker ps shows web as Up and curl http://localhost:8080 returns HTML. After step 4, docker ps -a no longer lists web, but docker images still lists nginx. If curl now fails with “Connection refused”, that is correct: nothing is listening any more.
Edge cases and gotchas#
- A published port can be reachable from the internet.
-p 8080:80listens on all host interfaces. Docker writes its own firewall rules, so published ports bypass ufw rules (see publish container ports safely). Use-p 127.0.0.1:8080:80when only the host (or a reverse proxy on it) should connect. - A name can only be used once. Running
docker run --name web ...a second time fails with a conflict saying the name is already in use, even if the old container is stopped. Remove it withdocker rm webor pick another name. - The container exits immediately. The main process finished or crashed.
docker ps -ashowsExited (0)for a clean finish or a non-zero code for an error, anddocker logs NAMEshows why. - Data in the container is temporary. Files written inside a container vanish when you
docker rmit. Use a volume for anything you need to keep. - Architecture mismatch. On an ARM machine (Apple silicon, some cloud servers), an image built only for amd64 can fail with “no matching manifest for linux/arm64/v8”. Pick an image that publishes an arm64 variant.
latestis only a name. It may point at a different version next month. Pin a tag such asnginx:1.27for anything you rely on.- Docker needs permission. A normal user not in the
dockergroup gets a permission error on the Docker socket. Usesudoor add the user to the group (which gives that user root-level power on the host). See Install Docker on Ubuntu.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
Bind for 0.0.0.0:8080 failed: port is already allocated | Another container or process uses host port 8080 | Choose another host port, or find the owner with sudo ss -ltnp |
permission denied while trying to connect to the Docker daemon socket | Your user cannot read the Docker socket | Use sudo, or add your user to the docker group and log in again |
| Conflict, the container name is already in use | A container with that name exists (running or stopped) | docker rm -f web, or use a different name |
curl: (7) Failed to connect | Container stopped, or wrong host port | Run docker ps, check the PORTS column |
Unable to find image then a pull error | No internet, a typo in the image name, or a registry rate limit | Check the name and network, and run docker login if you hit a pull limit |
cannot remove a running container | You tried docker rm on a running container | docker stop web first, or docker rm -f web |
| Docker says the container exited immediately with a non-zero code | The main process crashed at start, often a bad config or missing variable | docker logs NAME, fix the cause, run again |
| Daemon error saying the container name is already in use | A container with that name exists, even a stopped one | docker rm NAME, or choose another name |
| Page loads on the server but not from your laptop | The port is bound to 127.0.0.1, or a cloud firewall blocks it | Publish on the right interface and open the port in the provider firewall |
docker run hangs at pulling | Slow or blocked network to the registry | Check DNS and outbound access, then retry |
Real-world scenario#
A teammate asks you to try a static site on a fresh server. You log in, run docker run -d --name site -p 127.0.0.1:8080:80 nginx and check with curl http://localhost:8080: the nginx welcome page comes back. From your laptop the same URL fails, which is what you want, because the port is bound to loopback only.
Later the teammate asks to see it. Instead of opening the port to the world you tunnel it: ssh -L 8080:127.0.0.1:8080 you@203.0.113.10, and the page opens at http://localhost:8080 on the laptop. When the review is over you run docker stop site and docker rm site. The server is clean again except for the cached nginx image, which makes the next demo start in a second.
Quick reference#
| Task | Command |
|---|---|
| Run in background with a name and port | docker run -d --name web -p 8080:80 nginx |
| Run once, then delete the container | docker run --rm alpine echo hello |
| List running / all containers | docker ps / docker ps -a |
| Read or follow logs | docker logs web / docker logs -f --tail 50 web |
| Stop, start, remove | docker stop web, docker start web, docker rm web |
| List or remove images | docker images, docker rmi nginx |
Related lessons#
- Install Docker on Ubuntu: the setup this lesson assumes
- Run commands inside a container: open a shell in a running container
- Publish container ports safely: the full story behind
-pand the firewall - Docker volumes and persistent data: keep data after
docker rm
Next#
Describe several containers in one file with Docker Compose basics.
Try it: Run your first container
Start nginx, test it, read its logs, then stop and remove it.
Want a real machine? Free browser labs: Killercoda Play with Docker
Check what you learned
Frequently asked questions
What is the difference between an image and a container?
An image is the packaged files and settings. A container is a running copy of an image. You can start many containers from one image.
Why did my data disappear when I removed the container?
A container's own filesystem goes away with it. Keep data in a volume or a bind mount.
Why does my container exit right after it starts?
A container lives only as long as its main process. If that process finishes or crashes, the container stops. Read docker logs NAME to see why.
Can I reach the container from another computer?
Only if you publish the port on a public interface, and Docker's published ports can bypass ufw rules. Bind to 127.0.0.1 for local-only access.
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.