{"id":910,"date":"2020-08-13T05:51:34","date_gmt":"2020-08-13T05:51:34","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=910"},"modified":"2020-08-13T05:51:37","modified_gmt":"2020-08-13T05:51:37","slug":"dockerizd-service","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/08\/13\/dockerizd-service\/","title":{"rendered":"Dockeriz&#8217;d service"},"content":{"rendered":"<p>Someone asked me to define a simple docker pod for the service I wrote in GOLang <a href=\"https:\/\/blog.samarthya.me\/wps\/2020\/05\/20\/go-webservice-postget\/\">here.<\/a> I completely forgot about it, till today.<\/p>\n<p>So here is my first attempt at getting a docker image for the service.<\/p>\n\n\n<figure class=\"wp-block-pullquote has-background is-style-solid-color\" style=\"background-color:#f78da7\"><blockquote><p>The code is available <a href=\"https:\/\/github.com\/samarthya\/gws.git\">here<\/a>.<\/p><\/blockquote><\/figure>\n\n\n\n<p>My code space is organised as under, and I am using VS Code to write this docker file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u251c\u2500\u2500 Dockerfile\n\u251c\u2500\u2500 README.MD\n\u251c\u2500\u2500 gqs.code-workspace\n\u2514\u2500\u2500 src\n    \u251c\u2500\u2500 counter\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 count.go\n    \u2502\u00a0\u00a0 \u2514\u2500\u2500 count.service.go\n    \u251c\u2500\u2500 go.mod\n    \u251c\u2500\u2500 server.go\n    \u251c\u2500\u2500 user\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 user.data.go\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 user.go\n    \u2502\u00a0\u00a0 \u2514\u2500\u2500 user.service.go\n    \u2514\u2500\u2500 users.json\n\n<\/code><\/pre>\n\n\n\n<p>Let me first publish my <code>Dockerfile<\/code> and then I will explain it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Building the container image for the image.\nARG VERSION=alpine\n\n# Sets the base image\nFROM golang:${VERSION} AS baseimage\nRUN echo 'Building image...'\n\n# Environment variable for the home directory\nENV home \/server\nWORKDIR ${home}\n\nADD .\/src\/counter\/* .\/counter\/\nADD .\/src\/user\/* .\/user\/\nADD .\/src\/server.go .\/server.go\nADD .\/src\/go.mod .\/go.mod\nADD .\/src\/users.json .\/\n\nRUN go build -o main server.go  \n\nEXPOSE 8090\nENTRYPOINT &#91; \"\/server\/main\" ]<\/code><\/pre>\n\n\n\n<p>We start with the <code>FROM<\/code> using the <code>golang:alpine<\/code> image.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>ARG<\/code> instruction defines a variable that users can pass at build-time to the builder with the <code>docker build<\/code> command using the <code>--build-arg &lt;varname>=&lt;value><\/code> flag. The default value is <code>alpine<\/code> in our case.<\/li><li>The <code>FROM<\/code> instruction initialises a new build stage and sets the <em>Base Image<\/em> for subsequent instructions.<\/li><li>The <code>RUN<\/code> instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the <code>Dockerfile<\/code>. In our case it is a simple Text message <code>denoting build initiation<\/code>.<\/li><li>The <code>ENV<\/code> instruction sets the environment variable <code>home<\/code> to the  <code>\/server<\/code>. This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.<\/li><li>The <code>WORKDIR<\/code> instruction sets the working directory to <code>${home}<\/code>for any <code>RUN<\/code>, <code>CMD<\/code>, <code>ENTRYPOINT<\/code>, <code>COPY<\/code> and <code>ADD<\/code> instructions that follow it in the <code>Dockerfile<\/code>.<\/li><li>The multiple <code>ADD<\/code> instructions used copies files, directories or remote file URLs from <code>&lt;src><\/code> and adds them to the filesystem of the image at the path <code>&lt;dest><\/code>.<\/li><li>The <code>RUN <\/code>instruction used begins building the program binary.<\/li><li>The <code>EXPOSE<\/code> instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified. <\/li><li>The final command <code>ENTRYPOINT<\/code> allows to configure a container that will run as an executable.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Build image<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>docker build -t samarthya\/serviceingo:v1.0 .<\/code><\/pre>\n\n\n\n<p>Run the image<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d -p 9090:8090 --name smv samarthya\/serviceingo:v1.0<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>REPOSITORY                           TAG                                              IMAGE ID            CREATED             SIZE\nsamarthya\/serviceingo                v1.0                                             10bc4a2e8183        15 hours ago        409MB<\/code><\/pre>\n\n\n\n<p>docker inspect can elaborate more in the image create.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n    {\n        \"Id\": \"sha256:10bc4a2e8183df14784a0ec97856559e1d8bf8c3f3dded73ece5b74a01da0f46\",\n        \"RepoTags\": &#91;\n            \"samarthya\/serviceingo:v1.0\"\n        ],\n        \"RepoDigests\": &#91;\n            \"samarthya\/serviceingo@sha256:bac7\"\n        ],\n        \"Parent\": \"sha256:ba00\",\n        \"Comment\": \"\",\n        \"Created\": \"2020-08-12T14:33:20.916677548Z\",\n        \"Container\": \"1af\",\n        \"ContainerConfig\": {\n            \"Hostname\": \"1afe36040e10\",\n            \"Domainname\": \"\",\n            \"User\": \"\",\n            \"AttachStdin\": false,\n            \"AttachStdout\": false,\n            \"AttachStderr\": false,\n            \"ExposedPorts\": {\n                \"8090\/tcp\": {}\n            },\n            \"Tty\": false,\n            \"OpenStdin\": false,\n            \"StdinOnce\": false,\n            \"Env\": &#91;\n                \"PATH=\/go\/bin:\/usr\/local\/go\/bin:\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin\",\n                \"GOLANG_VERSION=1.15\",\n                \"GOPATH=\/go\",\n                \"home=\/server\"\n            ],\n            \"Cmd\": &#91;\n                \"\/bin\/sh\",\n                \"-c\",\n                \"#(nop) \",\n                \"ENTRYPOINT &#91;\\\"\/server\/main\\\"]\"\n            ],\n            \"Image\": \"sha256:ba0\",\n            \"Volumes\": null,\n            \"WorkingDir\": \"\/server\",\n            \"Entrypoint\": &#91;\n                \"\/server\/main\"\n            ],\n            \"OnBuild\": null,\n            \"Labels\": {}\n        },\n        \"DockerVersion\": \"19.03.12\",\n        \"Author\": \"\",\n        \"Config\": {\n            \"Hostname\": \"\",\n            \"Domainname\": \"\",\n            \"User\": \"\",\n            \"AttachStdin\": false,\n            \"AttachStdout\": false,\n            \"AttachStderr\": false,\n            \"ExposedPorts\": {\n                \"8090\/tcp\": {}\n            },\n            \"Tty\": false,\n            \"OpenStdin\": false,\n            \"StdinOnce\": false,\n            \"Env\": &#91;\n                \"PATH=\/go\/bin:\/usr\/local\/go\/bin:\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin\",\n                \"GOLANG_VERSION=1.15\",\n                \"GOPATH=\/go\",\n                \"home=\/server\"\n            ],\n            \"Cmd\": null,\n            \"Image\": \"sha256:ba00f\",\n            \"Volumes\": null,\n            \"WorkingDir\": \"\/server\",\n            \"Entrypoint\": &#91;\n                \"\/server\/main\"\n            ],\n            \"OnBuild\": null,\n            \"Labels\": null\n        },\n        \"Architecture\": \"amd64\",\n        \"Os\": \"linux\",\n        \"Size\": 409358142,\n        \"VirtualSize\": 409358142,\n        \"GraphDriver\": {\n            \"Data\": {\n                \"LowerDir\": \"\/var\/lib\/docker\/overlay2\/ca784789ba90274d9bb7ea66d6fd88bb802f04a81117b33e271fbcf05a52f0ec\/diff:\/var\/lib\/docker\/overlay2\/3148cf2fd8b0c66a0609dfcdf19cad2e6434117379765f4329eca82d750e2b1b\/diff:\/var\/lib\/docker\/overlay2\/6ff4ab6c9f99471ca4d4c06fefdfa80fe95b2eda9505e55d2b2b7746d4a3070d\/diff:\/var\/lib\/docker\/overlay2\/756a454e35cf1dfcf450829ddd10f90a85b8b41e447a01d3a1c483a06b6a6e0d\/diff:\/var\/lib\/docker\/overlay2\/4e64169defbd603b6c390e9e2447a2a1d7596b46a516013e03e43b15d2e1e0fa\/diff:\/var\/lib\/docker\/overlay2\/c0f2ddbe44ea7b5fb17a3074ceae1478c08d0dce890c5d17b25c32b17fe0904a\/diff:\/var\/lib\/docker\/overlay2\/7af1b9667dc01775de4c71554e10e54e3cc424000dd1823f26682065c74fd7e4\/diff:\/var\/lib\/docker\/overlay2\/42c28bbea609cc637ca22830bd78a5a8b9f1f15385d21abd8ddd37bb12917304\/diff:\/var\/lib\/docker\/overlay2\/bb96c77843dbe71872b0b5368519eee56e5f13883c8fd1eb2184724ff93dba70\/diff:\/var\/lib\/docker\/overlay2\/db8152e75c500db9e23be1063f963527a685c61552a12b1c13d23d43af621ef9\/diff:\/var\/lib\/docker\/overlay2\/6f5\/diff\",\n                \"MergedDir\": \"\/var\/lib\/docker\/overlay2\/c38\/merged\",\n                \"UpperDir\": \"\/var\/lib\/docker\/overlay2\/c38\/diff\",\n                \"WorkDir\": \"\/var\/lib\/docker\/overlay2\/c38\/work\"\n            },\n            \"Name\": \"overlay2\"\n        },\n        \"RootFS\": {\n            \"Type\": \"layers\",\n            \"Layers\": &#91;\n                \"sha256:506\",\n                \"sha256:0f7\",\n                \"sha256:1ba1\",\n                \"sha256:91f\",\n                \"sha256:02d0\",\n                \"sha256:1a7a\",\n                \"sha256:5e95\",\n                \"sha256:23a\",\n                \"sha256:a3e\",\n                \"sha256:4ea8\",\n                \"sha256:ea8c\",\n                \"sha256:dd2\"\n            ]\n        },\n        \"Metadata\": {\n            \"LastTagTime\": \"2020-08-12T14:33:20.942126827Z\"\n        }\n    }\n]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/\">https:\/\/docs.docker.com\/engine\/reference\/builder\/<\/a><\/li><li><a href=\"https:\/\/hub.docker.com\/layers\/golang\/library\/golang\/alpine\/images\/sha256-660d670cbe7d9f5d183bc4ab8603cbd05a3436404475a5718876cb58c14e9a11?context=explore\">https:\/\/hub.docker.com\/layers\/golang\/library\/golang\/alpine\/images\/sha256-660d670cbe7d9f5d183bc4ab8603cbd05a3436404475a5718876cb58c14e9a11?context=explore<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Someone asked me to define a simple docker pod for the service I wrote in GOLang here. I completely forgot about it, till today. So here is my first attempt at getting a docker image for the service. The code is available here. My code space is organised as under, and I am using VS [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":912,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[86,34],"tags":[82,23],"class_list":["post-910","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-docker","category-technical","tag-docker","tag-golang","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/912"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}