{"id":2601,"date":"2017-12-23T22:38:55","date_gmt":"2017-12-23T22:38:55","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2601"},"modified":"2018-06-13T19:31:41","modified_gmt":"2018-06-13T19:31:41","slug":"using-docker","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/12\/23\/using-docker\/","title":{"rendered":"using docker"},"content":{"rendered":"<p>Although docker does have quite a few command options a small subset is all you need for general usage.\u00a0 The number of individual tasks you need is limited the following.<\/p>\n<div style=\"margin-left: 1cm;\">\n<ul>\n<li>download an image<\/li>\n<li>run the image<\/li>\n<li>remove the image<\/li>\n<li>build a new image<\/li>\n<li>monitoring images<\/li>\n<\/ul>\n<\/div>\n<p>Docker has a store that contains both their official containers as well as community supplied containers.\u00a0 The official containers contain a lot of very large well known software programs.<\/p>\n<div style=\"margin-left: 1cm;\">\n<ul>\n<li>Microsoft SQL server<\/li>\n<li>Oracle database<\/li>\n<li>Oracle Java 8<\/li>\n<li>WordPress<\/li>\n<li>Tomcat<\/li>\n<li>Owncloud<\/li>\n<li>gcc<\/li>\n<\/ul>\n<\/div>\n<p>The store is important as it contains a list of the containers that are available and what their name is, however, there is one more really important thing in the store.\u00a0 Each of the containers that are listed have the name of the container to be downloaded but more importantly they contain helpful notes.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2634 aligncenter\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-busybox.jpg\" alt=\"\" width=\"394\" height=\"303\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-busybox.jpg 394w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-busybox-300x231.jpg 300w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/><\/p>\n<p>Each container shows the command necessary for pulling down the image to your machine.\u00a0 This is important but other details are listed that might be helpful when actually running the container.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2641\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-pull-busybox.png\" alt=\"\" width=\"1032\" height=\"189\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-pull-busybox.png 1032w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-pull-busybox-300x55.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-pull-busybox-768x141.png 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-pull-busybox-1024x188.png 1024w\" sizes=\"(max-width: 1032px) 100vw, 1032px\" \/><\/p>\n<p>This might be how to run the image, how to extend the existing container to make a new container, descriptions or perhaps the license for this software.<\/p>\n<p>The good news is that all of the entries in the store show the command for retrieving that specific image.<\/p>\n<p>The only inconvenience is that you need elevated privileges to run the docker commands.\u00a0 This is actually as easy as using sudo.\u00a0 Easy but still somewhat inconvenient.\u00a0 Yet this too can be overcome with the tiniest of changes. Quite some time back Docker was changed to have the group docker for the docker daemon.\u00a0 The daemon has ownership of the read\/writable for that socket.<\/p>\n<p>Thus the solution is to simply add your user to the docker group.\u00a0 This is either done when creating your user or if your user already exists simply use the <a href=\"https:\/\/www.howtogeek.com\/50787\/add-a-user-to-a-group-or-second-group-on-linux\/\" target=\"_blank\" rel=\"noopener\">usermod<\/a> command to add the user to the group.\u00a0 This is explained in a really good post at the <a href=\"https:\/\/www.howtogeek.com\/50787\/add-a-user-to-a-group-or-second-group-on-linux\/\" target=\"_blank\" rel=\"noopener\">howtogeek<\/a> website.<\/p>\n<h2>General usage<\/h2>\n<p>One of the challenges for production environments as well development environments is to ensure that the exact identical setup exists.\u00a0 The neat thing about docker is that you can pull down all the specific tools by version. You simply provide a host and pull down your containers. If for any reason you need a different version or multiple versions you can run them on the same machine as a container regardless of if this would be possible as a simple install.<\/p>\n<p>Docker has a lot of different command options which can be used to monitor the containers but the most important one is actually running containers. Simply use the run parameter and then the container is run as a normal process.\u00a0 This actually isn&#8217;t very different from some installed program running but the containerization makes this a bit different.<\/p>\n<p>Containers are kept segregated from the host and from each other which means that they cannot actually do very much unless you allow the container to interact with system.\u00a0 Docker was not only done really well but also very granular.\u00a0 It is possible to allow the container to access a file, directory or even a port.<\/p>\n<p>The most simple container does not need any input to the outside world, but those types of jobs would be pretty rare.\u00a0 An example of this is the hello-world container provided by docker.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2646\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-helloworld.png\" alt=\"\" width=\"680\" height=\"378\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-helloworld.png 680w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-helloworld-300x167.png 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>This example doesn&#8217;t save anything to the file system nor read any input from a port.<\/p>\n<p>A more common use case would be to either access a directory or to map a configuration file from the host into a container.\u00a0 This is done by passing in the mapping or mappings by using the -v argument.<\/p>\n<pre>-v &lt;host source directory&gt;:&lt;container dest directory&gt;<\/pre>\n<p>This can be done with either a directory or just a simple file &#8211; in either case the syntax is the same.\u00a0 The same format is used for mapping a port from the host machine to the container.<\/p>\n<pre>-p &lt;host port&gt;:&lt;container port&gt;<\/pre>\n<p>It is possible to also pass through environment variables to the container as well.<\/p>\n<pre>-e &lt;host variable&gt;:&lt;container variable&gt;<\/pre>\n<h2>Interactive container<\/h2>\n<p>In my opinion one of the neatest uses of containers is to run a graphical program like Eclipse. However as the container itself is not persistent Eclipse in a container is not very useful unless it is exposed to the host.<\/p>\n<p>In this case a directory from the host is mapped to the container workspace directory. Simply by mapping the local directory to the container makes Eclipse behave exactly as if it was a normal install on the host machine.<\/p>\n<p>Below is a small script to run the container and map a few directories and an environment variable into the container.\u00a0 This script below is fairly similar to the one in the <a href=\"https:\/\/store.docker.com\/community\/images\/fgrehm\/eclipse\" target=\"_blank\" rel=\"noopener\">docker store<\/a> for this container.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ncd ~cdock\r\nmkdir `pwd`\/.eclipse-docker &gt;\/dev\/null 2&gt;&amp;1\r\n\r\nmyip=`hostname -I | awk -e '{ print ($1)}'`\r\nxhost + $myip\r\n\r\nexport DISPLAY=$ip:0\r\n\r\nCMD=&quot;docker run -d --rm -e DISPLAY=$DISPLAY \\\r\n-v \/tmp\/.X11-unix:\/tmp\/.X11-unix \\\r\n-v `pwd`\/.eclipse-docker:\/home\/developer \\\r\n-v \/home\/cdock\/workspace:\/home\/developer\/workspace \\\r\nfgrehm\/eclipse:v4.4.1 &quot;\r\n\r\n$CMD \r\n<\/pre>\n<p>It is not alwtays obvious what host resources need to be mapped into a container.\u00a0 This particular container is just another example why it is useful to look at the notes that are in the store.<\/p>\n<p>The eclipse container is a great example of an user interactive task but not all containers have a gui.\u00a0 It is possible to run a program interactively but that program could also be bash.<\/p>\n<pre>docker run -i -t fgrehm\/eclipse:v4.4.1 \/bin\/bash<\/pre>\n<h2>Monitoring Docker<\/h2>\n<p>There are two different tasks to monitor.\u00a0 The first is what containers are downloaded to your computer.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2639\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-images-ps.png\" alt=\"\" width=\"1033\" height=\"132\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-images-ps.png 1033w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-images-ps-300x38.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-images-ps-768x98.png 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-images-ps-1024x131.png 1024w\" sizes=\"(max-width: 1033px) 100vw, 1033px\" \/><\/p>\n<p>This will however only show the images not which containers are actually running.\u00a0 The &#8220;ps&#8221; argument, much like the Linux ps command, is used to see which containers are actually running.<\/p>\n<p>The tasks that are running are probably a daemon or other process that runs in the background.\u00a0 Stopping the process is actually as simple as starting it.\u00a0 Simply ask docker to stop the process for the container id.<\/p>\n<pre>docker stop\u00a0ae527eb50499<\/pre>\n<p>After stopping a container it will no longer show up in the list of running tasks.\u00a0 However, if you ask right, you can see a list of the containers that have already been stopped.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2654\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-all.png\" alt=\"\" width=\"1239\" height=\"207\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-all.png 1239w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-all-300x50.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-all-768x128.png 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-all-1024x171.png 1024w\" sizes=\"(max-width: 1239px) 100vw, 1239px\" \/><\/p>\n<p>It is possible to clean up this list by removing the stopped processes.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2655 aligncenter\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-cleanup.png\" alt=\"\" width=\"495\" height=\"360\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-cleanup.png 495w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/10\/docker-ps-cleanup-300x218.png 300w\" sizes=\"(max-width: 495px) 100vw, 495px\" \/><\/p>\n<p>If you were limited to pre-created docker containers from the docker store this would still be a very powerful tool, but it is actually possible to either create your own containers or even extend existing containers.<\/p>\n<p>I will talk about that in my next article on docker.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although docker does have quite a few command options a small subset is all you need for general usage.\u00a0 The number of individual tasks you need is limited the following. download an image run the image remove the image build &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/12\/23\/using-docker\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[91],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2601"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2601"}],"version-history":[{"count":25,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2601\/revisions"}],"predecessor-version":[{"id":2898,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2601\/revisions\/2898"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}