docker run to create a container.
Useful links:
Example terminal prompt used below:
What you’ll see in this lesson
- Pulling small base images (Alpine) and running short commands
- Running a service image (NGINX) and mapping ports to access it from the host
- Quick reference for common
docker runflags
Try a minimal OS image: Alpine
Alpine Linux is a tiny distribution commonly used for base images. Its small size (a few MB) makes pulls fast and is ideal for lightweight containers. Run Alpine (no command specified):docker run alpine with no foreground process typically exits immediately because the container has nothing to keep it alive. To run a short-lived command inside the container:
sleep 10 inside it, and then the container exits after 10 seconds.
Containers remain running only while their main process runs. To explore a container interactively, start a shell with
docker run -it --rm alpine sh. The --rm flag removes the container when it exits.Run a real application: NGINX
NGINX images are available in several variants (Alpine-based, slim, full) and tagged per version. Run the default NGINX image:http://localhost:8080 (or the host IP and port) to see the NGINX welcome page.
When mapping container ports to host ports, be careful exposing services directly to public networks. For production use, secure your service, use firewalls, and control access. Avoid publishing unnecessary ports.
Quick reference: common docker run flags
Next steps
After this introduction, useful topics to learn next include:- Image tags and version pinning (use specific tags, not
latest) - Persistent storage with volumes
- Container networking and port management
- Container lifecycle and process management