importerror:no module name docker.client - docker

I've gotten a question when usde ansible to configurate a docker container.
Here is my ansible playbook:
---
-name: localhost
hosts: localhost
vars:
- ansible_python_interpreter: python
tasks:
- name: busybox test
docker:
image: busybox
name: test
but when I run the file with :
ansible-playbook ad.yml
I got following error:
from docker.client import APIError as DockerAPIError
ImportError: No module named docker.client
no docker.client? I have installed docker-py, but still got this question , how can I fix this ?? h....e....l....p....

I think the problem is caused by the way how I install the ansible. After I reinstall the ansible through python-pip, this problem has been solved successfully.
zypper install python-pip
pip install ansible
use this to solve the problem on suse.

Related

Ansible can't import docker even though it's installed

I'm trying to build a server that runs a docker container using ansible, but I'm getting the error Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on ubuntu-xenial's Python /usr/bin/python3.
The target machine is ubuntu xenial, currently in vagrant but I get the same error on an Azure VM. My version of ansible is 2.9.15, the docker version ansible is installing is 20.10.1.
My test inventory file consists of
all:
vars:
ansible_user: vagrant
ansible_become: yes
ansible_python_interpreter: /usr/bin/python3
children:
a_servers:
hosts:
192.168.33.100
b_servers:
hosts:
192.168.33.100
prod:
hosts:
192.168.33.100
The relevant part of my playbook is:
- name: Docker role
include_role:
name: nickjj.docker
vars:
docker__users: ['deploy']
docker_registries:
- #registry_url: "https://index.docker.io/v1/"
username: "{{ docker_user.username }}"
password: "{{ docker_user.password }}"
- name: Log in to docker
docker_login:
username: "{{ docker_user.username }}"
password: "{{ docker_user.password }}"
become: yes
become_user: deploy
When I log in to the vagrant box as user deploy, I can run docker login and docker-compose up -d without any problems. I can also run /usr/bin/python3 and import docker without issue.
There is no other python on the system.
The error comes from the docker_login resource, which can't seem to import docker.
Is a configuration I'm missing, or something else I've overlooked that would cause it to fail? Any help is much appreciated.

Docker-in-Docker issues with connecting to internal container network (Anchore Engine)

I am having issues when trying to connect to a docker-compose network from inside of a container. These are the files I am working with. The whole thing runs when I ./run.sh.
Dockerfile:
FROM docker/compose:latest
WORKDIR .
# EXPOSE 8228
RUN apk update
RUN apk add py-pip
RUN apk add jq
RUN pip install anchorecli
COPY dockertest.sh ./dockertest.sh
COPY docker-compose.yaml docker-compose.yaml
CMD ["./dockertest.sh"]
docker-compose.yaml
services:
# The primary API endpoint service
engine-api:
image: anchore/anchore-engine:v0.6.0
depends_on:
- anchore-db
- engine-catalog
#volumes:
#- ./config-engine.yaml:/config/config.yaml:z
ports:
- "8228:8228"
..................
## A NUMBER OF OTHER CONTAINERS THAT ANCHORE-ENGINE USES ##
..................
networks:
default:
external:
name: anchore-net
dockertest.sh
echo "------------- INSTALL ANCHORE CLI ---------------------"
engineid=`docker ps | grep engine-api | cut -f 1 -d ' '`
engine_ip=`docker inspect $engineid | jq -r '.[0].NetworkSettings.Networks."cws-anchore-net".IPAddress'`
export ANCHORE_CLI_URL=http://$engine_ip:8228/v1
export ANCHORE_CLI_USER='user'
export ANCHORE_CLI_PASS='pass'
echo "System status"
anchore-cli --debug system status #This line throws error (see below)
run.sh:
#!/bin/bash
docker build . -t anchore-runner
docker network create anchore-net
docker-compose up -d
docker run --network="anchore-net" -v //var/run/docker.sock:/var/run/docker.sock anchore-runner
#docker network rm anchore-net
Error Message:
System status
INFO:anchorecli.clients.apiexternal:As Account = None
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 172.19.0.6:8228
Error: could not access anchore service (user=user url=http://172.19.0.6:8228/v1): HTTPConnectionPool(host='172.19.0.6', port=8228): Max retries exceeded with url: /v1
(Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused',))
Steps:
run.sh builds container image and creates network anchore-net
the container has an entrypoint script, which does multiple things
firstly, it brings up the docker-compose network as detached FROM inside the container
secondly, nstalls anchore-cli so I can run commands against container network
lastly, attempts to get a system status of the anchore-engine (d.c network) but thats where I am running into HTTP request connection issues.
I am dynamically getting the IP of the api endpoint container of anchore-engine and setting the URL of the request to do that. I have also tried passing those variables from command line such as:
anchore-cli --u user --p pass --url http://$engine_ip/8228/v1 system status but that throws the same error.
For those of you who took the time to read through this, I highly appreciate any input you can give me as to where the issue may be lying. Thank you very much.

Concourse Quickstart Docker Permissions Error

I'm trying to setup the Concourse CI locally by following the documentation. Everything goes great until I try to run a sample hello-world pipeline. The job results in this error...
runc create: exit status 1: container_linux.go:264: starting container process caused "process_linux.go:339: container init caused \"rootfs_linux.go:56: mounting \\\"/worker-state/3.8.0/assets/bin/init\\\" to rootfs \\\"/worker-state/volumes/live/a8d3b403-19e7-4f16-4a8a-40409a9b017f/volume/rootfs\\\" at \\\"/worker-state/volumes/live/a8d3b403-19e7-4f16-4a8a-40409a9b017f/volume/rootfs/tmp/garden-init\\\" caused \\\"open /worker-state/volumes/live/a8d3b403-19e7-4f16-4a8a-40409a9b017f/volume/rootfs/tmp/garden-init: permission denied\\\"\""
Looks like I'm getting a permissions error but I've double-checked that the container is running in privileged mode.
$ docker inspect --format='{{.HostConfig.Privileged}}' concourse_concourse_1
true
Docker Compose File
version: '3'
services:
concourse-db:
image: postgres
environment:
- POSTGRES_DB=concourse
- POSTGRES_PASSWORD=concourse_pass
- POSTGRES_USER=concourse_user
- PGDATA=/database
concourse:
image: concourse/concourse
command: quickstart
privileged: true
depends_on: [concourse-db]
ports: ["8080:8080"]
environment:
- CONCOURSE_POSTGRES_HOST=concourse-db
- CONCOURSE_POSTGRES_USER=concourse_user
- CONCOURSE_POSTGRES_PASSWORD=concourse_pass
- CONCOURSE_POSTGRES_DATABASE=concourse
- CONCOURSE_EXTERNAL_URL
- CONCOURSE_BASIC_AUTH_USERNAME
- CONCOURSE_BASIC_AUTH_PASSWORD
- CONCOURSE_NO_REALLY_I_DONT_WANT_ANY_AUTH=true
- CONCOURSE_WORKER_GARDEN_NETWORK
Pipeline
---
jobs:
- name: job-hello-world
public: true
plan:
- task: hello-world
config:
platform: linux
image_resource:
type: docker-image
source: {repository: busybox}
run:
path: echo
args: [hello world]
Concourse Version
$ curl http://192.168.99.100:8080/api/v1/info
{"version":"3.12.0","worker_version":"2.0"}
Other Versions
$ docker --version
Docker version 18.04.0-ce, build 3d479c0
$ docker-machine --version
docker-machine version 0.14.0, build 89b8332
$ docker-compose --version
docker-compose version 1.21.0, build unknown
$ system_profiler SPSoftwareDataType
Software:
System Software Overview:
System Version: macOS 10.13.1 (17B1003)
Kernel Version: Darwin 17.2.0
Boot Volume: OSX
While everything on the surface may look like it's up to date. It's important to note that docker-machine runs docker inside of VMs which can get stale if you're not updating them on a regular basis and Concourse needs kernel 3.19 or higher.
Running docker info can shed some light on the situation from Docker server's perspective.
What worked for me was...
$ docker-compose down
$ docker-machine upgrade
$ docker-compose up -d

Kitchen and Kitchen-docker

I am trying to use kitchen-docker driver on a GNU/Linux machine. I have installed the kitchen-docker gem using chef gem install command.
This is an extract of my .kitchen.yml file:
---
driver:
name: docker
provisioner:
name: chef_zero
verifier:
name: inspec
platforms:
- name: centos-7.2
driver_config:
image: centos:7.2
platform: centos
suites:
- name: zaz
run_list:
- recipe[foo::bar]
...
...
I have Docker installed on it's latest version using Docker repositories for Centos. The service is running and Docker is in my path. However when I try to run a simple kitchen list using that .kitchen.yml I get this error:
[FakeyMcFakeFace#workstation foo]$ kitchen list
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::UserError
>>>>>> Message: You must first install the Docker CLI tool http://www.docker.io/gettingstarted/
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration
Why is docker not being recognized by Kitchen? If I run the diagnose -all option I just see it is failing on the dependencies check:
backtrace:
- "/home/FakeyMcFakeFace/.chefdk/gem/ruby/2.3.0/gems/kitchen-docker-2.6.0/lib/kitchen/driver/docker.rb:93:in
`rescue in verify_dependencies'"
What am I missing here?
To copy down from the comments, kitchen-docker requires passwordless sudo (if using sudo) right now, the error message is misleading

Ansible error: KeyError: 'ApiVersion'

I'm using Ansible 1.7 (devel) and Docker 0.9.1 build 3600720, and I'm stuck with this error:
failed: [myapp.com] => {"failed": true, "item": "", "parsed": false}
invalid output was: Traceback (most recent call last):
File "/root/.ansible/tmp/ansible-tmp-1400951250.7-173380463612813/docker", line 1959, in <module>
main()
File "/root/.ansible/tmp/ansible-tmp-1400951250.7-173380463612813/docker", line 693, in main
containers = manager.create_containers(1)
File "/root/.ansible/tmp/ansible-tmp-1400951250.7-173380463612813/docker", line 548, in create_containers
if docker.utils.compare_version('1.10', self.client.version()['ApiVersion']) < 0:
KeyError: 'ApiVersion'
Any ideas? Is there any combination of versions that works? I needed Ansible 1.7 because of the 'running' state that was added for docker containers.
I ran into this issue today and decided to fix it. The gist of the problem is older versions of docker don't have an ApiVersion specified (that or the docker-py package doesn't return it).
I've submitted a pull request to fix this issue in the ansible docker module here: https://github.com/ansible/ansible/pull/7619
Alternatively you could upgrade your docker version to get around it.
I got the same error and this fixed it
$ sudo apt-get install -y python-pip
$ sudo pip install docker-py
As said in a more general answer, use the docker_api_version: auto argument :
- name: Mongo data container
docker:
docker_api_version: auto
name: mongo-primary-dc
image: debian:wheezy
state: present
volumes:
- /data

Resources