T-Plaza, 6th Floor, Thika, Kenya
+254 703 412 771

Sharing local files on a docker container

Do Your Business, We Secure

Sharing local files on a docker container

Have you heard about a docker container? If not please check out our blog and video to help you learn what they are and what they entail.

In this blog we will dive deeper into docker containers where we will look at how to share local files on the containers we have created. Note that one of the advantages of working with docker is that you don’t have to have a service installed locally on the machine.

You can simply pull an image from the docker repository and share local files on a docker container already running and you are good to go.

. . .

The fun stuff…

Today, we will be looking at sharing files from our local computer and running these files on a container.

Think of this as having to host files on a web server that we do not want to run locally. We want to run the files inside the docker container. Pretty interesting right?

So first things first, we are going to pull an image from the docker hub. The command to do this is;

sudo docker pull <image_name>
Fig: pulling an image

Once we have done the above successfully, we are going to view the list of images pulled, we should see our nginx image.

sudo docker images
Fig: list of docker images

Let us go ahead and create a directory that we will use to share the local files with the docker container. For this we’ll run the following command;

sudo mkdir ourwebsite

We should now navigate to this created directory;

sudo cd ourwebsite

Once here, we will make a few other directories as well as files which we will then navigate to.

sudo mkdir html

sudo cd html

Fig: creating docker website files

Inside the html directory that we have created, we will create a file and call it index.html as shown above

sudo nano index.html 

Once we have created the index.html file we will go ahead and write just a few lines of code to ensure its up and running.

<h1>Welcome to our new website</h1>

Once we have completed the above steps successfully, we are going to map the local directory that has the local files to the nginx directory that acts as the web server directory. In order to accomplish this we will use the following command;

sudo docker run -v /<local_directory_path:nginx/webshare_directory:ro> -p 8080:80 -d nginx
Fig: mapping

What the above command does is that it maps the local directory to the nginx directory that hosts web files. Below is a breakdown of what the above tags mean.

  • v maps; the local directory to the required location on the nginx image.
  • -p 8080:80; maps network service port 80 in the container to 8080 on our system. Also its good to note that apache’s default port is 8080 so you can change this if this is a concern.
  • -d; detaches the container from the terminal so that we are free to use it. In non-interactive mode
  • ro; tells docker to run it in read-only mode
  • nginx; is the name of the image we’ll be running.

After running the above we can navigate to the localhost on our web browser and have a look at the files being hosted.

. . .


You might be wondering why in the world would we want to do this? From a security point of view the web application is running on an isolated sand boxed environment. So in case of a security breach only the web application would be compromised. Therefore, attackers would not be able to navigate laterally to other directories.

We hope this has been informative, we have shared a video about this on YouTube, please feel free to check it out. Remember to keep an eye out for blogs and videos on our blogs .Cheers!

Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


Powered by WhatsApp Chat

× WhatsApp Us...