Running Ad Hoc Docker

Just a quick tip for those that love docker and find themselves wanting an app running quickly.

First, I recommend to everyone that wants to work with Docker to read their e-book. It gets auto updated with every release so this book will never get out of date. Yeah, they do continuous delivery with their books. Haha.

Running web server with local files
docker run --rm -p 8080:80 -v `pwd`:/usr/share/nginx/html:ro nginx:1.15-alpine
  • --rm cleans up after the container exits
  • -p 8080:80 mounts host port 8080 to container port 80
  • -v ... mounts the local directory to nginx’s default hosting path
    • ${PWD} can be used instead of `pwd` to print-working-directory
  • -it (not used above) will give you an interactive terminal. (it technically doesn’t stand for that.)
  • Simply navigate in your browser to localhost:8080 to see your files.

Other examples:

# Interactive Node12 container
docker run --rm -it -v ${PWD}:/code -w=/code node:12 /bin/bash
# Interactive php7.4 container
docker run --rm -it -v ${PWD}:/code -w=/code php:7.4 /bin/bash
# Interactive python3 container
docker run --rm -it -v ${PWD}:/code -w=/code python:3-buster /bin/bash
# Run mysqldump to a local file
docker run --rm -i mysql:8 mysqldump <args> 1>dump.sql
# Run pg_dump to a local file
docker run --rm -i postgres:10.6 pg_dump <args> 1>dump.sql
# Interactively connect to redis
docker run --rm -it redis redis-cli -h <host>

But Why?

I can think of at least two reasons. First, you might want to run a different version of a piece of software than what is installed locally. Second, you might not want to install the software locally for various reasons.