How to create own docker image :

Before creating the Dockerfile perform all the necessary task manually. Once we execute all the manual steps to create the image then we can write the Dockerfile with the format.

For Example: In the below code I am just creating a my-app image for simple workflow.


	
#  Manual Step by step : 

	# Runs the container and connect us to write the step :
	docker run -it ubuntu bash
	
	# Update Package Index and Install Python :
	apt-get update
	apt-get insall python-pip
	apt-get install -y python
	
	# Install other dependencies :
	pip install flask
	
	# create a python file and paste the source code:
	cat > /opt/app.py
	
	# Start Web Server:
	cd /opt/
	FLASK_APP=app.py flask run --host=0.0.0.0

		
	
#  Create a docker file : 

	# create a directory :
	mkdir my-app
	cd my-app
	
	# create a dockfile :
	cat > Dockerfile
	
	FROM ubuntu
	RUN apt-get update
	RUN apt-get install -y python
	RUN pip install flask
	
	COPY app.py /opt/app.py
	
	ENTRYPOINT FLASK_APP=app.py flask run --host=0.0.0.0
		

create app.py file and paste the application source code there.

	
# # build the docker file : 

	docker build . -t my-app
	
	# validate the created image file :
	docker images
	
	# run the created image file :
	docker run my-app
		

Push the created image to docker hub

We can only push to the repository of our account in docker. For that we need to build the image and tag it to the account-name with -t option.

To push the image to the repository we need to login to docker hub account.

	
# # Push the image to docker hub : 
	
	docker build . -t account-name/image-name 
	docker login
	docker push my-app

		

Environment Variable :

We can use the environment Variable to change the behaviour of our [python] script at run time.

In our python script we can assign the value os.environ.get('Param_Name') of any veriable and run time we can use export Param_Name= value; python 'filename.py'.

Once we create the image then we can use -e option to pass the parameter.

	
# # How to set Environment Variable : 
	
	docker run -e Param_Name=value my-app
		

Inspect Environment Variable :

	
# # How to see the list of environment variable of an image : 
	docker inspect image-name
	
	inside env: all the environment variable will be listed.
		

Command Vs. Entrypoint :

In case of CMD command line parameter will be replaced by the value given at run time whereas in case of ENTRYPOINT command line parameter will be appended by the value given at run time.

If we run the image incase of ENTRYPOINT without providing the parameter then it will give us an error as " missing operand".

To avoid such issue we can provide the default value with the help of CMD.

	
# # Command Vs. Entrypoint : 

	# CMD :
	FROM Ubuntu
	
	CMD sleep 5
	
	docker run ubuntu sleep 10:
	Command at Startup: sleep 10
	
	# ENTRYPOINT :
	FROM Ubuntu
	
	ENTRYPOINT ["sleep"]
	
	docker run ubuntu 10:
	Command at Startup: sleep 10
	
		
	
# # Entrypoint with CMD: 

	FROM Ubuntu
	
	ENTRYPOINT ["sleep"]
	
	CMD["5"]
	
	docker run ubuntu:
	Command at Startup: sleep 5
		
	
# #Override the Entrypoint at run time: 

	docker run --entrypoint:2.0 ubuntu 10
	Command at Startup: sleep2.0 10