2014-01-20 22:31:23 -05:00
|
|
|
:title: First steps with Docker
|
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
|
|
|
|
|
|
|
|
2014-01-20 22:31:23 -05:00
|
|
|
First steps with Docker
|
|
|
|
=======================
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2014-01-20 20:48:48 -05:00
|
|
|
Check your docker install
|
|
|
|
-------------------------
|
2013-04-01 22:11:09 -04:00
|
|
|
|
2014-01-20 20:48:48 -05:00
|
|
|
This guide assumes you have a working installation of Docker. To check
|
|
|
|
your docker install, run the following command:
|
2013-04-01 22:11:09 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-01-20 20:48:48 -05:00
|
|
|
# Check that you have a working install
|
|
|
|
docker info
|
|
|
|
|
|
|
|
If you get ``docker: command not found`` or something like
|
2014-01-20 21:10:32 -05:00
|
|
|
``/var/lib/docker/repositories: permission denied`` you may have an incomplete
|
|
|
|
docker installation or insufficient privileges to access docker on your machine.
|
2014-01-20 20:48:48 -05:00
|
|
|
|
2014-01-20 21:10:32 -05:00
|
|
|
Please refer to :ref:`installation_list` for installation instructions.
|
2013-04-01 22:11:09 -04:00
|
|
|
|
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-12-30 13:57:11 -05: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
|
2013-10-24 21:59:59 -04:00
|
|
|
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-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-12-30 13:57:11 -05: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
|
2013-08-13 21:05:35 -04:00
|
|
|
``unix:///var/run/docker.sock`` to allow only local connections by the
|
2013-12-30 13:57:11 -05:00
|
|
|
*root* user. You *could* set it to ``0.0.0.0:4243`` or a specific host IP to
|
2013-08-13 21:05:35 -04:00
|
|
|
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
|
|
|
|
2013-12-28 16:16:03 -05:00
|
|
|
``-H``, when empty, will default to the same value as when no ``-H`` was passed in.
|
|
|
|
|
|
|
|
``-H`` also accepts short form for TCP bindings:
|
|
|
|
``host[:port]`` or ``:port``
|
|
|
|
|
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-12-30 13:57:11 -05: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 the ``docker
|
|
|
|
images`` command.
|
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`
|