3. First containers
Finding your way around the CLI
Section titled “Finding your way around the CLI”wslc --help # all commandswslc <COMMAND> --help # help for a commandwslc image list # images available locallywslc container list # running containerswslc container list --all # including stopped containerswslc stats # container CPU / memory usageA throwaway container
Section titled “A throwaway container”wslc run --rm -it ubuntu:latest bash -c "echo Hello world from WSL container!"| Option | Effect |
|---|---|
--rm |
removes the container as soon as it stops |
-it |
interactive mode with a terminal |
ubuntu:latest |
the image (downloaded automatically if missing) |
bash -c "…" |
the command to run in the container |
A web server in the background
Section titled “A web server in the background”# Run nginx in the background, Windows port 8080 → container port 80wslc run -d --rm -p 8080:80 --name web nginx
# Query the server from Windowscurl localhost:8080
# See the containerwslc container list
# Run a command in the running containerwslc exec web cat /etc/os-release
# Stop it (and, thanks to --rm, remove it)wslc container stop web| Option | Effect |
|---|---|
-d |
detached: the container runs in the background |
-p 8080:80 |
publishes the port: host:container |
--name web |
readable name, usable instead of the ID |
Key takeaways
Section titled “Key takeaways”runcreates and starts a container;--rmavoids piling up stopped containers.-p host:containermakes a service reachable from Windows.execruns a command in an already running container.
Exercises
Section titled “Exercises”- Run an
nginxcontainer reachable on Windows port 9090, namedsite, then check that it responds.
Solution
wslc run -d --rm -p 9090:80 --name site nginxcurl localhost:9090wslc container stop site- How can you find out which Linux distribution the
nginximage uses without opening an interactive shell?
Solution
wslc run -d --rm --name site nginxwslc exec site cat /etc/os-releasewslc container stop site- What is the difference between
wslc container listandwslc container list --all?
Solution
Without --all, only running containers are listed. With --all, stopped (but not removed) containers appear as well.