{"id":228,"date":"2020-07-17T17:27:13","date_gmt":"2020-07-17T15:27:13","guid":{"rendered":"https:\/\/www.rothamel.com\/?p=228"},"modified":"2023-01-11T16:05:28","modified_gmt":"2023-01-11T15:05:28","slug":"container-teil-2-arbeiten-mit-docker-containern","status":"publish","type":"post","link":"https:\/\/www.rothamel.com\/index.php\/2020\/07\/17\/container-teil-2-arbeiten-mit-docker-containern\/","title":{"rendered":"Container \u2013 Part 2: Working with Docker-Containers"},"content":{"rendered":"\n<p>After showing the creation of an exemplary Docker container in the first part, this part of the article series now focuses on working with containers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Build<\/h4>\n\n\n\n<p>Creating a Docker container is done using the build command. For this, we must be in the Dockerfile folder, otherwise the location of the Dockerfile must also be specified.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker build -t opensuse-tomcat .<\/pre>\n\n\n\n<p>Docker now creates a corresponding Docker image named opensuse-tomcat. This now serves as a template for the containers we want to use (runtime environment(s)).<\/p>\n\n\n\n<p>If errors occur, e.g. the files to be added via ADD cannot be found, this is displayed in the form of corresponding messages on the console. The following is an example of a missing file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Step 13\/22 : ADD .\/webapp.war \/opt\/tomcat\/webapp\/\nADD failed: no source files were specified<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Images<\/h4>\n\n\n\n<p>If we now want to know which usable images already exist on the host system, enter the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker images<\/pre>\n\n\n\n<p>As feedback we get<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REPOSITORY      TAG    IMAGE ID     CREATED     SIZE\nopensuse-tomcat latest 662c7e16ad72 2 days ago  378MB\ndebian-tomcat   latest deeb279b66c2 3 days ago  319MB\nopensuse\/leap   15.2   9c0b2c564878 10 days ago 105MB\nopensuse\/leap   15.1   b1ee6ac1cd81 11 days ago 106MB<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Run<\/h4>\n\n\n\n<p>The start of a new container based on an image is done with run. Additionally, a port mapping can be specified with -p. If there are several ports, the p parameter is simply repeated<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -p 8080:8080 -p 8443:8443 opensuse-tomcat<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Name<\/h4>\n\n\n\n<p>Docker assigns randomly generated names to the running instances by default. If the container is to be specifically addressed by a name, it is possible to assign a name to it at startup using -name &lt;name&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -p 8080:8080 -p 8009:8009 -name tomcat01 opensuse-tomcat <\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Entrypoint<\/h4>\n\n\n\n<p>In the first part of this article series, we defined a so-called entrypoint in the Dockerfile. This is executed when the container is started. However, if you do not want to have this hardwired or overwrite it, the entrypoint parameter is available for this purpose. The associated argument defines what is to be executed at the end of the startup process. In this case we execute the shutdown.sh script of the Tomcat. The additional parameter rm specifies that the container is also removed after it has been shut down.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run --rm --entrypoint=\/opt\/tomcat\/bin\/shutdown.sh -p 8080:8080 -p 8009:8009 opensuse-tomcat<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Exec<\/h4>\n\n\n\n<p>If you want to run the above shell script in a running container (named tomcat01) exec is used, which I would prefer for terminating a running Tomcat.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker exec tomcat01 \/opt\/tomcat\/bin\/shutdown.sh<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Mount (v)<\/h4>\n\n\n\n<p>For the operation of a Tomcat, the log files are a helpful source, e.g. for troubleshooting. Here it makes sense that Tomcat writes to a directory specific to its instance. This can be specified externally with the v parameter. For this the external path and its mountpoint within the container is specified.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -it --rm -p 8080:8080 -p 8009:8009 -v D:\\docker\\tomcat01\\log:\/opt\/tomcat\/logs --name tomcat01 opensuse-tomcat<\/pre>\n\n\n\n<p>Each instance can thus be given its own log directory at startup.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Environment variables (env-file) and hostname (h)<\/h4>\n\n\n\n<p>If I now want to set additional environment variables from outside within the container, which e.g. the web application can access in order to adapt specifically depending on e.g. a stage information, then this and further environment variables and values can be specified by means of env-file and so-called properties files when starting the container. By means of the given entrypoint the defined environment variables are output in this example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run --entrypoint=\/usr\/bin\/env --env-file env-vars.properties -h testhost123 opensuse-tomcat<\/pre>\n\n\n\n<p>The following is an example of a Propierties file. The name of this file can be assigned arbitrarily and does not have to follow any defaults.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var1=1111<br>var2=2222<\/pre>\n\n\n\n<p>By calling \/usr\/bin\/env the defined environment variables are now output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">HOSTNAME=testhost123<br>var1=1111<br>var2=2222<br>HOME=\/root<\/pre>\n\n\n\n<p>The output hostname was not part of the properties file, it was passed by means of the h parameter when the container was started.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After showing the creation of an exemplary Docker container in the first part, this part of the article series now focuses on working with containers.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,3,10],"tags":[14,16,15],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-linux","category-netzwerk","tag-docker","tag-linux","tag-tomcat"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":14,"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions\/388"}],"wp:attachment":[{"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rothamel.com\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}