2013-05-21 13:47:16 -04:00
|
|
|
:title: Basic Commands
|
2013-03-25 22:52:52 -04:00
|
|
|
:description: Common usage and commands
|
2013-05-21 13:47:16 -04:00
|
|
|
:keywords: Examples, Usage, basic commands, docker, documentation, examples
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
|
2013-06-02 01:03:12 -04:00
|
|
|
The Basics
|
|
|
|
==========
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
Starting Docker
|
|
|
|
---------------
|
|
|
|
|
|
|
|
If you have used one of the quick install paths', Docker may have been installed with upstart, Ubuntu's
|
|
|
|
system for starting processes at boot time. You should be able to run ``docker help`` and get output.
|
|
|
|
|
|
|
|
If you get ``docker: command not found`` or something like ``/var/lib/docker/repositories: permission denied``
|
|
|
|
you will need to specify the path to it and manually start it.
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Run docker in daemon mode
|
|
|
|
sudo <path to>/docker -d &
|
|
|
|
|
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
Running an interactive shell
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Download a base image
|
2013-03-27 09:43:05 -04:00
|
|
|
docker pull base
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Run an interactive shell in the base image,
|
|
|
|
# allocate a tty, attach stdin and stdout
|
2013-03-26 22:31:35 -04:00
|
|
|
docker run -i -t base /bin/bash
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-05-22 12:15:52 -04:00
|
|
|
Bind Docker to another host/port
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
If you want Docker to listen to another port and bind to another ip
|
|
|
|
use -host and -port on both deamon and client
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Run docker in daemon mode
|
2013-05-24 12:49:18 -04:00
|
|
|
sudo <path to>/docker -H 0.0.0.0:5555 &
|
2013-05-22 12:15:52 -04:00
|
|
|
# Download a base image
|
2013-05-24 12:49:18 -04:00
|
|
|
docker -H :5555 pull base
|
2013-05-22 12:15:52 -04:00
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
Starting a long-running worker process
|
|
|
|
--------------------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Start a very useful long-running process
|
2013-03-26 23:28:46 -04:00
|
|
|
JOB=$(docker run -d base /bin/sh -c "while true; do echo Hello world; sleep 1; done")
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Collect the output of the job so far
|
|
|
|
docker logs $JOB
|
|
|
|
|
|
|
|
# Kill the job
|
|
|
|
docker kill $JOB
|
|
|
|
|
|
|
|
|
|
|
|
Listing all running containers
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
docker ps
|
|
|
|
|
|
|
|
Expose a service on a TCP port
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Expose port 4444 of this container, and tell netcat to listen on it
|
2013-03-26 23:28:46 -04:00
|
|
|
JOB=$(docker run -d -p 4444 base /bin/nc -l -p 4444)
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Which public port is NATed to my container?
|
|
|
|
PORT=$(docker port $JOB 4444)
|
|
|
|
|
|
|
|
# Connect to the public port via the host's public address
|
2013-04-01 22:11:09 -04:00
|
|
|
# Please note that because of how routing works connecting to localhost or 127.0.0.1 $PORT will not work.
|
2013-04-07 03:43:57 -04:00
|
|
|
IP=$(ifconfig eth0 | perl -n -e 'if (m/inet addr:([\d\.]+)/g) { print $1 }')
|
|
|
|
echo hello world | nc $IP $PORT
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Verify that the network connection worked
|
|
|
|
echo "Daemon received: $(docker logs $JOB)"
|
2013-03-26 15:14:58 -04:00
|
|
|
|
|
|
|
|
2013-05-09 20:05:20 -04:00
|
|
|
Committing (saving) a container state
|
|
|
|
-------------------------------------
|
2013-03-29 20:30:10 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
Save your containers state to a container image, so the state can be re-used.
|
2013-03-29 20:30:10 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
When you commit your container only the differences between the image the container was created from
|
|
|
|
and the current state of the container will be stored (as a diff). See which images you already have
|
|
|
|
using ``docker images``
|
2013-03-29 20:30:10 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
.. code-block:: bash
|
2013-03-29 20:30:10 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
# Commit your container to a new named image
|
|
|
|
docker commit <container_id> <some_name>
|
2013-03-29 20:30:10 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
# List your containers
|
|
|
|
docker images
|
2013-03-29 20:30:10 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
You now have a image state from which you can create new instances.
|
2013-03-29 20:30:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
Read more about :ref:`working_with_the_repository` or continue to the complete :ref:`cli`
|
2013-03-29 20:30:10 -04:00
|
|
|
|