{"id":2434,"date":"2019-09-09T23:14:22","date_gmt":"2019-09-09T23:14:22","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=2434"},"modified":"2020-04-20T09:58:54","modified_gmt":"2020-04-20T09:58:54","slug":"docker-images","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=2434","title":{"rendered":"4 Docker Images"},"content":{"rendered":"<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=2423\">&lt; 3 Docker Run<\/a> | <a href=\"http:\/\/wiki.thomasandsofia.com\/?p=2448\">5 Docker Compose &gt;<\/a><\/p>\n<h1>19: Docker Images<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/course\/learn-docker\/learn\/lecture\/7894020#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894020#overview<\/a><\/p>\n<h2>How to create images<\/h2>\n<h3>Manually<\/h3>\n<ul>\n<li>Start with OS<\/li>\n<li>Upgrade apt repo<\/li>\n<li>Install dependencies<\/li>\n<li>Install Python<\/li>\n<li>Copy source code to \/opt<\/li>\n<li>Run application<\/li>\n<\/ul>\n<h3>Creating a Dockerfile<\/h3>\n<p>INSTRUCTION argument<\/p>\n<pre>FROM ubuntu\r\nRUN apt-get updateRUN install python\r\nRUN pip install flask\r\nRUN pip install flask-mysql\r\nCOPY . \/opt\/source-code\r\nENTRYPOINT FLASK_APP=\/opt\/source-code\/app.py flask run<\/pre>\n<ul>\n<li>All images must start with a Docker OS image\n<ul>\n<li>RUN Ubuntu<\/li>\n<\/ul>\n<\/li>\n<li>Install the dependencies<\/li>\n<li>Copy the source code<\/li>\n<li>Specify the entry point.<\/li>\n<\/ul>\n<h2>Layered Architecture<\/h2>\n<ul>\n<li>Each line of the file is a layer<\/li>\n<li>Each line of instruction is just the changes from the previous layer<\/li>\n<\/ul>\n<h2>Docker Build<\/h2>\n<p>Use the docker build command to create the image from the dockerfile.<\/p>\n<ul>\n<li>Each build step is cast (id&#8217;d?) so you can restart a build in the event a step failed or required modification.<\/li>\n<li>Only the layers below the changed layer need to be rebuilt.<\/li>\n<\/ul>\n<h2>What can you containerize?<\/h2>\n<ul>\n<li>Everything<\/li>\n<li>It is expected that one day, all applications will be containerized.\n<ul>\n<li>sure&#8230; riiigghhtt<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>20: Demo &#8211; Creating a new Docker Image<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894022#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894022#overview<\/a><\/p>\n<h3>Start with the OS<\/h3>\n<pre>docker run -it ubuntu bash<\/pre>\n<h3>Install Python<\/h3>\n<pre>apt-get update\r\napt-get install -y python<\/pre>\n<h3>install Python pip<\/h3>\n<pre># ubuntu\r\napt-get install -y python-pip\r\n# centos\r\nyum install -y python2-pi<\/pre>\n<h3>Install Flask<\/h3>\n<pre># ubuntu\r\npip install flask\r\n# centos\r\npip2 install flas<\/pre>\n<p>Create the Python Flask file<\/p>\n<p>\/opt\/app.py Code:<\/p>\n<pre>import os\r\nfrom flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route(\"\/\")\r\ndef main():\r\n   return \"Welcome\"\r\n@app.route('\/how are you')\r\ndef hello():\r\n   return 'I am good. How about you?'\r\n\r\nif __name__ == '__main__':\r\n   app.run()<\/pre>\n<h3>Run the script<\/h3>\n<pre>FLASK_APP=\/opt\/app.py flask run --host=0.0.0.0<\/pre>\n<h2>Access the website<\/h2>\n<p>Get the container IP<\/p>\n<pre># Open a new shell\r\n# get the container's id\r\ndocker ps\r\n# Use inspect to get the IP\r\ndocker inspect &lt;CONTAINER-ID&gt;\r\n#Open a browser on the docker host\r\nhttp:\/\/IP.ADD.RE.SS:5000<\/pre>\n<h2>Build the Image<\/h2>\n<p>This is done from the host machine.\u00a0 You can view each step by running &#8216;history&#8217;<\/p>\n<h3>Create your source folder<\/h3>\n<pre>mkdir -p ~\/docker\/src\/my-simple-flaskapp\r\ncd ~\/docker\/src\/my-simple-flaskapp<\/pre>\n<h3>Create the application &#8216;app.py&#8217; file (^C to write\/quit)<\/h3>\n<pre>cat &gt; app.py\r\nimport os from flask import Flask app = Flask(__name__) @app.route(\"\/\") def main(): return \"Welcome\" @app.route('\/how are you') def hello(): return 'I am good. How about you?' if __name__ == '__main__': app.run()<\/pre>\n<h3>Create the Docker build &#8216;Dockerfile&#8217; file<\/h3>\n<h4>Ubuntu<\/h4>\n<pre>cat &gt; Dockerfile\r\nFROM ubuntu\r\nRUN apt-get -y update\r\nRUN apt-get install -y python python-pip\r\nRUN pip install flask\r\nCOPY app.py \/opt\/app.py\r\nENTRYPOINT FLASK_APP=\/opt\/app.py flask run --host=0.0.0.0<\/pre>\n<h4>CentOS<\/h4>\n<pre>cat &gt; Dockerfile\r\nFROM centos\r\nRUN yum -y update\r\nRUN yum install -y python2\r\nRUN yum install -y python2-pip\r\nRUN pip2 install flask\r\nCOPY app.py \/opt\/app.py\r\nENTRYPOINT FLASK_APP=\/opt\/app.py flask run --host=0.0.0.<\/pre>\n<h3>Build the Image<\/h3>\n<ul>\n<li>-t = tag (aka image name)<\/li>\n<li>Note it starts with your docker account<\/li>\n<\/ul>\n<h4>Ubuntu<\/h4>\n<pre># If you are only running from your local repository\r\ndocker build . -t easyflask-u\r\n# Add your account id to push it to Docker Hub\r\ndocker build . -t account_id\/easyflask-u<\/pre>\n<h4>CentOs<\/h4>\n<pre># No Account ID required for local use only\r\ndocker build . -t easyflask-c\r\n# Add your account id to push it to Docker Hub\r\ndocker build . -t account_id\/easyflask-c<\/pre>\n<h2>Push the image to docker<\/h2>\n<h3>Log into your docker account<\/h3>\n<pre>docker login<\/pre>\n<h3>Push the image to your account<\/h3>\n<pre>docker push accountid\/easyflask-x<\/pre>\n<h1><\/h1>\n<h1>21 Online Lab<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/course\/learn-docker\/learn\/lecture\/15828598#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/course\/learn-docker\/learn\/lecture\/15828598#content<\/a><\/p>\n<h1><\/h1>\n<h1>22: Environmental Variables<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/course\/learn-docker\/learn\/lecture\/12240112#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/12240112#overview<\/a><\/p>\n<p>Looks like these need to be set in Python code<\/p>\n<h3>Define a bash-style variable in the Dockerfile:<\/h3>\n<pre>username = 'default_name'\r\nif os.environ.get('USERNAME'):\r\n   username = os.environ.get('USERNAME'<\/pre>\n<p>Run the image with the enviromnental variable<\/p>\n<pre>docker run -e USERNAME=Thomas sayhello<\/pre>\n<ul>\n<li>You can use the <code>docker inspect<\/code> command to see any environmental variables that had been set at run time.<\/li>\n<\/ul>\n<h1>23: Env Lab<\/h1>\n<p>&nbsp;<\/p>\n<h1>24: CMD vs ENTRYPOINT<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/12485580#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/12485580#overview<\/a><\/p>\n<ul>\n<li>Using CMD, any commands following the `docker run IMAGE` will over write the specified CMD.<\/li>\n<li>Using ENTRYPOINT, and command following `docker run IMAGE` will be appended to the entrypoint command.<\/li>\n<\/ul>\n<h2>CMD<\/h2>\n<ul>\n<li>New containers over-ride their Image&#8217;s CMD command by following it with the desired command\n<ul>\n<li>docker run ubuntu sleep 5<\/li>\n<\/ul>\n<\/li>\n<li>You can create new Images based on the &#8216;root&#8217; image and set the desired command\n<ul>\n<li>Dockerfile:\n<ul>\n<li>If you want this: CMD sleep 5<\/li>\n<li>Use this: CMD [&#8216;sleep&#8217;,&#8217;5&#8242;]\n<ul>\n<li>When using JSON format, you must separate the command &#8216;sleep&#8217; from the variable &#8216;5&#8217;<\/li>\n<li>DO NOT use: CMD [&#8216;sleep 5&#8217;]<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><code>docker run ubuntu-sleeper sleep 5<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>ENTRYPOINT<\/h2>\n<ul>\n<li>Using the &#8216;ENTRYPOINT&#8217; command, when you suffix the image name with a variable, that variable is appended to the ENTRYPOINT command\n<ul>\n<li>Dockerfile: ENTRYPOINT [&#8216;sleep&#8217;]<\/li>\n<li><code>docker run ubuntu-sleeper 5<\/code><\/li>\n<\/ul>\n<\/li>\n<li>The issue with this format, is if you do not append the value for the sleep command, you&#8217;ll see an error.\n<ul>\n<li>docker run ubuntu-sleeper\n<ul>\n<li>Error!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>To prevent this, use both the ENTRYPOINT CMD commands together\n<ul>\n<li>Dockerfile:\n<ul>\n<li>ENTRYPOINT [&#8216;sleep&#8217;]<\/li>\n<li>CMD [&#8216;5&#8217;]\n<ul>\n<li>Both must be entered in JSON format!!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>With this format, the default value will be &#8216;5&#8217;, but will be over-ridden if you include a value at the end of the run command\n<ul>\n<li>docker run ubuntu-sleeper\n<ul>\n<li>runs for 5 seconds<\/li>\n<\/ul>\n<\/li>\n<li>docker run ubuntu-sleeper 10\n<ul>\n<li>runs for 10 seconds<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>&#8211;entrypoint<\/h2>\n<ul>\n<li>To completely over ride the image&#8217;s ENTRYPOINT, you can use the &#8211;entrypoint run option.\n<ul>\n<li>docker run &#8211;entrypoint runthisinstead ubuntu-sleeper 10<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt; 3 Docker Run | 5 Docker Compose &gt; 19: Docker Images https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894020#overview How to create images Manually Start with OS Upgrade apt repo Install dependencies Install Python Copy source code to \/opt Run application Creating a Dockerfile INSTRUCTION argument FROM ubuntu RUN apt-get updateRUN install python RUN pip install flask RUN pip install flask-mysql ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=2434\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-2434","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/2434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2434"}],"version-history":[{"count":18,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/2434\/revisions"}],"predecessor-version":[{"id":2673,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/2434\/revisions\/2673"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}