How to get your code inside a Docker Container

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:

  1. Using COPY or ADD command
  2. Using Volume feature
  3. 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

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

Use 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.

Fig. 1 Using COPY/ADD Method

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.

Fig. 2 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
#Set working directory

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.

Fig. 3 Using Git

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.

2 thoughts on “How to get your code inside a Docker Container

Leave a Reply

Your email address will not be published.