{"id":172,"date":"2019-02-12T20:09:35","date_gmt":"2019-02-12T14:39:35","guid":{"rendered":"http:\/\/blogs.shydrate.com\/?p=172"},"modified":"2019-02-12T20:09:35","modified_gmt":"2019-02-12T14:39:35","slug":"how-to-connect-and-deploy-application-from-docker-container-to-ec2-instance","status":"publish","type":"post","link":"https:\/\/blogs.shydrate.com\/index.php\/2019\/02\/12\/how-to-connect-and-deploy-application-from-docker-container-to-ec2-instance\/","title":{"rendered":"How to connect and deploy application from docker container to EC2 instance"},"content":{"rendered":"<p>In this article, I will be showing how to run and deploy the angular project from Docker Container(Localhost) to the Amazon EC2 instance.<\/p>\n<p>I have made a video and uploaded it on <a href=\"https:\/\/www.youtube.com\/watch?v=HBuuQY5Uxu8\">Youtube<\/a>. This is the theoretical article.<\/p>\n<p>First, I have taken a base Ubuntu OS in docker and installed Angular, Nodejs, JDK, Jenkins and created a new image, which I pushed to my <a href=\"https:\/\/hub.docker.com\/r\/shydrate\/jenkins_angular\">repository<\/a>. After doing that I ran my docker container by this cmd<\/p>\n<p>docker run -it -d -p 8095:8080 -p 81:80 -p 50000:50000 image_id. For entering into container\u00a0type this cmd<\/p>\n<p>docker exec -it container_id bin\/bash.<\/p>\n<p>After entering into the container you will be logged in root user. Run this command &#8220;service Jenkins start&#8221;.You will get a message as Jenkins\u00a0started on some pid\u00a0number. If you are using a MAC OS the jenkins\u00a0URL will be localhost and the port number which you entered when you started the\u00a0container. I am running it on port 8095 so my URL will be &#8220;localhost:8095&#8221;. If you are using windows then your jenkins\u00a0url will be 192.168.99.100: port number. it&#8217;s because of the NAT setting in the network.<\/p>\n<p>Now create an EC2 instance using Ubuntu any version. I am using 18.04. log into instance.\u00a0By default you will ubuntu user. Now run &#8220;sudo\u00a0apt-get update&#8221; this cmd will update packages. And install apache2 in an instance.<\/p>\n<p>&#8220;sudo\u00a0apt-get install apache2 -y&#8221; by running this cmd apache will be installed. Now run &#8220;sudo service apache2 start&#8221; to start Apache service in an instance.<\/p>\n<p>Now go to your docker container and switch to jenkins user. I have created a user called jenkins by using &#8220;useradd jenkins&#8221; cmd and set a password for the user by&#8221;passwd jenkins&#8221; cmd. To switch to jenkins user<\/p>\n<p>&#8220;su &#8211; jenkins&#8221;. Now run &#8220;ssh-keygen&#8221; hit enter until you get the message\u00a0that key has been created. Now this key will be under jenkins folder i.e, \/usr\/lib\/jenkins\/.ssh\/id_rsa.pub. view that key by &#8220;cat \/.ssh\/id_rsa.pub&#8221; and copy that key. And go to ec2 instance, here you are logged in as ubuntu user run &#8220;ls -la&#8221;. You will find .ssh folder in your home directory. In that folder, there will be a\u00a0file called authorize_keys open it and paste the key that you copied from your docker container by &#8220;sudo vi authorize_keys&#8221;. Once done go back to your docker container and run &#8220;ssh ubuntu@public-ip-address&#8221;. If everything is working fine you will be welcomed as ubuntu user inside ec2 instance. run &#8220;exit&#8221; to exit from ssh connection.<\/p>\n<p>Now go to your jenkins url and create a new job or project. Under source management, select git. And add the git repository and under build section select &#8220;execute as shell&#8221;. And add these cmds &#8220;npm install&#8221;, &#8220;ng build&#8221;, &#8220;cd dist&#8221;, &#8220;tar -czvf new-name.tar.gz *&#8221;, &#8220;scp new-name.tar.gz instance-2-user@public-ip-address:folder&#8221; apply and save it. Now click on Build. The build is successful<\/p>\n<p>.<\/p>\n<p>&nbsp;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In this article, I will be showing how to run and deploy the angular project from Docker Container(Localhost) to the Amazon EC2 instance. I have made a video and uploaded it on Youtube. This is the theoretical article. First, I<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[18,16,17],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-my-articles","tag-angular-project-deploy","tag-how-to-connect-and-deploy-application-from-docker-container-to-ec2-instance","tag-ssh-connection"],"_links":{"self":[{"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":1,"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/posts\/172\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.shydrate.com\/index.php\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}