docker container in detach mode exits instantly - python

I am using docker SDK for python and trying to create a container. Following is the code I am executing:
import docker
client = docker.DockerClient(base_url='tcp://10.41.70.76:2375')
image = client.images.get('siab_user_one')
container = client.containers.run(image.tags[0], detach=True)
container.exec_run("ls")
But, the above code throws the following error:
Traceback (most recent call last):
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/api/client.py", line 261, in _raise_for_status
response.raise_for_status()
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 409 Client Error: Conflict for url: http://10.41.70.76:2375/v1.35/containers/ccdb556fb234eeb86b19d37c30e9d64e428bf42a8d2b70784225dcf3c5347859/exec
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "dock.py", line 7, in <module>
container.exec_run("ls")
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/models/containers.py", line 196, in exec_run
workdir=workdir,
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/api/exec_api.py", line 80, in exec_create
return self._result(res, True)
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/api/client.py", line 267, in _result
self._raise_for_status(response)
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/api/client.py", line 263, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/Users/aditya/workspace/term/lib/python3.6/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 409 Client Error: Conflict ("Container ccdb556fb234eeb86b19d37c30e9d64e428bf42a8d2b70784225dcf3c5347859 is not running")
Even after running the container in detached mode, container exits soon after creation.
PS: The docker image is present locally and was created manually.
It works fine with remote images.

I think you need to supply a command to prevent the container from exiting, try this:
container = client.containers.run(image.tags[0], command=["tail", "-f", "/dev/null"],detach=True)
or using tty=True with detach=True will help also

Related

Getting this error when accessing Panel Dashboard on different computer in a network

I have deployed a panel app on IIS using reverse proxy method but when accessing the same I get the following error and the dashboard seems to be incompletely loaded in browser
Exception in callback <bound method BokehTornado._keep_alive of <bokeh.server.tornado.BokehTornado object at 0x00000229666E6680>>
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\Dashboard\lib\site-packages\tornado\ioloop.py", line 921, in _run
val = self.callback()
File "C:\ProgramData\Anaconda3\envs\Dashboard\lib\site-packages\bokeh\server\tornado.py", line 760, in _keep_alive
c.send_ping()
File "C:\ProgramData\Anaconda3\envs\Dashboard\lib\site-packages\bokeh\server\connection.py", line 93, in send_ping
self._socket.ping(str(self._ping_count).encode("utf-8"))
File "C:\ProgramData\Anaconda3\envs\Dashboard\lib\site-packages\tornado\websocket.py", line 444, in ping
raise WebSocketClosedError()
tornado.websocket.WebSocketClosedError
I have tried running it on localhost and it runs fine.

pymongo: Resolver configuration could not be read or specified no nameservers

It's my first time using MongoDB but I can't seem to fix this one issue, my friend who uses MongoDB doesn't know how to use python so he can't really help me.
Here's my code:
import pymongo
# Replace the uri string with your MongoDB deployment's connection string.
conn_str = "mongodb+srv://sqdnoises:{mypass}#sqd.d4kjb.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
# set a 5-second connection timeout
client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=5000)
try:
print(client.server_info())
print('\n\n\n aka connected')
except Exception:
print("Unable to connect to the server.")
Where {mypass} is my MongoDB password.
I keep getting this error:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/dns/resolver.py", line 782, in read_resolv_conf
f = stack.enter_context(open(f))
FileNotFoundError: [Errno 2] No such file or directory: '/etc/resolv.conf'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/srv_resolver.py", line 88, in _resolve_uri
results = _resolve('_' + self.__srv + '._tcp.' + self.__fqdn,
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/srv_resolver.py", line 41, in _resolve
return resolver.resolve(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/dns/resolver.py", line 1305, in resolve
return get_default_resolver().resolve(qname, rdtype, rdclass, tcp, source,
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/dns/resolver.py", line 1278, in get_default_resolver
reset_default_resolver()
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/dns/resolver.py", line 1290, in reset_default_resolver
default_resolver = Resolver()
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/dns/resolver.py", line 734, in __init__
self.read_resolv_conf(filename)
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/dns/resolver.py", line 785, in read_resolv_conf
raise NoResolverConfiguration
dns.resolver.NoResolverConfiguration: Resolver configuration could not be read or specified no nameservers.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/storage/emulated/0/! workspace/mongolearn/main.py", line 7, in <module>
client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=5000)
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/mongo_client.py", line 677, in __init__
res = uri_parser.parse_uri(
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/uri_parser.py", line 532, in parse_uri
nodes = dns_resolver.get_hosts()
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/srv_resolver.py", line 119, in get_hosts
_, nodes = self._get_srv_response_and_hosts(True)
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/srv_resolver.py", line 99, in _get_srv_response_and_hosts
results = self._resolve_uri(encapsulate_errors)
File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/pymongo/srv_resolver.py", line 95, in _resolve_uri
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: Resolver configuration could not be read or specified no nameservers.
How do I fix this?
I am following https://docs.mongodb.com/drivers/pymongo/
indeed, the problem is that dnspython tries to open /etc/resolv.conf
import dns.resolver
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=['8.8.8.8']
Just adde this code to the top of your main code, and that should be sufficient to get you past this hurdle..

Twitter app requires python 3.6.13 to run but Dockerfiles' similar alpine version is not working. Running into mismatch of python package

The Twitter bot app requires python 3.6.13 to run. While dockerizing it, Dockerfile has alpines' python versions which is not meeting my apps requirement.
How can I install python 3.6.13 in a Dockerfile?
For example, in my case, I tried initiating a docker file like this
FROM python:3.6-alpine
COPY bots/config.py /bots/
COPY bots/stream.py /bots/
COPY requirements.txt /tmp
RUN pip3 install -r /tmp/requirements.txt
WORKDIR /bots
CMD ["python3", "stream.py"]
After creating the docker container, the app crashes due to pythons mismatch of versions.
Later I tried this with the version I want:
FROM python:3.6.13
COPY bots/config.py /bots/
COPY bots/stream.py /bots/
COPY requirements.txt /tmp
RUN pip3 install -r /tmp/requirements.txt
WORKDIR /bots
CMD ["python3", "stream.py"]
the python package is very large unlike alpine provides.
But When I tried this:
FROM python:3.6.13-alpine
COPY bots/config.py /bots/
COPY bots/stream.py /bots/
COPY requirements.txt /tmp
RUN pip3 install -r /tmp/requirements.txt
WORKDIR /bots
CMD ["python3", "stream.py"]
It looks like it worked as the program started execution but I got another new error which is this:
INFO:tweepy.binder:PARAMS: {}
ERROR:root:Error creating API
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 187, in execute
proxies=self.api.proxy)
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 528, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 466, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 320, in prepare
self.prepare_auth(auth, url)
File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 556, in prepare_auth
r = auth(self)
File "/usr/local/lib/python3.6/site-packages/requests_oauthlib/oauth1_auth.py", line 109, in __call__
unicode(r.url), unicode(r.method), None, r.headers
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 351, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 165, in get_oauth_signature
uri, headers, body = self._render(request)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 248, in _render
request.oauth_params, request.headers, realm=realm)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/utils.py", line 25, in wrapper
return target(params, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/parameters.py", line 53, in prepare_headers
escaped_value = utils.escape(value)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/utils.py", line 50, in escape
'Got {!r} of type {}.'.format(u, type(u)))
ValueError: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/bots/config.py", line 18, in create_api
api.verify_credentials()
File "/usr/local/lib/python3.6/site-packages/tweepy/api.py", line 631, in verify_credentials
)(**kargs)
File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 189, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 187, in execute
proxies=self.api.proxy)
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 528, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 466, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 320, in prepare
self.prepare_auth(auth, url)
File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 556, in prepare_auth
r = auth(self)
File "/usr/local/lib/python3.6/site-packages/requests_oauthlib/oauth1_auth.py", line 109, in __call__
unicode(r.url), unicode(r.method), None, r.headers
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 351, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 165, in get_oauth_signature
uri, headers, body = self._render(request)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 248, in _render
request.oauth_params, request.headers, realm=realm)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/utils.py", line 25, in wrapper
return target(params, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/parameters.py", line 53, in prepare_headers
escaped_value = utils.escape(value)
File "/usr/local/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/utils.py", line 50, in escape
'Got {!r} of type {}.'.format(u, type(u)))
ValueError: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/bots/config.py", line 18, in create_api
api.verify_credentials()
File "/usr/local/lib/python3.6/site-packages/tweepy/api.py", line 631, in verify_credentials
)(**kargs)
File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/usr/local/lib/python3.6/site-packages/tweepy/binder.py", line 189, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "stream.py", line 50, in <module>
main(['485visas', 'extend485', '485visaextension', '485', '485visa',])
File "stream.py", line 42, in main
api = create_api()
File "/bots/config.py", line 21, in create_api
raise Exception
Exception
The config.py file is this:
import tweepy
import logging
import os
logger = logging.getLogger()
def create_api():
# TR485Graduate = tweepy.Stream('CONSUMER_KEY_485', 'CONSUMER_SECRET_485', 'ACCESS_TOKEN_485','ACCESS_TOKEN_SECRET_485' )
consumer_key = os.getenv("CONSUMER_KEY_485")
consumer_secret = os.getenv("CONSUMER_SECRET_485")
access_token = os.getenv("ACCESS_TOKEN_485")
access_token_secret = os.getenv("ACCESS_TOKEN_SECRET_485")
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
try:
api.verify_credentials()
except Exception as e:
logger.error("Error creating API", exc_info=True)
raise Exception
logger.info("API created")
return api
Is there anything that I can do to avoid this exception error?
Later, I tried to add the python version that I want in the requirements.txt file but it didn't work.
I found up a source but couldn't understand but I feel the answer is here, source.
Is there any way I can instantiate the python 3.6.13 version in the Dockerfile so that it uniformly supplies this version to the whole image and the app?

How to add constraints in client.services.create while creating a new service in existing docker swarm Python Docker SDK?

I want to create a service in existing swarm network using python docker sdk. I have a swarm network named test_net.
Installation of library : pip3 install docker
Below is the code used for creating the service
import docker
from docker.types import RestartPolicy, Placement
def python_sdk():
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
service_created = client.services.create(
image='python:3.7-alpine',
command='python /home/ubuntu/python.py',
constraints=Placement(constraints=['worker']),
mounts='/home/ubuntu/deployment/python.py:/home/ubuntu/python.py:rw',
networks=['test_net'],
restart_policy=RestartPolicy(condition='none'),
name='python_sdk'
)
print("Created service : ", service_created)
Below is the error which I got by executing above code :
Traceback (most recent call last):
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/api/client.py", line 268, in _raise_for_status
response.raise_for_status()
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http+docker://localhost/v1.41/services/create
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "client.py", line 20, in <module>
python_sdk()
File "client.py", line 16, in python_sdk
name='python_sdk'
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/models/services.py", line 227, in create
service_id = self.client.api.create_service(**create_kwargs)
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/utils/decorators.py", line 34, in wrapper
return f(self, *args, **kwargs)
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/api/service.py", line 190, in create_service
self._post_json(url, data=data, headers=headers), True
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/api/client.py", line 274, in _result
self._raise_for_status(response)
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/api/client.py", line 270, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/home/ubuntu/deployment/dags/venv/lib/python3.6/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 400 Client Error for http+docker://localhost/v1.41/services/create: Bad Request ("json: cannot unmarshal object into Go struct field Placement.TaskTemplate.Placement.Constraints of type []string")
I am referring to this documentation.
How can I use Placement object to use constraints?
I also tried constraints = ["Placement(constraints=['worker']"]
I got the answer for the above issue. In the documentation, it is mentioned that the list of str needs to be passed in constraints.
The mentioned parameter is documentation : constraints (list of str) – Placement constraints.
So the final code will be,
import docker
from docker.types import RestartPolicy, Placement
def python_sdk():
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
service_created = client.services.create(
image='python:3.7-alpine',
command='python /home/ubuntu/python.py',
constraints=['node.role == worker']),
mounts='/home/ubuntu/deployment/python.py:/home/ubuntu/python.py:rw',
networks=['test_net'],
restart_policy=RestartPolicy(condition='none'),
name='python_sdk'
)
print("Created service : ", service_created)

Mongodb and Celery "pymongo.errors.OperationFailure: command createIndexes requires authentication"

I am working Celery with Mongodb( as backend and as a broker).
I follow this tutorial on how to set them up: https://skillachie.com/2013/06/15/intro-celery-and-mongodb/
When I have the security enable on the /etc/mongod.conf file like this:
security:
authorization: enabled
and I call the .get() to get the results of the tasks that I have set up I get this error:
Traceback (most recent call last):
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/kombu/utils/__init__.py", line 323, in __get__
return obj.__dict__[self.__name__]
KeyError: 'collection'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/celery/result.py", line 169, in get
no_ack=no_ack,
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/celery/backends/base.py", line 229, in wait_for
meta = self.get_task_meta(task_id)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/celery/backends/base.py", line 307, in get_task_meta
meta = self._get_task_meta_for(task_id)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/celery/backends/mongodb.py", line 158, in _get_task_meta_for
obj = self.collection.find_one({'_id': task_id})
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/kombu/utils/__init__.py", line 325, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/celery/backends/mongodb.py", line 246, in collection
collection.ensure_index('date_done', background='true')
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/pymongo/collection.py", line 2028, in ensure_index
self.__create_index(keys, kwargs, session=None)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/pymongo/collection.py", line 1894, in __create_index
session=session)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/pymongo/collection.py", line 250, in _command
user_fields=user_fields)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/pymongo/pool.py", line 613, in command
user_fields=user_fields)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/pymongo/network.py", line 167, in command
parse_write_concern_error=parse_write_concern_error)
File "/home/celeryProject/celeryProject/lib/python3.6/site-packages/pymongo/helpers.py", line 159, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command createIndexes requires authentication
But when I disable the security it is working!
I also tried to change the roles on the user on mongodb and nothing happened.
Also, I have tried to log in with that way:
BROKER_URL = 'mongodb://tester:123456#178.128.250.181:27017/test?authSource=admin'
celery = Celery('EOD_TASKS',broker=BROKER_URL)
Is this a security problem or I can ignore it and move on?
if i'm not mistaken, using authorization: enabled means you need to setup a user/pass to login in order to query the mongodb. by default it is left wide open, which is a security consideration

Categories