basecommands.rst 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. :title: docker documentation
  2. :description: Common usage and commands
  3. :keywords: Examples, Usage
  4. Base commands
  5. =============
  6. Running an interactive shell
  7. ----------------------------
  8. .. code-block:: bash
  9. # Download a base image
  10. docker import base
  11. # Run an interactive shell in the base image,
  12. # allocate a tty, attach stdin and stdout
  13. docker run -a -i -t base /bin/bash
  14. Starting a long-running worker process
  15. --------------------------------------
  16. .. code-block:: bash
  17. # Run docker in daemon mode
  18. (docker -d || echo "Docker daemon already running") &
  19. # Start a very useful long-running process
  20. JOB=$(docker run base /bin/sh -c "while true; do echo Hello world!; sleep 1; done")
  21. # Collect the output of the job so far
  22. docker logs $JOB
  23. # Kill the job
  24. docker kill $JOB
  25. Listing all running containers
  26. ------------------------------
  27. .. code-block:: bash
  28. docker ps
  29. Expose a service on a TCP port
  30. ------------------------------
  31. .. code-block:: bash
  32. # Expose port 4444 of this container, and tell netcat to listen on it
  33. JOB=$(docker run -p 4444 base /bin/nc -l -p 4444)
  34. # Which public port is NATed to my container?
  35. PORT=$(docker port $JOB 4444)
  36. # Connect to the public port via the host's public address
  37. echo hello world | nc $(hostname) $PORT
  38. # Verify that the network connection worked
  39. echo "Daemon received: $(docker logs $JOB)"