---
# System prepended metadata

title: Ansible as a Docker Container

---

# Ansible as a Docker Container

### Running Ansible inside a Docker container

**Docker workflow**
![DockerWorkflow](https://i.imgur.com/ssqXF4g.png)


#### Step 1: Install Docker
````go=1
sudo apt-get update
sudo apt install docker-ce
docker version
sudo usermod -aG docker labsuser
docker images
````

#### Step 2: Write Dockerfile and create Ansible image
````go=1
vi Dockerfile

Dockerfile Content

FROM ubuntu:20.04
ENV ANSIBLE_VERSION 2.9.17
RUN apt-get update; \
    apt-get install -y gcc python3; \
    apt-get install -y python3-pip; \
    apt-get clean all
RUN pip3 install --upgrade pip; \
    pip3 install "ansible==${ANSIBLE_VERSION}"; \
    pip3 install ansible
````
#### Step 3: Build the image using the following Docker command
````go=1
docker build -t ansible:latest .
````

:mag: *Note the . (dot) at the end of command which denotes that Dockerfile is in current directory.*

#### Step 4: Validate image creation by checking image list
````go=1
docker images
or
docker image ls
````

#### Step 5: Run Ansible inside a docker container
````go=1
docker run -it ansible
ansible --version
````

#### Step 6: [Optional] Push your Ansible image to Docker Hub

````go=1
docker login
docker push ansible:latest
docker tag ansible sk12k/ansible
docker image ls
docker push sk12k/ansible
````