Skip to content

Instantly share code, notes, and snippets.

@1UC1F3R616
Last active July 19, 2020 04:03
Show Gist options
  • Save 1UC1F3R616/256ec53b07f11f15ee1143468084f4cf to your computer and use it in GitHub Desktop.
Save 1UC1F3R616/256ec53b07f11f15ee1143468084f4cf to your computer and use it in GitHub Desktop.

Installing Docker-CE in Kali Linux

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
echo 'deb [arch=amd64] https://download.docker.com/linux/debian buster stable' | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update
sudo apt-get install docker-ce

Check it's installed properly

# Expected behaviour: It's shows a message which includes success
sudo docker run hello-world

Starting Docker everytime

sudo systemctl start docker 

Enabling Docker to autostart when reboot

sudo systemctl enable docker 

Adding non-root user

sudo usermod -aG docker $no-root-user-name

Installing Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Running the first app (Flask)

> Create app.py
> Place it inside another folder named *web* (or any other name of your choice)
> We can place our DB in another container by simply creating another folder *db* and then dockerize it as we do for *web*
> Now create a file **_Dockerfile_** and _requirements.txt_ (any name of your choice) in inside the web folder
> Create a file **_docker-compose.yml_** in main directory where you are having *web* and *db* folder (It's for managing the 
  different docker containers)

Directory Structure

.
├── db
├── docker-compose.yml
└── web
    ├── app.py
    ├── Dockerfile
    └── requirements.txt

Inside Dockerfile

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Inside requirements.txt

Flask

Inside docker-compose.yml

version: '3'

services:
    web:
        build: ./web
        ports:
            - "5000:5000"

Running using Docker

# In main directory where you have docker-compose.yml
sudo docker-compose build
sudo docker-compose up

Note

#Inside your app.py, make sure host is '0.0.0.0' because localhost won't work
app.run(host='0.0.0.0')

Now we are adding DB in our example project

cd db
touch Dockerfile

Inside db/Docker file

FROM mongo:3.6.4

Inside (upgraded) docker-compose.yml file

version: '3'

services:
    web:
        build: ./web
        ports:
            - "5000:5000"
        links:
            - db
    db:
        build: ./db

PyMongo Code Inside my app

from pymongo import MongoClient

app = Flask(__name__)

client = MongoClient("mongodb://db:27017") # Docker Handles it, here db comes from the folder name and 27017 is MongoDB port
db = client.aNewDB # Database creation
UserNum = db['UserNum'] # Collection

UserNum.insert({
    'num_of_users':0
})

@app.route('/visit', methods=['GET'])
def visit():
    prev_num = UserNum.find({})[0]['num_of_users']
    new_num = prev_num + 1
    UserNum.update({}, {"$set":{"num_of_users":new_num}}) # Here Key also a string
    return str(new_num)

Now Running it

docker-compose build
docker-compose up
docker-compose rm # To remove the container, so to start from fresh DB

Removing Dangling Images (docker images -a)

docker rmi $(docker images -f "dangling=true" -q)
@1UC1F3R616
Copy link
Author

1UC1F3R616 commented Jul 19, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment