Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Tuesday, March 13, 2018

Define your container with Dockerfile

If you haven't installed Docker, then install it on your machine using the link (Installing Docker on Ubuntu).

Define a container with Dockerfile

Now its time to create your own custom environment using DockerFile. DockerFile behaves exactly the same wherever it runs enabling you to establish same standard configuration on dev, test and production environment.

Step 1: Create a folder (in my case i have created my-docker-img folder)
Step 2: Create a Dockerfile inside the folder (I have created Dockerfile.txt)
Dockerfile.txt
 # Use an official Python runtime as a parent image  
 FROM python:2.7-slim  
 # Set the working directory to /app  
 WORKDIR /app  
 # Copy the current directory contents into the container at /app  
 ADD . /app  
 # Set proxy server, replace host:port with values for your servers (If your are behind proxy server)  
 #ENV http_proxy host:port  
 #ENV https_proxy host:port  
 # Install any needed packages specified in requirements.txt  
 RUN pip install --trusted-host pypi.python.org -r requirements.txt  
 # Make port 80 available to the world outside this container  
 EXPOSE 80  
 # Define environment variable  
 ENV NAME World  
 # Run app.py when the container launches  
 CMD ["python", "app.py"]  

Step 3: Create app.py and requirement.txt specified in Docker file in the same folder.
app.py file is your python application that you want to deploy

app.py

 from flask import Flask  
 from redis import Redis, RedisError  
 import os  
 import socket  
 # Connect to Redis  
 redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)  
 app = Flask(__name__)  
 @app.route("/")  
 def hello():  
   try:  
     visits = redis.incr("counter")  
   except RedisError:  
     visits = "<i>cannot connect to Redis, counter disabled</i>"  
   html = "<h3>Hello {name}!</h3>" \  
       "<b>Hostname:</b> {hostname}<br/>" \  
       "<b>Visits:</b> {visits}"  
   return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)  
 if __name__ == "__main__":  
   app.run(host='0.0.0.0', port=80)  

requirements.txt
 Flask  
 Redis  

Note:
When the Dockerfile is built into an image, Dockerfile’s ADD command will add app.py into the image, and the app is served over HTTP with help the EXPOSE command which expose port 80. PIP command install python dependency libraries specified on requirements.txt file

Build the app

Now execute the build command from top of the directory. This creates a Docker image with tag using -t so it has a friendly name.
In my case directory is /Documents/my-docker-img and image name is my-app
 docker build -t my-app .  
To view the list images created on your machine
 docker image ls  

Run the app

Run the app, by mapping your machine’s port 8000 to the container’s published port 80 using -p
 docker run -p 4000:80 friendlyhello  
Then you can access your app deployed within the container using following URL
 http://localhost:8000/  
If you want to run the container in the background in detach mode
 docker run -d -p 4000:80 my-app  
To view all the running containers on your machine
 docker container ls  
If you want to stop the running container using CONTAINER ID
  sudo docker container stop c1081125c6e9   

Next Share your docker image

Useful links
https://docs.docker.com/get-started/part2/#log-in-with-your-docker-id

Installing Docker in Ubuntu

Set up repository to install dependent package for Docker installation
 sudo apt-get update  
Install packages to allow apt to use a repository over HTTPS:
  sudo apt-get install \  
   apt-transport-https \  
   ca-certificates \  
   curl \  
   software-properties-common  
Add Docker’s official GPG key:
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -  
To Verify the key with the fingerprint by matching the last 8 characters of the fingerprint.
 sudo apt-key fingerprint 0EBFCD88  
Set up the stable repository I have chosen stable repository and x86_64/amd64. For different version please follow the link
 sudo add-apt-repository \  
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \  
   $(lsb_release -cs) \  
   stable"  
Now to install Docker
Update the apt package index
 sudo apt-get update  
Install the latest version of Docker CE
 sudo apt-get install docker-ce  
To verify that Docker CE is installed correctly by running the hello-world image
 sudo docker run hello-world  
It will download a test image and run it in a container. When the container runs, it prints an informational message and exits. Now Docker CE is installed and running on your machine.

If you need to uninstall Docker installed
 sudo apt-get purge docker-ce  
To delete all images, containers, and volumes which is not removed automatically
 sudo rm -rf /var/lib/docker  
If you want to configure Linux as post installation Click the Link. Some Useful Commands: To see list of images downloaded image on your machine.
 docker image ls  
To get help
 docker container --help  
Next Define your own container with Docker file

Helpful links
https://docs.docker.com/get-started/#conclusion-of-part-one
https://github.com/docker/labs/tree/master/beginner/

SQL INTERVIEW QUESTIONS AND ANSWERS