'Creates a container. Parameters are similar to those for the ``docker
run`` command except it doesn\'t support the attach options (``-a``).
The arguments that are passed directly to this function are
host-independent configuration options. Host-specific configuration
is passed with the `host_config` argument. You\'ll normally want to
use this method in combination with the :py:meth:`create_host_config`
method to generate ``host_config``.
**Port bindings**
Port binding is done in two parts: first, provide a list of ports to
open inside the container with the ``ports`` parameter, then declare
bindings with the ``host_config`` parameter. For example:
.. code-block:: python
container_id = cli.create_container(
\'busybox\', \'ls\', ports=[1111, 2222],
host_config=cli.create_host_config(port_bindings={
1111: 4567,
2222: None
You can limit the host address on which the port will be exposed like
such:
.. code-block:: python
cli.create_host_config(port_bindings={1111: (\'127.0.0.1\', 4567)})
Or without host port assignment:
.. code-block:: python
cli.create_host_config(port_bindings={1111: (\'127.0.0.1\',)})
If you wish to use UDP instead of TCP (default), you need to declare
ports as such in both the config and host config:
.. code-block:: python
container_id = cli.create_container(
\'busybox\', \'ls\', ports=[(1111, \'udp\'), 2222],
host_config=cli.create_host_config(port_bindings={
\'1111/udp\': 4567, 2222: None
To bind multiple host ports to a single container port, use the
following syntax:
.. code-block:: python
cli.create_host_config(port_bindings={
1111: [1234, 4567]
You can also bind multiple IPs to a single container port:
.. code-block:: python
cli.create_host_config(port_bindings={
1111: [
(\'192.168.0.100\', 1234),
(\'192.168.0.101\', 1234)
**Using volumes**
Volume declaration is done in two parts. Provide a list of
paths to use as mountpoints inside the container with the
``volumes`` parameter, and declare mappings from paths on the host
in the ``host_config`` section.
.. code-block:: python
container_id = cli.create_container(
\'busybox\', \'ls\', volumes=[\'/mnt/vol1\', \'/mnt/vol2\'],
host_config=cli.create_host_config(binds={
\'/home/user1/\': {
\'bind\': \'/mnt/vol2\',
\'mode\': \'rw\',
\'/var/www\': {
\'bind\': \'/mnt/vol1\',
\'mode\': \'ro\',
You can alternatively specify binds as a list. This code is equivalent
to the example above:
.. code-block:: python
container_id = cli.create_container(
\'busybox\', \'ls\', volumes=[\'/mnt/vol1\', \'/mnt/vol2\'],
host_config=cli.create_host_config(binds=[
\'/home/user1/:/mnt/vol2\',
\'/var/www:/mnt/vol1:ro\',
**Networking**
You can specify networks to connect the container to by using the
``networking_config`` parameter. At the time of creation, you can
only connect a container to a single networking, but you
can create more connections by using
:py:meth:`~connect_container_to_network`.
For example:
.. code-block:: python
networking_config = docker_client.create_networking_config({
\'network1\': docker_client.create_endpoint_config(
ipv4_address=\'172.28.0.124\',
aliases=[\'foo\', \'bar\'],
links=[\'container2\']
ctnr = docker_client.create_container(
img, command, networking_config=networking_config
Args:
image (str): The image to run
command (str or list): The command to be run in the container
hostname (str): Optional hostname for the container
user (str or int): Username or UID
detach (bool): Detached mode: run container in the background and
return container ID
stdin_open (bool): Keep STDIN open even if not attached
tty (bool): Allocate a pseudo-TTY
mem_limit (float or str): Memory limit. Accepts float values (which
represent the memory limit of the created container in bytes)
or a string with a units identification char (``100000b``,
``1000k``, ``128m``, ``1g``). If a string is specified without
a units character, bytes are assumed as an intended unit.
ports (list of ints): A list of port numbers
environment (dict or list): A dictionary or a list of strings in
the following format ``["PASSWORD=xxx"]`` or
``{"PASSWORD": "xxx"}``.
dns (:py:class:`list`): DNS name servers. Deprecated since API
version 1.10. Use ``host_config`` instead.
volumes (str or list): List of paths inside the container to use
as volumes.
volumes_from (:py:class:`list`): List of container names or Ids to
get volumes from.
network_disabled (bool): Disable networking
name (str): A name for the container
entrypoint (str or list): An entrypoint
working_dir (str): Path to the working directory
domainname (str): The domain name to use for the container
memswap_limit (int):
host_config (dict): A dictionary created with
:py:meth:`create_host_config`.
mac_address (str): The Mac Address to assign the container
labels (dict or list): A dictionary of name-value labels (e.g.
``{"label1": "value1", "label2": "value2"}``) or a list of
names of labels to set with empty values (e.g.
``["label1", "label2"]``)
volume_driver (str): The name of a volume driver/plugin.
stop_signal (str): The stop signal to use to stop the container
(e.g. ``SIGINT``).
stop_timeout (int): Timeout to stop the container, in seconds.
Default: 10
networking_config (dict): A networking configuration generated
by :py:meth:`create_networking_config`.
runtime (str): Runtime to use with this container.
healthcheck (dict): Specify a test to perform to check that the
container is healthy.
Returns:
A dictionary with an image \'Id\' key and a \'Warnings\' key.
Raises:
:py:class:`docker.errors.ImageNotFound`
If the specified image does not exist.
:py:class:`docker.errors.APIError`
If the server returns an error.'

def create_container(self, image, command=None, hostname=None, user=None, detach=False, stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None, dns=None, volumes=None, volumes_from=None, network_disabled=False, name=None, entrypoint=None, cpu_shares=None, working_dir=None, domainname=None, memswap_limit=None, cpuset=None, host_config=None, mac_address=None, labels=None, volume_driver=None, stop_signal=None, networking_config=None, healthcheck=None, stop_timeout=None, runtime=None):

    if isinstance(volumes, six.string_types):
        volumes = [volumes]
    if (host_config and (utils.compare_version('1.15', self._version) < 0)):
        raise errors.InvalidVersion('host_config    is    not    supported    in    API    <    1.15')
    config = self.create_container_config(image, command, hostname, user, detach, stdin_open, tty, mem_limit, ports, dns, environment, volumes, volumes_from, network_disabled, entrypoint, cpu_shares, working_dir, domainname, memswap_limit, cpuset, host_config, mac_address, labels, volume_driver, stop_signal, networking_config, healthcheck, stop_timeout, runtime)
    return self.create_container_from_config(config, name)
