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.
On this page
Run a command in a container#
docker run alpine:3.24 echo "Hello from a container"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 containerHere is what happened:
- Docker looked for the image
alpine:3.24locally, and did not find it. - It downloaded the image from Docker Hub.
- It created a container and ran
echoinside it. - 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:
docker run alpine:3.24 cat /etc/os-release | head -n 2NAME="Alpine Linux"
ID=alpineA container is isolated#
A container has its own file system, processes and network. It sees only what is inside it:
docker run --rm alpine:3.24 ps
docker run --rm alpine:3.24 ls /homePID USER TIME COMMAND
1 root 0:00 psOnly 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:
docker run -it --rm alpine:3.24 shYou 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:
docker run -d --name web -p 8080:80 nginx:1.30-alpine > /dev/null
sleep 2
curl -sI http://localhost:8080 | head -n 2Unable 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.6Nginx 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.
Browser --> localhost:8080 --> [ container port 80: nginx ]See what is running#
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'NAMES IMAGE STATUS PORTS
web nginx:1.30-alpine Up 2 seconds 0.0.0.0:8080->80/tcp, [::]:8080->80/tcpPlain docker ps works too, with more columns. Read the container's logs, which show the request we just made:
docker logs web 2>&1 | tail -n 1 | cut -c1-70172.17.0.1 - - [05/Nov/2026:07:04:43 +0000] "HEAD / HTTP/1.1" 200 0 "-Stop and remove it#
docker stop web
docker rm web
docker ps -a --format '{{.Names}}' | grep -c webweb
web
0docker rm -f web does both in one step.
Common docker run options#
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
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 site200
siteKey takeaway#
Next: Docker Images.
Finished this lesson?
Related lessons
Recommended Docker and Kubernetes resources
- Docker Deep Dive by Nigel Poulton
A practical guide to how Docker works, for readers who have finished the basics.
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.