| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | title: Creating a Web Server inside Docker | 
					
						
							|  |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | # Creating a Web Server inside Docker
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Start docker and it’s container -
 | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | 	$ systemctl restart docker | 
					
						
							|  |  |  |  | 	$ systemctl enable docker | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | 	$ docker run -it --name webserver centos:latest | 
					
						
							|  |  |  |  | 	``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Install httpd
 | 
					
						
							|  |  |  |  | - **yum** is already configured in centos docker image. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | So directly install **httpd** software - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ yum install httpd -y` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | - Create a dummy web page to check the server - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | 	``` | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | 	$ cd /var/www/html | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | 	$ vi index.html | 
					
						
							|  |  |  |  | 	``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | ### Start services -
 | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  |   - If we use **systemctl** to start the services, this will not work and gives an error. | 
					
						
							|  |  |  |  |   - **systemctl** doesn't work in docker. | 
					
						
							| 
									
										
										
										
											2018-11-15 02:19:32 +00:00
										 |  |  |  |   - In actual RedHat system, when we start a service it actually runs a script in background. That script starts daemons. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  |   - To find the path of that script, check status of service | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ systemctl status httpd` | 
					
						
							|  |  |  |  | `Loaded` option shows script file path. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  |   - In that file, we have a line which actually starts service - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `ExecStart = /usr/sbin/httpd....... ` | 
					
						
							|  |  |  |  | So the command `/usr/sbin/httpd` actually starts **httpd** server. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | - Service is running or not, can be checked by - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ ps -aux | grep httpd` | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | - So we don't require `systemctl` we can directly start our web server using- | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ /usr/sbin/httpd` | 
					
						
							|  |  |  |  | This will start the web server. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | - `ifconfig` doesn't work in docker. | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  |   - So install software, which gives `ifconfig` command. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  |   - It can be checked in actual Redhat system by running this command- | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  |   `$ rpm -qf /usr/sbin/ifconfig` | 
					
						
							|  |  |  |  |   - This comes from **net-tools** package. | 
					
						
							|  |  |  |  |   - So Install **net-tools** in docker os. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | - Making _image_ of created web server - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ docker commit webserver apacheimg:v1` | 
					
						
							|  |  |  |  |   - Name of container is _webserver_. | 
					
						
							|  |  |  |  |   - This image can be share with exact setup with other users. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | - To save created image - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ docker save apacheimg:v1 -o mywebserver.tar` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | - To start **httpd** service automatically when container starts - | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  |   - Write `/usr/sbin/httpd` in following file. | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | 	`$ vim /root/.bashrc` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  |  | - To copy a file in container from the base system - | 
					
						
							| 
									
										
										
										
											2018-10-15 22:20:01 +05:30
										 |  |  |  | `$ docker cp /root/form.txt  myconatiner:/` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## Summary
 | 
					
						
							|  |  |  |  | Summary of all the commands - | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | $ docker run -it --name webserver centos:latest | 
					
						
							|  |  |  |  | $ yum install httpd -y | 
					
						
							|  |  |  |  | $ rpm -q httpd | 
					
						
							|  |  |  |  | $ cd /var/www/html | 
					
						
							|  |  |  |  | $ vi index.html | 
					
						
							|  |  |  |  | // write some web content | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | $ /usr/sbin/httpd | 
					
						
							|  |  |  |  | $ yum install net-tool | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | $ ifconfig | 
					
						
							| 
									
										
										
										
											2018-11-15 02:19:32 +00:00
										 |  |  |  | ``` |