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 — runningalpine with no command:
sleep:
--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):-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 runprints “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 -ato see stopped containers anddocker 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 imagepulls 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_portto make them reachable from your host and-dto 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.
- Docker Hub: https://hub.docker.com
- Docker documentation: https://docs.docker.com/
- NGINX Docker image: https://hub.docker.com/_/nginx
- Alpine Docker image: https://hub.docker.com/_/alpine