Skip to main content
In this lesson you’ll run your first Docker containers and see how images from Docker Hub are used to create containers. This covers pulling images, running short-lived vs. long-lived containers, starting interactive shells, and publishing container ports to the host. Docker Hub is Docker’s central image registry where you can find images for operating systems, language runtimes, databases, web servers, machine-learning frameworks, and more — for example: Ubuntu, Alpine, Python, TensorFlow, PyTorch, Postgres, MySQL, MongoDB, NGINX, and Grafana. Use image tags to select versions (for example alpine:3.18, nginx:1.31, python:3.11-slim).
Docker images are identified by repository and tag (for example nginx:1.31 or alpine:latest). When you run docker run image-name, Docker will pull the image from Docker Hub if it doesn’t exist locally, defaulting to the :latest tag if none is specified. Use explicit tags for reproducible builds and deployments.

Running a minimal OS image (Alpine)

Alpine is a minimal Linux distribution commonly used as a tiny base image. Its small size (a few MB) makes it fast to download and ideal for quick experiments. Important: if a container image’s default command completes immediately, the container will exit right away. That’s why running an image with no interactive process often returns you immediately to your shell. Example — running alpine with no command:
Keep a container alive for a short time by running a long-lived command such as sleep:
The --rm flag tells Docker to remove the container after it exits. To get an interactive shell inside Alpine, start it with -it:

Running a web server (NGINX)

Many container images run a long-lived foreground process. NGINX is a common example: by default it runs in the foreground and streams logs to the terminal, so running it without detaching will show logs. Example — running NGINX in the foreground (Docker will pull the image if needed):
By default, NGINX listens on port 80 inside the container. To access it from your host machine, publish the container port to a host port and (optionally) run the container in detached mode. Example — run NGINX detached and map host port 8080 to container port 80:
If you run NGINX without -p, the service is not reachable from the host on localhost unless you use other Docker networking options. You can still view its logs if it runs in the foreground.
Be cautious when exposing container ports to the host. Only publish the ports you need, and consider firewall settings and network exposure — especially when binding to public interfaces.

Quick reference — common images and commands

Common Docker Hub images:

Troubleshooting tips

  • If docker run prints “Unable to find image … locally” then Docker is pulling the image — wait for the download.
  • If containers exit immediately, check the image’s default command or run an interactive shell to inspect logs and filesystem.
  • Use docker ps -a to see stopped containers and docker logs <container-id> to view logs for a container that ran in the background or exited.

Summary and next steps

  • Docker Hub offers a wide variety of pre-built images for OSes, runtimes, databases, web servers, and ML frameworks.
  • docker run image pulls the image if needed, creates a container, and executes the image’s default command.
  • Short-lived default commands result in containers that exit immediately. Use sleep, -it, or run long-lived services to keep containers active.
  • For services like NGINX, use -p host_port:container_port to make them reachable from your host and -d to run detached.
  • Key topics to explore next: image tags and versioning, Docker networking modes, port binding and security, persistent storage (volumes), and image building with Dockerfile.
Links and references

Watch Video