Chapter 2 · Lesson 2 of 20
Docker

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.

By the end you will

Start a web server in a container, check it, read its logs, then stop and remove it.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • 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:

bash
docker run --name hi alpine echo hello
docker ps -a
docker rm hi

The 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#

bash
docker run -d --name web -p 8080:80 nginx
  • -d (detached) runs it in the background and prints the container ID.
  • --name web gives it a name you can use in later commands. Names must be unique on the host.
  • -p 8080:80 sends host port 8080 to container port 80. The order is host first, container second.
  • nginx is the image. With no tag, Docker uses latest.

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#

bash
curl http://localhost:8080

What you should see: the nginx welcome page as HTML, including the title “Welcome to nginx!”.

Step 3: Look around#

bash
docker ps
docker ps -a
docker logs web

docker 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#

bash
docker stop web
docker rm web

docker 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:80 listens 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:80 when 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 with docker rm web or pick another name.
  • The container exits immediately. The main process finished or crashed. docker ps -a shows Exited (0) for a clean finish or a non-zero code for an error, and docker logs NAME shows why.
  • Data in the container is temporary. Files written inside a container vanish when you docker rm it. 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.
  • latest is only a name. It may point at a different version next month. Pin a tag such as nginx:1.27 for anything you rely on.
  • Docker needs permission. A normal user not in the docker group gets a permission error on the Docker socket. Use sudo or add the user to the group (which gives that user root-level power on the host). See Install Docker on Ubuntu.

Troubleshooting#

SymptomLikely causeFix
Bind for 0.0.0.0:8080 failed: port is already allocatedAnother container or process uses host port 8080Choose another host port, or find the owner with sudo ss -ltnp
permission denied while trying to connect to the Docker daemon socketYour user cannot read the Docker socketUse sudo, or add your user to the docker group and log in again
Conflict, the container name is already in useA container with that name exists (running or stopped)docker rm -f web, or use a different name
curl: (7) Failed to connectContainer stopped, or wrong host portRun docker ps, check the PORTS column
Unable to find image then a pull errorNo internet, a typo in the image name, or a registry rate limitCheck the name and network, and run docker login if you hit a pull limit
cannot remove a running containerYou tried docker rm on a running containerdocker stop web first, or docker rm -f web
Docker says the container exited immediately with a non-zero codeThe main process crashed at start, often a bad config or missing variabledocker logs NAME, fix the cause, run again
Daemon error saying the container name is already in useA container with that name exists, even a stopped onedocker rm NAME, or choose another name
Page loads on the server but not from your laptopThe port is bound to 127.0.0.1, or a cloud firewall blocks itPublish on the right interface and open the port in the provider firewall
docker run hangs at pullingSlow or blocked network to the registryCheck 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#

TaskCommand
Run in background with a name and portdocker run -d --name web -p 8080:80 nginx
Run once, then delete the containerdocker run --rm alpine echo hello
List running / all containersdocker ps / docker ps -a
Read or follow logsdocker logs web / docker logs -f --tail 50 web
Stop, start, removedocker stop web, docker start web, docker rm web
List or remove imagesdocker images, docker rmi nginx

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.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda Play with Docker

Check what you learned4 questions
1What does -p 8080:80 do?
2After docker stop web and docker rm web, what is left on disk?
3What does -d do in docker run -d?
4docker run -p 8080:80 fails with port is already allocated. What does it mean?

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.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios