basecommands.rst 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. :title: Base commands
  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 pull base
  11. # Run an interactive shell in the base image,
  12. # allocate a tty, attach stdin and stdout
  13. docker run -i -t base /bin/bash
  14. Starting a long-running worker process
  15. --------------------------------------
  16. .. code-block:: bash
  17. # Run docker in daemon mode
  18. (sudo docker -d || echo "Docker daemon already running") &
  19. # Start a very useful long-running process
  20. JOB=$(docker run -d 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 -d -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)"
  40. Continue to the complete `Command Line Interface`_
  41. .. _Command Line Interface: ../commandline/cli.html