Saturday 9 May 2020

Docker: Pass Environment variables

You can pass the environment variables to a container using -e option.

Syntax
docker run -e var1=value1 var2=value2 ….. image

Follow below step-by-step procedure to build complete working application.

Step 1: Create a directory ‘flask_hello_world’.

Step 2: Navigate to flask_hello_world and create Dockerfile.
$mkdir flask_hello_world
$
$cd flask_hello_world/
$
$touch Dockerfile
$ls
Dockerfile

Step 3: Update Dockerfile with below content.

Dockerfile
FROM ubuntu

RUN apt-get update

RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb

RUN pip install flask

COPY app.py /opt/app.py

ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0

Step 4: Create a file app.py in the folder where Dockerfile is located.

app.py
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
  return "Hello World"

@app.route('/aboutme')
def welcome():
  print(os.environ.get('appName'))
  name = os.environ.get('appName')
  return name

if __name__ == "__main__":
  app.run(host = "0.0.0.0", port = 8080)

Step 5: Execute the command ‘docker build -t hello_world_env .’ from the directory where Dockerfile is located.
$docker build -t hello_world_env .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 93102f1bf79f
Step 3/6 : RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb
 ---> Using cache
 ---> c04fb08e8e87
Step 4/6 : RUN pip install flask
 ---> Using cache
 ---> 4f0eb1453957
Step 5/6 : COPY app.py /opt/app.py
 ---> 7f0256021048
Step 6/6 : ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
 ---> Running in 33ab341f4f58
Removing intermediate container 33ab341f4f58
 ---> 65e7e4f349d4
Successfully built 65e7e4f349d4
Successfully tagged hello_world_env:latest

Execute the command ‘docker images’ to list all the images.

$docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED              SIZE
hello_world_env                    latest              65e7e4f349d4        About a minute ago   467MB
{my_user_name}/flask_hello_world   latest              4e76840aaf25        12 hours ago         467MB

Step 6: Start the docker container by executing below command.
docker run -p 1234:5000 --name app1 -e appName=chatServer hello_world_env

$docker run -p 1234:5000 --name app1 -e appName=chatServer hello_world_env
 * Serving Flask app "/opt/app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [31/Aug/2019 10:20:44] "GET /aboutme HTTP/1.1" 200 –

Open the url ‘http://localhost:1234/aboutme’ in browser, you can see below screen.


Previous                                                    Next                                                    Home

No comments:

Post a Comment