It sounds like you're using docker-py.
Also, it sounds like maybe you're not familiar with TLS, so please read the documentation for using TLS with docker-py. You may need to download your TLS files and copy them local to the docker-py client as they are used to authenticate that you are authorized to connect to the Docker daemon.
I hope your remote Docker daemon is not exposed to the world.
If it is not running TLS (exposed to the world):
client = docker.Client(base_url='<https_url>', tls=False)
If it is secured with TLS (not exposed to the world):
client = docker.Client(base_url='<https_url>', tls=True)
Answer from taco on Stack OverflowIt sounds like you're using docker-py.
Also, it sounds like maybe you're not familiar with TLS, so please read the documentation for using TLS with docker-py. You may need to download your TLS files and copy them local to the docker-py client as they are used to authenticate that you are authorized to connect to the Docker daemon.
I hope your remote Docker daemon is not exposed to the world.
If it is not running TLS (exposed to the world):
client = docker.Client(base_url='<https_url>', tls=False)
If it is secured with TLS (not exposed to the world):
client = docker.Client(base_url='<https_url>', tls=True)
This is not answer, but need your feedback.
The error message is: Connection refused, so can you run the command:
telnet 52.90.216.176 2375
To confirm if there is no firewall issue. Sometime the port is 2376
dockerpy - link containers with the docker python API - Stack Overflow
How to connect to Docker API from Docker container using Python? - Stack Overflow
Hosting a Docker web app with a Python API
python - How to execute host's Docker command from container? - Stack Overflow
» pip install docker-py
https://github.com/docker/docker-py
A Python library for the Docker Remote API. It does everything the docker command does, but from within Python – run containers, manage them, pull/push images, etc.
create_container:
Creates a container that can then be .start() ed.
Parameters are similar to those for the docker run
command except it doesn't support the attach options (-a).
The source code of create_container
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):
But I found links at start function:
def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
publish_all_ports=None, links=None, privileged=None,
dns=None, dns_search=None, volumes_from=None, network_mode=None,
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
security_opt=None, ulimits=None):
So I think you should:
from docker import Client
>>> cli = Client(base_url='tcp://127.0.0.1:2375')
>>> container = cli.create_container(
... image='busybox:latest',
... command='/bin/sleep 30')
>>> response = cli.start(container=container.get('Id'),links=[('EXISTING_CONTAINER', 'LINK_NAME')])
The working example (DO)
I am using CoreOS on DO:
- run docker container and mount inside the
/var/run/docker.sockfrom host - install tools
- run test container
EXISTING_CONTAINER - run python example
The set of commands:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:12.04 bash
apt-get update;apt-get install python-pip -y;pip install docker-py
docker run -d --name EXISTING_CONTAINER busybox sh -c "while true; do sleep 1;done"
Python example
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30')
response = cli.start(container=container.get('Id'),links=(('EXISTING_CONTAINER', 'LINK_NAME'))
The result on host:
wp-coreos-512mb-ams2-01 ~ # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f58e661579d busybox "sh -c 'while true; d" 23 seconds ago Up 22 seconds EXISTING_CONTAINER
6f08dd3f5017 busybox:latest "/bin/sleep 30" 9 minutes ago Up 5 seconds condescending_brown
Yes, the networking documentation for docker-py is seriously lacking - the maintainers agree (https://github.com/docker/docker-py/issues/982 for a global alias example).
Note that Valeriy's answer above will create a legacy link, which might (will in my case) lead to issues if you use a non-default network such as the ones created by docker-compose.
In any case, adding parameters to Client.start is depreciated.
The new way to do this can be found in the unitttest: https://github.com/docker/docker-py/blob/master/tests/integration/network_test.py#L190-213
@requires_api_version('1.22')
def test_create_with_links(self):
net_name, net_id = self.create_network()
container = self.create_and_start(
host_config=self.client.create_host_config(network_mode=net_name),
networking_config=self.client.create_networking_config({
net_name: self.client.create_endpoint_config(
links=[('docker-py-test-upstream', 'bar')],
),
}),
)
container_data = self.client.inspect_container(container)
self.assertEqual(
container_data['NetworkSettings']['Networks'][net_name]['Links'],
['docker-py-test-upstream:bar'])
self.create_and_start(
name='docker-py-test-upstream',
host_config=self.client.create_host_config(network_mode=net_name),
)
self.execute(container, ['nslookup', 'bar'])
Valeriy's Example would then look as follows:
Python Example
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
# Note: 'bridge' is the default network
net_config = cli.create_networking_config(
{'bridge': self.docker_client.create_endpoint_config(
links=[('EXISTING_CONTAINER', 'LINK_NAME')]
)}
)
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30',
network_configuration=net_config
)
response = cli.start(container=container.get('Id'))
I have not tested this specific code, but this is the way I have been able to connect a new container to an existing container, whereas the existing one had been created by compose into a network "project_default"
You might also want to check this link for more information and background.
This may not necessarily fall under “self-hosted,” but I figured the people of this community would be able to give me some advice. I need to host a demo web app for a research project at work, but our IT/HPC department(s) said they don’t have the infrastructure to host a web app. The web app has a frontend web server and a backend Python API. I just need a service that can host the web app and offers various tiers of compute resources. Ideally, it would be able to spin up/down the API container depending on demand so we don’t have to pay for availability 24/7 (working at a university, budget is important). I looked into services like AWS and DigitalOcean, but they’re not quite what I’m after. If anyone has some advice, I’d appreciate it!
You can either mount the docker binary and socket into the container:
$ docker run -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock debian docker --version
Docker version 1.7.0, build 0baf609
Or set up docker to allow remote access (I can't find a good reference for this at the minute).
You should also look at using the Docker API rather than making command line calls. There is a python library available to help you.
If you make the Docker daemon reachable via HTTPS then you can communicate with the daemon from remote machines, or from within Docker containers. The instructions for enabling HTTPS in the daemon are here https://docs.docker.com/articles/https/
In short it involves creating client and server certificates (for security) and running the Docker daemon with a command such as
docker -d --tlsverify --tlscacert=ca.pem \
--tlscert=server-cert.pem --tlskey=server-key.pem \
-H=0.0.0.0:2376
When running in this mode, you can use an appropriate client library for the programming language of your choice https://docs.docker.com/engine/reference/api/remote_api_client_libraries/
There is one for python docker-py I haven't tried it but can say from experience this approach works using docker-java client library, having a Java program inside a container stopping and starting other containers.