{"id":6778,"date":"2017-10-03T01:56:00","date_gmt":"2017-10-03T05:56:00","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=6778"},"modified":"2018-04-02T10:25:10","modified_gmt":"2018-04-02T14:25:10","slug":"how-to-get-the-code-inside-docker-container","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/how-to-get-the-code-inside-docker-container\/","title":{"rendered":"How to get your code inside a Docker Container"},"content":{"rendered":"<p>When you start learning about <a href=\"https:\/\/www.docker.com\/\">Docker<\/a>, you will definitely\u00a0wonder about how to get your code within the Docker container. This blog will guide you about the different ways to get your code inside the Docker container and which method you need to select according to your role\/situation.<\/p>\n<hr \/>\n<h3>Methods to get your code inside Docker container:<\/h3>\n<ol>\n<li>Using COPY or ADD command<\/li>\n<li>Using Volume\u00a0feature<\/li>\n<li>Using Git<\/li>\n<\/ol>\n<p><strong>NOTE:<\/strong> For our example we have used our <a href=\"https:\/\/hub.docker.com\/r\/qxf2rohand\/qxf2_pom_essentials\/\">qxf2_pom_essentials<\/a> Docker image as a base image.\u00a0<span>This is Qxf2&#8217;s Base Image designed for running any Python based Selenium tests. To know more about this image refer our blog <a href=\"https:\/\/qxf2.com\/blog\/preparing-a-docker-image-for-running-selenium-tests\/\">here<\/a>.<\/span><\/p>\n<hr \/>\n<h3>1. Using COPY or ADD\u00a0command:<\/h3>\n<p>You can use COPY or ADD command within Dockerfile to copy your\u00a0file\/code into the Docker container. The following Dockerfile example shows how to\u00a0add the current working directory files and folders into the directory<code>\/usr\/Qxf2_POM<\/code> of the container image.<\/p>\n<pre lang=\"python\">#Content of Dockerfile\r\n#Use ADD or COPY command to get your code inside container image\r\nFROM qxf2rohand\/qxf2_pom_essentials\r\nMAINTAINER Qxf2 Services\r\n\r\n#Add all files available at current location\r\nCOPY . \/usr\/Qxf2_POM\r\n\r\n#Set working directory\r\nWORKDIR \/usr\/Qxf2_POM\r\n<\/pre>\n<p><strong>NOTE:<\/strong> You need to keep your code and above Dockerfile in the same directory to get your code inside container image.<\/p>\n<p>Create an image using the dockerfile by running the below command. Run the next command to create a container out of that image.<\/p>\n<p><strong>Command to build docker image:<\/strong><\/p>\n<pre lang=\"python\">docker build -t image-name path\/to\/Dockerfile\r\n<\/pre>\n<p><strong>Command to create a container:<\/strong><\/p>\n<pre lang=\"python\">docker run -it image-name-which-provided-during-image-build\r\n<\/pre>\n<p>Use <code>ls<\/code> command to check if your code is copied over to the container or not? Fig 1\u00a0shows the screenshot of our code getting copied over into the container using COPY command.<\/p>\n<figure id=\"attachment_6793\" aria-describedby=\"caption-attachment-6793\" style=\"width: 829px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/copy-mtd-1.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-6793 size-full\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/copy-mtd-1.png\" alt=\"\" width=\"829\" height=\"78\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/copy-mtd-1.png 829w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/copy-mtd-1-300x28.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/copy-mtd-1-768x72.png 768w\" sizes=\"auto, (max-width: 829px) 100vw, 829px\" \/><\/a><figcaption id=\"caption-attachment-6793\" class=\"wp-caption-text\">Fig. 1 Using COPY\/ADD Method<\/figcaption><\/figure>\n<p>ADD and COPY both works similar. ADD command has the additional capability of\u00a0fetching remote URLs and extracting tarballs. COPY and ADD techniques are mostly used in production to build production-ready Docker Image.<\/p>\n<hr \/>\n<h3>2. Using Volume feature:<\/h3>\n<p><a href=\"https:\/\/docs.docker.com\/engine\/admin\/volumes\/volumes\/\">Docker volume feature<\/a> allows\u00a0sharing a local directory with the container. To use volume\u00a0feature you need to specify the local directory which you want to mount and\u00a0the location where it should mount within the container along with -v argument as shown in below command:<\/p>\n<pre lang=\"python\">docker run -it -v path\/to\/local\/dir:path\/to\/container\/dir image-name\r\n<\/pre>\n<p>Following Fig 2 shows, how we mounted our local directory using Docker volume feature.<\/p>\n<figure id=\"attachment_6783\" aria-describedby=\"caption-attachment-6783\" style=\"width: 832px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/volume-feature.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6783\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/volume-feature.png\" alt=\"\" width=\"832\" height=\"89\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/volume-feature.png 832w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/volume-feature-300x32.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/volume-feature-768x82.png 768w\" sizes=\"auto, (max-width: 832px) 100vw, 832px\" \/><\/a><figcaption id=\"caption-attachment-6783\" class=\"wp-caption-text\">Fig. 2 Using Docker volume feature<\/figcaption><\/figure>\n<p>Docker volume\u00a0feature is very useful during development, you can use your favorite editor to edit the code from the host machine and run and check output inside the Docker container.<\/p>\n<hr \/>\n<h3>3. Using Git:<\/h3>\n<p>Using Git, you can clone your code repository within container image. This does entail a security risk if you plan on hosting your Docker image publicly. The following dockerfile example shows, how to get a public Git repository. We also added a step to install git, as our qxf2_pom_essentials image doesn&#8217;t have git already installed.<\/p>\n<pre lang=\"python\">#Content of Dockerfile\r\n#Use Git to get your code inside container image\r\nFROM qxf2rohand\/qxf2_pom_essentials\r\nMAINTAINER Qxf2 Services\r\n\r\n#Install git\r\nRUN apt-get update \\\r\n    && apt-get install -y git\r\n\r\n#Change directory and clone Qxf2 Public POM repo\r\nRUN mkdir \/usr\/Qxf2_POM \\\r\n    && cd \/usr\/Qxf2_POM \\\r\n    && git clone https:\/\/github.com\/qxf2\/qxf2-page-object-model.git\r\n\r\n#Set working directory\r\nWORKDIR \/usr\/Qxf2_POM\r\n<\/pre>\n<p>Now create an image using above dockerfile and run it to create a container using the build and run commands mentioned in ADD or COPY method.<\/p>\n<p>Following Fig. 3 shows how we cloned Qxf2 Public POM using git within container image.<\/p>\n<figure id=\"attachment_6784\" aria-describedby=\"caption-attachment-6784\" style=\"width: 971px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/using-git.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-6784\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/using-git.png\" alt=\"\" width=\"971\" height=\"65\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/using-git.png 971w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/using-git-300x20.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/08\/using-git-768x51.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><\/a><figcaption id=\"caption-attachment-6784\" class=\"wp-caption-text\">Fig. 3 Using Git<\/figcaption><\/figure>\n<hr \/>\n<p>In short, for production use ADD\/COPY method, for the development use docker volume feature and use the Git method only as a last resort.<\/p>\n<hr \/>\n<h3>References:<\/h3>\n<ol>\n<li> <a href=\"https:\/\/forums.docker.com\/t\/best-practices-for-getting-code-into-a-container-git-clone-vs-copy-vs-data-container\/4077\/15\">https:\/\/forums.docker.com\/t\/best-practices-for-getting-code-into-a-container-git-clone-vs-copy-vs-data-container\/4077\/15<\/a><\/li>\n<li> <a href=\"http:\/\/blog.cloud66.com\/how-to-get-code-into-a-docker-container\/\"> http:\/\/blog.cloud66.com\/how-to-get-code-into-a-docker-container<\/a><\/li>\n<\/ol>\n<hr \/>\n<p>Hope you like this blog. To know more about our <a href=\"https:\/\/hub.docker.com\/r\/qxf2rohand\/qxf2_pom_essentials\/\">qxf2_pom_essentials<\/a> Docker image and how to use it for continuous integration and running Selenium tests refer our blogs\u00a0<a href=\"https:\/\/qxf2.com\/blog\/preparing-a-docker-image-for-running-selenium-tests\/\">Preparing a Docker image for running Selenium tests<\/a> and <a href=\"https:\/\/qxf2.com\/blog\/continuous-integration-using-bitbucket-pipelines-and-docker-image\/\">Continuous Integration using Bitbucket Pipelines and Docker<\/a>.<\/p>\n<p><strong>If you are a startup finding it hard to hire technical QA engineers, learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2 Services<\/a>.<\/strong><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>When you start learning about Docker, you will definitely\u00a0wonder about how to get your code within the Docker container. This blog will guide you about the different ways to get your code inside the Docker container and which method you need to select according to your role\/situation. Methods to get your code inside Docker container: Using COPY or ADD command [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140,30],"tags":[],"class_list":["post-6778","post","type-post","status-publish","format-standard","hentry","category-docker","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/6778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=6778"}],"version-history":[{"count":24,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/6778\/revisions"}],"predecessor-version":[{"id":15555,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/6778\/revisions\/15555"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=6778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=6778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=6778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}