2013-12-06 18:26:57 -05:00
|
|
|
:title: Learn 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-12-06 18:26:57 -05:00
|
|
|
Learn Basic Commands
|
|
|
|
====================
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-04-01 22:11:09 -04:00
|
|
|
Starting Docker
|
|
|
|
---------------
|
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
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 ``sudo docker help`` and get output.
|
2013-04-01 22:11:09 -04:00
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
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.
|
2013-04-01 22:11:09 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Run docker in daemon mode
|
|
|
|
sudo <path to>/docker -d &
|
|
|
|
|
2013-10-24 21:59:59 -04:00
|
|
|
Download a pre-built image
|
|
|
|
--------------------------
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-04 18:16:00 -04:00
|
|
|
# Download an ubuntu image
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker pull ubuntu
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-10-24 21:59:59 -04:00
|
|
|
This will find the ``ubuntu`` image by name in the :ref:`Central Index
|
|
|
|
<searching_central_index>` and download it from the top-level Central
|
|
|
|
Repository to a local image cache.
|
|
|
|
|
2013-11-13 20:45:22 -05:00
|
|
|
.. NOTE:: When the image has successfully downloaded, you will see a
|
|
|
|
12 character hash ``539c0211cd76: Download complete`` which is the
|
|
|
|
short form of the image ID. These short image IDs are the first 12
|
|
|
|
characters of the full image ID - which can be found using ``docker
|
|
|
|
inspect`` or ``docker images -notrunc=true``
|
2013-10-24 21:59:59 -04:00
|
|
|
|
|
|
|
Running an interactive shell
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-04 18:16:00 -04:00
|
|
|
# Run an interactive shell in the ubuntu image,
|
2013-03-25 22:52:52 -04:00
|
|
|
# allocate a tty, attach stdin and stdout
|
2013-08-18 22:44:46 -04:00
|
|
|
# To detach the tty without exiting the shell,
|
|
|
|
# use the escape sequence Ctrl-p + Ctrl-q
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker run -i -t ubuntu /bin/bash
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-11-13 20:45:22 -05:00
|
|
|
.. _dockergroup:
|
2013-08-28 20:26:10 -04:00
|
|
|
|
2013-11-15 14:38:03 -05:00
|
|
|
sudo and the docker Group
|
|
|
|
-------------------------
|
2013-08-13 21:05:35 -04:00
|
|
|
|
|
|
|
The ``docker`` daemon always runs as root, and since ``docker``
|
|
|
|
version 0.5.2, ``docker`` binds to a Unix socket instead of a TCP
|
|
|
|
port. By default that Unix socket is owned by the user *root*, and so,
|
2013-08-21 11:28:13 -04:00
|
|
|
by default, you can access it with ``sudo``.
|
2013-08-13 21:05:35 -04:00
|
|
|
|
2013-11-15 14:38:03 -05:00
|
|
|
Starting in version 0.5.3, if you (or your Docker installer) create a
|
|
|
|
Unix group called *docker* and add users to it, then the ``docker``
|
|
|
|
daemon will make the ownership of the Unix socket read/writable by the
|
|
|
|
*docker* group when the daemon starts. The ``docker`` daemon must
|
|
|
|
always run as root, but if you run the ``docker`` client as a user in
|
|
|
|
the *docker* group then you don't need to add ``sudo`` to all the
|
2013-12-09 12:25:20 -05:00
|
|
|
client commands. Warning: the *docker* group is root-equivalent.
|
2013-08-13 21:05:35 -04:00
|
|
|
|
2013-11-15 14:38:03 -05:00
|
|
|
**Example:**
|
2013-11-07 17:06:49 -05:00
|
|
|
|
2013-08-16 13:19:59 -04:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-11-15 14:38:03 -05:00
|
|
|
# Add the docker group if it doesn't already exist.
|
2013-08-16 13:19:59 -04:00
|
|
|
sudo groupadd docker
|
|
|
|
|
2013-11-23 07:16:28 -05:00
|
|
|
# Add the connected user "${USERNAME}" to the docker group.
|
2013-11-15 14:38:03 -05:00
|
|
|
# Change the user name to match your preferred user.
|
2013-08-21 11:28:13 -04:00
|
|
|
# You may have to logout and log back in again for
|
2013-11-15 14:38:03 -05:00
|
|
|
# this to take effect.
|
2013-11-23 07:16:28 -05:00
|
|
|
sudo gpasswd -a ${USERNAME} docker
|
2013-08-16 13:29:48 -04:00
|
|
|
|
2013-11-15 14:38:03 -05:00
|
|
|
# Restart the docker daemon.
|
2013-08-16 13:19:59 -04:00
|
|
|
sudo service docker restart
|
|
|
|
|
2013-10-18 21:00:44 -04:00
|
|
|
.. _bind_docker:
|
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
Bind Docker to another host/port or a Unix socket
|
2013-06-19 08:48:50 -04:00
|
|
|
-------------------------------------------------
|
2013-05-22 12:15:52 -04:00
|
|
|
|
2013-11-13 20:45:22 -05:00
|
|
|
.. warning:: Changing the default ``docker`` daemon binding to a TCP
|
2013-08-13 21:05:35 -04:00
|
|
|
port or Unix *docker* user group will increase your security risks
|
|
|
|
by allowing non-root users to potentially gain *root* access on the
|
|
|
|
host (`e.g. #1369
|
|
|
|
<https://github.com/dotcloud/docker/issues/1369>`_). Make sure you
|
|
|
|
control access to ``docker``.
|
2013-06-20 08:31:48 -04:00
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
With -H it is possible to make the Docker daemon to listen on a
|
|
|
|
specific ip and port. By default, it will listen on
|
|
|
|
``unix:///var/run/docker.sock`` to allow only local connections by the
|
|
|
|
*root* user. You *could* set it to 0.0.0.0:4243 or a specific host ip to
|
|
|
|
give access to everybody, but that is **not recommended** because then
|
|
|
|
it is trivial for someone to gain root access to the host where the
|
|
|
|
daemon is running.
|
|
|
|
|
|
|
|
Similarly, the Docker client can use ``-H`` to connect to a custom port.
|
|
|
|
|
|
|
|
``-H`` accepts host and port assignment in the following format:
|
|
|
|
``tcp://[host][:port]`` or ``unix://path``
|
2013-06-20 08:31:48 -04:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
* ``tcp://host:4243`` -> tcp connection on host:4243
|
|
|
|
* ``unix://path/to/socket`` -> unix socket located at ``path/to/socket``
|
2013-05-22 12:15:52 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Run docker in daemon mode
|
2013-07-22 23:26:40 -04:00
|
|
|
sudo <path to>/docker -H 0.0.0.0:5555 -d &
|
2013-08-04 18:16:00 -04:00
|
|
|
# Download an ubuntu image
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker -H :5555 pull ubuntu
|
2013-05-22 12:15:52 -04:00
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
You can use multiple ``-H``, for example, if you want to listen on
|
|
|
|
both TCP and a Unix socket
|
2013-06-19 08:48:50 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Run docker in daemon mode
|
2013-07-22 23:26:40 -04:00
|
|
|
sudo <path to>/docker -H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock -d &
|
2013-08-13 21:05:35 -04:00
|
|
|
# Download an ubuntu image, use default Unix socket
|
|
|
|
sudo docker pull ubuntu
|
|
|
|
# OR use the TCP port
|
|
|
|
sudo docker -H tcp://127.0.0.1:4243 pull ubuntu
|
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-08-13 21:05:35 -04:00
|
|
|
JOB=$(sudo docker run -d ubuntu /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
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker logs $JOB
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Kill the job
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker kill $JOB
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
Listing all running containers
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker ps
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-11-01 12:14:43 -04:00
|
|
|
Bind a service on a TCP port
|
2013-03-25 22:52:52 -04:00
|
|
|
------------------------------
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-11-01 12:14:43 -04:00
|
|
|
# Bind port 4444 of this container, and tell netcat to listen on it
|
2013-10-28 15:48:18 -04:00
|
|
|
JOB=$(sudo docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Which public port is NATed to my container?
|
2013-10-31 01:08:16 -04:00
|
|
|
PORT=$(sudo docker port $JOB 4444 | awk -F: '{ print $2 }')
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-11-01 12:14:43 -04:00
|
|
|
# Connect to the public port
|
|
|
|
echo hello world | nc 127.0.0.1 $PORT
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
# Verify that the network connection worked
|
2013-08-13 21:05:35 -04:00
|
|
|
echo "Daemon received: $(sudo 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-08-13 21:05:35 -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
|
|
|
|
``sudo 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
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo 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
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo 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-08-13 21:05:35 -04:00
|
|
|
Read more about :ref:`working_with_the_repository` or continue to the
|
|
|
|
complete :ref:`cli`
|