2

I have my windows docker installed in my windows 10 machine. Now I need to install python and ansible in my docker container.

I got few references to install python and ansible in a Linux machine. But I could not find a source how to install python 3 and ansible in a windows10 docker container.

Once python is installed I can try to install ansible using pip command. But for that I am not sure how to start with python installation first. In docker I have installed Jenkins, and want to run my ansible playbooks in Jenkins. Kindly help. Thanks!

  • You may want to refer to the following answer: https://stackoverflow.com/questions/47216784/how-to-install-python-in-a-docker-image/47216866. Based on the answer, you can run the following inside your container to install ansible: RUN sudo pip install ansible. Hope this helps. – Naufal Sep 03 '19 at 10:18
  • My environment is windows and the commands mentioned in this answer doesn't suit me as its for Linux environment. – Manikandan R C Sep 03 '19 at 10:59
  • Aren't running [linux containers on windows](https://learn.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/linux-containers)? e.g. what does your `Dockerfile` look like? – masseyb Sep 03 '19 at 15:37
  • Since docker containers are designed to run in Windows, this should solve your problem. – Naufal Sep 04 '19 at 15:49

2 Answers2

0

I build an ansible image periodically tracking the devel branch:

# syntax=docker/dockerfile:experimental
FROM ubuntu:18.04

ENV DEBIAN_FRONTEND noninteractive
ENV PATH /ansible/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

RUN apt-get update && \
    apt-get -y install \
        git \
        openssh-client \
        python3.7 \
        python3.7-dev \
        python3-pip \
        python3-setuptools \
        python3-pygit2 \
        build-essential \
        libssl-dev \
        libffi-dev \
        man

RUN groupadd -g 1000 ansible && \
    useradd -u 1000 -g ansible -d /home/ansible -m -k /etc/skel -s /bin/bash ansible

RUN mkdir -p -m 0600 ~/.ssh && \
    ssh-keyscan github.com >> ~/.ssh/known_hosts

RUN --mount=type=ssh git clone -b devel https://github.com/ansible/ansible.git /ansible && \
    chown -R 1000:1000 /ansible

RUN python3 -m pip install -r /ansible/requirements.txt

RUN ln -s /usr/bin/python3 /usr/bin/python

RUN echo '. /ansible/hacking/env-setup' >> /home/ansible/.bashrc
ENTRYPOINT ["/ansible/bin/ansible"]

Note:

Build the image: DOCKER_BUILDKIT=1 docker build --rm --network host -t so:5776957 .

Run the container: docker run --rm --network host -e ANSIBLE_HOME=/ansible -e PYTHONPATH=/ansible/lib so:5776957 localhost -m ping

enter image description here

masseyb
  • 3,745
  • 1
  • 17
  • 29
  • Facing this issue during mount "Error response from daemon: Dockerfile parse error line 26: Unknown flag: mount". While searching for error i found "DOCKER_BUILDKIT=1 " to be prefixed with the docker build command. But I don't get a exact solution for this. Kindly provide your inputs. – Manikandan R C Sep 06 '19 at 09:45
  • OP wants to install these tools on a Windows Docker container, so an Ubuntu Docker container unfortunately doesn't help. – Josh Correia Sep 27 '22 at 19:06
  • @JoshCorreia note [ansible is not intended to be run from a windows control server](https://docs.ansible.com/ansible/2.3/intro_windows.html#reminder-you-must-have-a-linux-control-machine). Use [Linux containers on Windows](https://learn.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/linux-containers). – masseyb Sep 27 '22 at 21:08
0

Instead of installing in container from yourself, you may try to use existing docker image which already have the same already installed. If you still want to build by yourself, you can look at the Dockerfile in the github repo.

https://hub.docker.com/r/zeeshanjamal16/ansibledocker

Zeeshan Jamal
  • 21
  • 1
  • 7