{"id":876,"date":"2023-08-06T07:31:00","date_gmt":"2023-08-06T07:31:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=876"},"modified":"2023-08-06T07:31:00","modified_gmt":"2023-08-06T07:31:00","slug":"run-scripts-docker-arguments","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=876","title":{"rendered":"Run Custom Scripts In Docker With Arguments &#8211; ENTRYPOINT Vs CMD"},"content":{"rendered":"<p><strong>Use Case: <\/strong> You need to run a custom shell script as an entrypoint on your <a href=\"https:\/\/devopscube.com\/what-is-docker\/\" rel=\"noreferrer noopener\">Docker container<\/a> with arguments passed to the script. These arguments decide how the script runs inside the container.<\/p>\n<p>We will look into running custom shell scripts inside a Docker container with command line arguments in this guide.<\/p>\n<h2 id=\"dockerfilerun-shell-script\"><strong>Dockerfile &#8211; Run Shell Script<\/strong><\/h2>\n<p>In this example, we have a custom <a href=\"https:\/\/devopscube.com\/recommends\/kk-shell-scripting\/\">shell script<\/a> that accepts three command-line arguments ($1, $2 &amp; $3).<\/p>\n<p>The <code>while true<\/code> loop then runs indefinitely, printing the values of <code>arg1<\/code>, <code>arg2<\/code>, and <code>arg3<\/code> in each iteration with a one-second delay between each iteration.<\/p>\n<p><strong>Step 1:<\/strong> Create a <strong><code>script.sh<\/code><\/strong> file and copy the following contents.<\/p>\n<pre><code>#!\/bin\/bash\narg1=${1}\narg2=${2}\narg3=${3}\n# Run the script in an infinite loop\nwhile true; do\n    echo \"Argument 1: $arg1\"\n    echo \"Argument 2: $arg2\"\n    echo \"Argument 3: $arg3\"\n    sleep 1\ndone<\/code><\/pre>\n<p><strong>Step 2:<\/strong> You should have the <code>script.sh<\/code> is the same folder where you have the <strong>Dockerfile<\/strong>.<\/p>\n<p>Create the Dockerfile with the following contents.<\/p>\n<pre><code>FROM ubuntu:latest\nLABEL maintainer=\"Your Name &lt;contact@techiescamp.com&gt;\"\nRUN apt-get update &amp;&amp; apt-get install -y \\\n    &amp;&amp; rm -rf \/var\/lib\/apt\/lists\/*\n# Copy the script to the container\nCOPY .\/script.sh \/\nRUN chmod +x \/script.sh\n# Set the entrypoint to the script with CMD arguments\nENTRYPOINT [\"\/script.sh\"]\nCMD [\"hulk\", \"batman\", \"superman\"]<\/code><\/pre>\n<p>Here the <strong>entrypoint<\/strong> of the container is set to <code>\/script.sh<\/code>, which means that this script will be run whenever the container is started.<\/p>\n<p>The <code>CMD<\/code> line specifies default arguments for the script if none are provided when the container is started. In this case, the default arguments are <code>hulk<\/code>, <code>batman<\/code>, and <code>superman<\/code>.<\/p>\n<p><strong>Step 3:<\/strong> Let&#8217;s <a href=\"https:\/\/devopscube.com\/build-docker-image\/\">build a docker image<\/a> from this <strong>Dockerfile<\/strong> with the name script-demo.<\/p>\n<pre><code>docker build -t script-demo .<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Now let&#8217;s create a container named demo using script-demo image.<\/p>\n<pre><code>docker run --name demo -d script-demo<\/code><\/pre>\n<p>You can check the container logs using the following command.<\/p>\n<pre><code>docker logs demo -f<\/code><\/pre>\n<p><strong>Step 4:<\/strong> You can also pass the CMD arguments at the end of the docker run command. It will override the arguments passed in the Dockerfile. For example,<\/p>\n<pre><code>docker run --name demo -d script-demo batman spiderman hulk<\/code><\/pre>\n<p>Here <code>\"batman spiderman hulk\"<\/code> will override <code>\"hulk\", \"batman\", \"superman\"<\/code> present in the docker image<\/p>\n<blockquote><p><strong>Note<\/strong>: If you don&#8217;t want to pass arguments to the shell script, you can ignore the CMD arguments and execute the shell script directly using Entrypoint.<\/p><\/blockquote>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/run-scripts-docker-arguments\/\" target=\"_blank\" rel=\"noopener noreferrer\">Run Custom Scripts In Docker With Arguments &#8211; ENTRYPOINT Vs CMD \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/run-scripts-docker-arguments\/<\/p>\n","protected":false},"author":1,"featured_media":877,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=876"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/876\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/877"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}