Giter VIP home page Giter VIP logo

docker-example's Introduction

Docker

Docker

root:
|   Play-WithDocker.ps1
|   Create-PythonAndMongoDbEnv.ps1
|
+---centos
|       dockerfile
|
+---pshost
|       dockerfile
|       PsNetTools.zip
|
\---pyhost
        dockerfile
        get-mongodbs.py

Create a mongodb host

Create the image

Donwload the latest image from Docker Hub.

docker pull mongo

Create the container

docker run -it --name mongodb -d mongo
docker ps -s

Get the ip address of the mongodb-container:

$container = docker inspect mongodb
$object = $container | ConvertFrom-Json
$object | Select-Object Name, @{l="IPAddress";e={$object.NetworkSettings.IPAddress}}

Create a python host

Create the dockerfile

$pyhost = @"
FROM centos:8
LABEL os="CentOS 8"
LABEL author="Martin Walther"
LABEL content="Python3"
LABEL release-date="2021-04-03"
LABEL version="0.0.1-beta"
ENV container docker
RUN echo "*** Build Image ***"
RUN yum -y update && yum clean all
RUN yum install git -y
RUN yum install -y python3
RUN python3 -m pip install -U pip
RUN python3 -m pip install pywinrm
RUN python3 -m pip install pymongo
RUN echo "*** Build finished ***"
"@

function New-Dockerfile{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [String] $Location, 

        [Parameter(Mandatory=$true)]
        [String] $content
    )
    if(Test-Path $Location){
        Write-Host "$Location already exists"
    }else{
        $null = New-Item $Location -ItemType Directory
    }
    $content | Out-File (Join-Path $Location 'dockerfile') -Force
    Get-Item (Join-Path $Location 'dockerfile') | Select-Object Name,LastWriteTime,Length
}

New-Dockerfile -Location "D:\docker\pyhost" -content $pyhost

Create the image

Set-Location "D:\docker\pyhost"; docker build -f "D:\docker\pyhost\dockerfile" -t pyhost:1.0.0 .

Create the container

docker run -it --hostname pyhost --name pyhost -d pyhost:1.0.0

Work on the python host

Login to the python-host and create the python-script to connect to the mongodb.

docker exec -it pyhost  /bin/bash
cd /home
touch get-mongodbs.py
vi get-mongodbs.py

Insert the following code and save the file (esc, :wq):

import sys

def get_dbs(connectionstring):
    '''Connect to MongoDB and print out all databases'''
    import pymongo
    mongo_client = pymongo.MongoClient(connectionstring)
    print(mongo_client.list_database_names())
    mongo_client.close()

if len(sys.argv) == 1:
  mongohost = ''
else:
  mongohost = str(sys.argv[1])

if len(mongohost) == 0 or mongohost == '--help':
  print('Usage: python3 ' + str(sys.argv[0]) + ' <argument>')
  print('  Argument: hostname or ip-address to connect to the mongodb')
  print('  Example:  172.17.0.2')
else:
  connectionstring = "mongodb://"+ mongohost
  print('Trying to connect to: ' + connectionstring + ':27017')
  get_dbs(connectionstring)

Run the python-script and print all database:

python3 get-mongodbs.py <ip address of the mongodb>
Trying to connect to: mongodb://172.17.0.2:27017
['admin', 'config', 'local']

docker-example's People

Contributors

tinuwalther avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.