Monday 29 January 2018

Creating simple hello world flask app using docker

First make sure docker is installed in your machine and you have necessary permissions to execute the following commands in your system. If you want to know about basics of docker please refer my previous blog - Docker guide for beginners

Following sample files are available in this github repo


Step 1 : Create working directory

mkdir flask-helloworld
cd flask-helloworld

Step 2: create following files inside the working directory


requirements.txt

Flask

app.py

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Dockerfile

# base image
FROM python:3-onbuild
# specify the port that container should expos
EXPOSE 5000
# run the application
CMD ["python", "./app.py"]


Step 3 : Build and Run your container


Build docker image:


Following command read your Dockerfile and build your custom images. Go to the path where your Docker file locates and execute following command.

docker build -t <imagename>:<tag-name> <path of your dockerfile>
docker build -t sample-app:v1 .

final dot(.) specifies the current directory. -t specifies the tag name of the image



Listing docker images:


docker images
docker images -a




Running Docker container:


Docker run command is used to run a image inside the container. If the specified image available in local machine docker will take it from local or it will download from dockerhub and then store it to local machine.
docker container run <image-name>
docker container run sample-app:v1
It will create a new container and run the sample-app image inside the container.




If you want to execute the container in background use --detach (or) -d flag. It will detach the process from foreground and allow us to execute it into background. It will return the unique container id.

docker container run -p 5000:5000 -d sample-app:v1













Executing commands inside the container:


Following command allow us to login inside the container. It is very helpful to debug our application if something went wrong. we can execute linux commands inside the container

docker run -it <image-name> sh

docker run -it sample-app:v1 sh




Stop the container:


Following command used to stop the container.
docker container stop 9a425901d134

Deleting containers:


Every run docker creating new containers so it will eat disk space, so best practice is cleanup the containers once done with that.

docker container rm <container id>

docker container rm 9a4

Sunday 28 January 2018

Docker guide for beginners - part 1


What is docker and Container?


Docker is a tool which helps developer life easier especially devops work. It helps us to create, deploy and running an application in easy manner using containers.

BTW what is container? containerization is one of the virtualization method and basically it is OS level virtualization concept. Container is an light weight image which contains executable piece of software application and its dependancies.

Think about normal application development and deployment, we need an instance with separate OS and need necessary packages to run an application and if we want to move our application to other environments (like dev, test, prod) we need to recreate entire instance/virtual machine and it is very tedious process.

But using software containers we can ship our code in to anywhere like physical containers,  Not like virtual machine, container won't have separate operating system. it uses host operating system's kernel so it is more efficient then VMs. container is like small instance which runs our application image.

Now just you can pack your code and dependance packages into container and run your container on anywhere with help of docker. Normally containers are very small and you can run many containers in single instance. We can move the containers to various environments like dev, test, prod and it uses same configuration.

Installing Docker(Community edition):


  

Once you installed docker, it will install docker client and docker server(also called docker machine/host) into your system. docker machine as an daemon process and it runs in background.  docker command is docker client and it speaks to docker server and return the response

Note:
Running docker without sudo : https://askubuntu.com/questions/477551/how-can-i-use-docker-without-sudo


run following commands in your command prompt to check docker client and server version details after successful installation. client and server versions normally same but it don't have to be same.

docker version





















If you want to get more details about your docker configuration and container information use "docker info" command

docker info




















Docker has two types of commands. Just simply type docker in command prompt and it will list out all the commands. Please find following standard format for using docker cli

docker <management command> <subcommand>
  • management commands
  • sub commands
docker





































Docker supports subcommands without management commands (old way). but latest subcommands are won't work without management commands. you can run docker commands in following two way.

docker container run
(or)

docker run


Docker Registry:


Docker's default registry is called dockerhub and it contains all container images similar to github. We can pull existing public images from docker-hub and can push our customized images to docker registry (it will be available for everyone). 

Docker image:


Docker image is a blueprint of an application. Docker images are basis of containers. We can search docker image in registry using docker search command.

docker search <image-name>
docker images can be classified 4 types.

  • Base images
    • It has no parent images. Usually it is like OS images Ex: ubuntu, python
  • Child Images
    • It is build based on parent images with additional functionality
  • Official images
    • It is created and maintained by docker folks
  • User images
    • It is custom images created by users. Normally it will be formatted like
      • <username>/<imagename>, 
      • ex: manojnuk50/flask-img
docker pull <image-name>
docker pull ubuntu:12.04

It will pull the ubuntu images from docker registry and store it your local system. 12.04 specifies the tag information. if not specifies the tag name it will download the latest images from registry.


What is unbuild images?



        Onbuild images are having multiple triggers. When we use unbuild image it will do some automate stuff for us. For example python inbuild image will do following steps while we creating containers

Image name: python:3-onbuild
  1. It will automatically copy requirements.txt file from current directory to container
  2. Run requirements.txt using pip install cmd
  3. Copy current directory files to /usr/src/app in container


Dockerfile:


    Dockerfile is a simple text-file that contains a list of commands that the Docker client calls while creating an image. It's a simple way to automate the image creation process.

Sample Docker file:
# base image
FROM python:3-onbuild
# specify the port that container should expos
EXPOSE 5000
# run the application
CMD ["python", "./app.py"]

FROM command specify the base image name. And expose 5000 specifies the port which need to expose
to outside the world.

CMD command specifies the command which needs to be execute when running the image in the container.
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect

Docker hello world:


Execute following hello-world docker command to check docker is installed properly in your machine. 

docker container run hello-world













It will download hello-world image from docker hub and pack it into container and then run the image. So run command create new container for every time.


Creating sample flask app using docker


Now we are going to create sample python flask hello world app using docker. First create a new folder and place following files into that folder. Refer this github repo

mkdir flask-helloworld
cd flask-helloworld

requirements.txt

Flask

app.py

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Dockerfile

# base image
FROM python:3-onbuild
# specify the port that container should expos
EXPOSE 5000
# run the application
CMD ["python", "./app.py"]


Build docker image:


Following command read your Dockerfile and build your custom images. Go to the path where your Docker file locates and execute following command.

docker build -t <imagename>:<tag-name> <path of your dockerfile>
docker build -t sample-app:v1 .

final dot(.) specifies the current directory. -t specifies the tag name of the image



Listing docker images:


docker images
docker images -a



Running Docker container:


Docker run command is used to run a image inside the container. If the specified image available in local machine docker will take it from local or it will download from dockerhub and then store it to local machine.
docker container run <image-name>
docker container run sample-app:v1
It will create a new container and run the sample-app image inside the container.




If you want to execute the container in background use --detach (or) -d flag. It will detach the process from foreground and allow us to execute it into background. It will return the unique container id.

docker container run -p 5000:5000 -d sample-app:v1



Executing commands inside the container:


Following command allow us to login inside the container. It is very helpful to debug our application if something went wrong. we can execute linux commands inside the container

docker run -it <image-name> sh

docker run -it sample-app:v1 sh

List out available containers:


you can see the list of containers in your local machine using following command
docker container ls





Flags:



docker container run -p 5000:5000 -d --name flask-helloworld sample-app:v1

docker flags are used to specify the specific action or behavior of the container while running
  • --name 
    • it specifies the name of the container
    • if we are not specifying the container name it will assign random  name
  • --detach (or) -d
    • it will detach the process from foreground and execute it into background process
    • docker container run -p 80:80 -d hello-world 
  •  --publish (or) -p
    • it specifies the port number need to be exposed from container to host machine
    • docker container run -p 80:80 hello-world
    • if you are not specifying the port while running command it will automatically assign random ports

docker port <container-name/id>

it will show all the ports used by the container




Docker logs:


docker log command used see the logs generated by containers while executing application images. basically it will be stdout results. following commands are used to play with container logs.


docker logs 9a425901d134






It will show the entire logs results generated by sample-host container.

docker logs --tail 20 9a425901d134
It will show the entire logs results generated by sample-host container.
docker logs --help
docker container top 9a425901d134
It will show the top processes running inside the container. (similar to linux top command)


Stop the container:


Following command used to stop the container.
docker container stop 9a425901d134

Deleting containers:


Every run docker creating new containers so it will eat disk space, so best practice is cleanup the containers once done with that.

docker container rm <container id>

docker container rm 9a4
docker rm $(docker ps -a -q -f status=exited)

  • Above command will delete all the containers created in local machine
  • -q only returns numeric Ids
  • -f filters output based on status
  • first 3 character of container id is enough to mention while deleting. it will be unique always


















Deleting Images:


docker rmi <image-id/name>

docker rmi $(docker images -a -q)














Conclusion:


In this part, I have convert some basic commands from docker. We will see more advanced concepts in next part.