When you start learning about Docker, you will definitely wonder 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
- Using Volume feature
- Using Git
NOTE: For our example we have used our qxf2_pom_essentials Docker image as a base image. This is Qxf2’s Base Image designed for running any Python based Selenium tests. To know more about this image refer our blog here.
1. Using COPY or ADD command:
You can use COPY or ADD command within Dockerfile to copy your file/code into the Docker container. The following Dockerfile example shows how to add the current working directory files and folders into the directory
/usr/Qxf2_POM of the container image.
#Content of Dockerfile #Use ADD or COPY command to get your code inside container image FROM qxf2rohand/qxf2_pom_essentials MAINTAINER Qxf2 Services #Add all files available at current location COPY . /usr/Qxf2_POM #Set working directory WORKDIR /usr/Qxf2_POM
NOTE: You need to keep your code and above Dockerfile in the same directory to get your code inside container image.
Create an image using the dockerfile by running the below command. Run the next command to create a container out of that image.
Command to build docker image:
docker build -t image-name path/to/Dockerfile
Command to create a container:
docker run -it image-name-which-provided-during-image-build
ls command to check if your code is copied over to the container or not? Fig 1 shows the screenshot of our code getting copied over into the container using COPY command.
ADD and COPY both works similar. ADD command has the additional capability of fetching remote URLs and extracting tarballs. COPY and ADD techniques are mostly used in production to build production-ready Docker Image.
2. Using Volume feature:
Docker volume feature allows sharing a local directory with the container. To use volume feature you need to specify the local directory which you want to mount and the location where it should mount within the container along with -v argument as shown in below command:
docker run -it -v path/to/local/dir:path/to/container/dir image-name
Following Fig 2 shows, how we mounted our local directory using Docker volume feature.
Docker volume feature 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.
3. Using Git:
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’t have git already installed.
#Content of Dockerfile #Use Git to get your code inside container image FROM qxf2rohand/qxf2_pom_essentials MAINTAINER Qxf2 Services #Install git RUN apt-get update \ && apt-get install -y git #Change directory and clone Qxf2 Public POM repo RUN mkdir /usr/Qxf2_POM \ && cd /usr/Qxf2_POM \ && git clone https://github.com/qxf2/qxf2-page-object-model.git #Set working directory WORKDIR /usr/Qxf2_POM
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.
Following Fig. 3 shows how we cloned Qxf2 Public POM using git within container image.
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.
Hope you like this blog. To know more about our qxf2_pom_essentials Docker image and how to use it for continuous integration and running Selenium tests refer our blogs Preparing a Docker image for running Selenium tests and Continuous Integration using Bitbucket Pipelines and Docker.
If you are a startup finding it hard to hire technical QA engineers, learn more about Qxf2 Services.
I love technology and learning new things. I explore both hardware and software. I am passionate about robotics and embedded systems which motivate me to develop my software and hardware skills. I have good knowledge of Python, Selenium, Arduino, C and hardware design. I have developed several robots and participated in robotics competitions. I am constantly exploring new test ideas and test tools for software and hardware. At Qxf2, I am working on developing hardware tools for automated tests ala Tapster. Incidentally, I created Qxf2’s first robot. Besides testing, I like playing cricket, badminton and developing embedded gadget for fun.