Skip to content
Laviyx Learn

Try

Docker and Kubernetes course menu

docker run Explained: Your First Containers

Run containers with docker run: pull images, run commands, open a shell, run a web server in the background, publish ports, name containers and clean up.

Beginner5 min readUpdated
On this page

Run a command in a container#

Terminal
docker run alpine:3.24 echo "Hello from a container"
Output
Unable to find image 'alpine:3.24' locally
3.24: Pulling from library/alpine
Digest: sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
Status: Downloaded newer image for alpine:3.24
Hello from a container

Here is what happened:

  1. Docker looked for the image alpine:3.24 locally, and did not find it.
  2. It downloaded the image from Docker Hub.
  3. It created a container and ran echo inside it.
  4. The command finished, so the container stopped.

Alpine is a very small Linux distribution, often used in containers. Run it again, and it starts at once, because the image is now stored locally:

Terminal
docker run alpine:3.24 cat /etc/os-release | head -n 2
Output
NAME="Alpine Linux"
ID=alpine

A container is isolated#

A container has its own file system, processes and network. It sees only what is inside it:

Terminal
docker run --rm alpine:3.24 ps
docker run --rm alpine:3.24 ls /home
Output
PID   USER     TIME  COMMAND
    1 root      0:00 ps

Only one process is running, and your files are not there. --rm removes the container when it exits, which keeps things tidy.

An interactive shell#

-i keeps input open, and -t gives you a terminal. Together, they let you work inside a container:

Terminal
docker run -it --rm alpine:3.24 sh

You get a prompt inside the container. Run any commands, and type exit to leave. Most images based on Ubuntu or Debian have bash, while Alpine has only sh.

Run a web server#

-d (detached) runs a container in the background. -p publishes a port, in the form host:container. --name gives it a name:

Terminal
docker run -d --name web -p 8080:80 nginx:1.30-alpine > /dev/null
sleep 2
curl -sI http://localhost:8080 | head -n 2
Output
Unable to find image 'nginx:1.30-alpine' locally
1.30-alpine: Pulling from library/nginx
Digest: sha256:0e52bd054126cdc040b1276c0af347aeb02a41b9cfe305d6094767ac30e97949
Status: Downloaded newer image for nginx:1.30-alpine
HTTP/1.1 200 OK
Server: nginx/1.30.6

Nginx is listening on port 80 inside the container, and Docker forwards port 8080 on your computer to it. Open http://localhost:8080 in your browser to see the welcome page.

Text
Browser --> localhost:8080 --> [ container port 80: nginx ]

See what is running#

Terminal
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
Output
NAMES     IMAGE               STATUS         PORTS
web       nginx:1.30-alpine   Up 2 seconds   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp

Plain docker ps works too, with more columns. Read the container's logs, which show the request we just made:

Terminal
docker logs web 2>&1 | tail -n 1 | cut -c1-70
Output
172.17.0.1 - - [05/Nov/2026:07:04:43 +0000] "HEAD / HTTP/1.1" 200 0 "-

Stop and remove it#

Terminal
docker stop web
docker rm web
docker ps -a --format '{{.Names}}' | grep -c web
Output
web
web
0

docker rm -f web does both in one step.

Common docker run options#

OptionMeaning
-dRun in the background
-itInteractive terminal
--rmDelete the container when it exits
--name webGive it a name
-p 8080:80Publish a port: host, then container
-e KEY=valueSet an environment variable
-v data:/pathAttach storage

Options come before the image name, and anything after the image is the command to run.

Common mistakes#

Practice#

Practice exercise

Run an nginx:1.30-alpine container named site, on host port 9090, check it with curl, then remove it.

Show solution
Terminal
docker run -d --name site -p 9090:80 nginx:1.30-alpine > /dev/null
sleep 2
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:9090
docker rm -f site
Output
200
site

Key takeaway#

Next: Docker Images.

Finished this lesson?

Some links are affiliate links. If you buy through them, Laviyx Learn may earn a commission at no extra cost to you. As an Amazon Associate, we earn from qualifying purchases.