Docker Tutorials

cd /d E:\projects\tests\php\docker\test
http://www.docker.com/
signup
login
for documentation and tutorials , Help >> Documentation . https://docs.docker.com/docker-hub/

download docker desktop
	allow required permissions
		in control panel
			"Programs and Features" enable Vhost
		in task manager(Ctrl + Shift +Esc) >>Performance tab
			"Enable Virtuaization"
		
Docker Commands
===============
 0. Common Tips
	0.1	get version
		docker --version
 
	0.2 get help on options and arguments
		
		docker run --help
		
		for particular option
		
		docker stop --help
	0.3 You can combine single character flags to shorten the full command. 

		docker run -d -p 80:80 docker/getting-started
		As an example, the command above could be written as:
		docker run -dp 80:80 docker/getting-started
		
 1.With images
	1.1 docker pull <image>:<tag>
	
	eg Usage
	Search in hub.docker.com for "php". check "available tags"
	
	docket pull php:7.4-apache

	1.2 List all images
	docker images  (lists all instlled images)
	output
		REPOSITORY   TAG          IMAGE ID       CREATED        SIZE
		php          7.4-apache   05e7c943eaa9   9 days ago     471MB
		ubuntu       latest       fb52e22af1b0   13 days ago    72.8MB
		alpine/git   latest       b8f176fa3f0d   3 months ago   25.1MB
	1.3remove image :  
	docker rmi <name>
 
 2. To start apache container
	2.1 docker run -d php:7.4-apache
		adding -d (detached mode) means run in back ground 
		
		give a name
		
			docker run --name php_cont -d php:7.4-apache
			
	    enamble command line while running
			docker run -it --name php_cont php:7.4-apache /bin/bash
			
			"-it" means interactive mode. be sure you dont use -d
			sample output root@116a984919f1:/var/www/html# 
			echo '<?php phpinfo();?>' >> phpinfo.php
			
			
	2.2 display all containers
		docker ps (ps for processes, show all running processes)
		docker ps -a(-a for all processes, including stopped ones)
		Output 
		=======
		CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS                       PORTS     NAMES
		116a984919f1   php:7.4-apache   "docker-php-entrypoi…"   8 minutes ago    Exited (127) 2 minutes ago             php_cont2
		cc51a66df7cd   php:7.4-apache   "docker-php-entrypoi…"   10 minutes ago   Up 10 minutes                80/tcp    php_cont
	2.3. run with specific port
		docker run -tid -p 8000:80 --name apache_server php:7.4-apache
		(-p 8000:80 - map port 8000 of the host to port 80 in the container, so site accessible with)
	2.4.To map the Apache server to run our application instead of the default Apache homepage. This means that we need to keep our application folder synced 	with the server root folder (/var/www/html). We can do that using the -v option. You can read more about container "volumes" in the Docker documentation:

		docker run -tid -p 8000:80 --name apache_server -v <YOUR_HOST_WWW_ROOT>:/var/www/html php:7.4-apache
		
		"$PWD" may be used for current dir
		-v "$PWD":/usr/src/myapp
		
	2.5 to remove the container when stoped use
		--rm 
 3.We can get our container’s IP with docker inspect:
		docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_ID_OR_NAME


			Error: Template parsing error: template: :1: unclosed action
			use double quotes in windows	
		
 4. Stop container
	
	docker stop <container id  OR name given> 
	docker stop php_cont 
 
 5. remove docker container
	
	docker rm <container id  OR name given> 
	docker rm php_cont 
	
		
    To remove all inactive ones
	============================
	   In linux:
	   docker rm -f $(docker ps -aq)
	   -a: all 
	   -q: id only
	   
	   In Windows:
	   https://stackoverflow.com/questions/2323292/assign-output-of-a-program-to-a-variable-using-a-ms-batch-file
	   use /f to loop in array of result
	   eg: for /f %i in ('docker ps -a -q') do echo %i
	   for /f %i in ('docker ps -a -q') do docker stop %i
	   
	   for /f %i in ('docker ps -a -q') do docker rm %i
	   
	   if using in .bat files , use additional % to escape %
	
	
	
	
	
 6.	TO START TESTING
	
	docker run -d -p 80:80 docker/getting-started
	
	http://localhost/  WILL LEAD YOU TO http://localhost/tutorial/
	
	docker run -tid -p 8000:80 --name apache_server -v E/projects/tests/php/docker/test:/var/www/html php:7.4-apache
	you can access http://localhost:8000/phpinfo.php
	
	docker run -tid -p 800:80 --name apache_server php:7.4-apache -it  /bin/bash
	you can access http://localhost:8000/phpinfo.php
	
	
 7. Building an image (Dockerfile)
	hub.docker.com >> help >> documentation >> Guide
 
	https://docs.docker.com/develop/
	
			>> Build Images >> Best practices
		https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
		

		
		learn commands
		FROM <image:tag>
		COPY <src> <dest>
		RUN <Executatble file eg: a2enmod rewrite>
		ENV <ENV Var name> <ENV Var val>
			eg: ENV APACHE_DOCUMENT_ROOT /path/to/new/root
			
		The CMD command should be used only one time in a Dockerfile, and it needs to have the following form:
		CMD ["executable","param1","param2"]
		
		EXPOSE <port no:>
		
		
		
  Usage:
  ======
    Dockerfile should be in the folder where our scripts are saved
    cd to the folder containing Dockerfile
	
	$ docker build -t <'name:tag' format, tag is optional> <Dockerfile path , eg . if it is in the current folder)
	eg: docker build -t php_test .
	$ docker run -it --rm --name php_cont_test php_test
		
		
		
    For Build example with "php"
	
	scroll down to https://hub.docker.com/_/php (page got on searching 'PHP')
	can get a lot of options
		
 8. Compose:(use multiple containers and build channels to intereact)
	==================================================================
	for more detailed docker-compose.yml check https://semaphoreci.com/community/tutorials/dockerizing-a-php-application
	uses
	1. Dockerfile
	2. YAML File (docker-compose.yml)
	
	command:
		docker compose up
		or
		docker-compose up
		
    To stop Ctrl C
	To remove containers
		docker compose down
	
	hub.docker.com >> Manuals (https://docs.docker.com/desktop/)
						>> Docker Compose (https://docs.docker.com/compose/)
 
 
	.yml file is whitespace sensitive
	#https://hub.docker.com/_/mysql, 
	#docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
	
	docker exec -it <container id> bash
	docker exec -it test_db_1 bash
	mysql -uroot -p12345
	
	show databases;
	
	use docker_database;
	show tables;
	describe test_table;
	
	insert into test_table values (1,'docker'),(2,'php');
	insert into test_table (name)values ('Apache'),('MYSQL');
	
	
	
	9. Install PD0(add RUN command in Dockerfile)
	=============================================
	
	RUN docker-php-ext-install pdo pdo_mysql
	
	this is mentioned in https://hub.docker.com/_/php
	
		9.1 Error while connecting with PDO
		=====================================
		Connection failed: SQLSTATE[HY000] [2002] No such file or directory
			use IP address instead of 'localhost'
		
		Connection failed: SQLSTATE[HY000] [2002] Connection refused
			dont use 127.0.0.1, use the ip address in network instead eg: 192.168.0.1
			get ip address by running 'ipconfig' in command line
			IPv4 Address. . . . . . . . . . . : 192.168.29.185 under Wireless LAN adapter Wi-Fi:

 
 
   10. Docker Commit and Push
   =============================
   login to https://hub.docker.com/
   
   go to help >> documentation https://docs.docker.com/docker-hub/
   search 'commit'
   will take you to https://docs.docker.com/engine/reference/commandline/commit/
   
		gives diffrent options for docker commit command
		simplest
		docker commit <container id>  <username>/<imge name to be saved in docker hub>:<tag>
		
		eg: docker commit c3f279d17e0a  svendowideit/testimage:version3
		
   Check for "push" in the same page
       docker push [OPTIONS] NAME[:TAG]
		
   
   in CLI
   
		docker login
		
		
			Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
			Username: dockerosol
			Password:
			Login Succeeded
			
		run in another cli
		cd /d E:\projects\tests\php\docker\test		
		docker run --rm --name my-running-app -p 800:80 -v %cd%:/var/www/html test_web
		
		confirm checking access with http://localhost:800/
		
		get container id with 
		
		docker ps
		Now Commit with container id
		============================
		docker commit 47136804930c  dockerosol/php_apache_pdo:1.0
		
		now docker images will show dockerosol/php_apache_pdo also
		
		now
		
		docker push dockerosol/php_apache_pdo:1.0
		
		once the process is over the respository will appear on https://hub.docker.com/repositories
		docker hub gives one private repository for free and rest for paid plans
		
		on clicking it
		
		will show details like To push a new tag to this repository, "docker push dockerosol/php_apache_pdo:tagname"
		on clicking "public view" will show pull command "docker pull dockerosol/php_apache_pdo" & "docker pull dockerosol/php_apache_pdo:1.0"