How To Include Files Outside Of Docker's Build Context ?
Last Updated :
27 Mar, 2024
In this guide, we'll walk through the steps to include files outside of Docker's build context on an AWS EC2 instance. This process allows you to access files located outside of the Docker build context within your Docker container. We'll demonstrate this with an Apache web server setup on an EC2 instance.
Understanding Of Primary Terminologies
- Docker: Docker is a containerized platform tool that manages the life cycle of containers. It packages the dependencies of an application into a single entity.
- Docker Image: Docker images are read-only templates that provide instructions for creating a container with the application dependencies.
- Dockerfile: It is a text or document file that contains the assembly of commands that are needed for an application with its packages and dependencies that help in making a Docker image.
Include Files Outside Of Docker's Build Context: A Step-By-Step Guide
Step 1: Login as an AWS User

Step 2: Login Password with your password respective to the provide AWS user.

Step 3: Create Instance by clicking on Launch Instance button.

Step 4: Choosing AMI such as Amazon Linux latest AMI and specify the no of instances as 1 and instance type in free tier.

Step 5: Create Key Pair by clicking on create key pair option.

Step 6: Choose Pem Key type format for here, After creating it automatically downloads the pem file into your local system. That can be further usable for remote login purpose using ssh protocol.

Step 7: Choose Key Pair that we created know, By providing this we have option to connect to this instance from our local terminal using ssh protocol.

Step 8: Configure Security Groups by clicking on edit option and provide the option values as shown in the below screenshot, It helps in to not face security issues from the AWS Instance.

Step 9: Review the Instance configuration and defintions once and then Launch Instance

Step 10: Connect Instance after once the created instance came to the running state.

Step 11: Navigate to EC2 Console by clicking on connect button.

Step 12: Switch Root with the following command:
sudo su -

Step 13: Install Docker
Ensure that the instance has Docker installed. If not, install Docker using:
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Step 14: Prepare Your Dockerfile
- Create A Folder name myfold in the host system i.e, In AWS Instance in directory "/root/myfold" and save index.html file with content anything. Here We are saving the file with content "Hello Guys, This is my index html page".

- Create a Dockerfile for your Apache web server setup.
- Include instructions to copy files from the host machine into the Docker container, such as:
FROM httpd:latest
COPY /path/to/your/files /usr/local/apache2/htdocs/
- The following screenshot illustrates on defining the Dockerfile with Outside directory files.

Step 15: Build and Run Docker Container
- Build your Docker image using the Dockerfile:
docker build -t my-apache-server .

- Run the Docker container:
docker run -d -p 80:80 my-apache-server

Step 16: Verify Apache Web Server Setup
- Access your EC2 instance's public IP address in a web browser to verify that the Apache web server is serving the files correctly.
- The following screenshot shows that we successfully able to access the apache server hosting web page.

Imperative Way Of Including Files Outside Of Docker's Build
Step 17: Create a Folder In Host System
Create a folder in the host system with name such as myrootdir. You can specify the files that you want to include in the docker container. Create a directory with following command:
mkdir myrootdir

Step 18: Run Container With Volume Bind ( -v )
Now, bind the directory of host system that we created with the directory docker container that we gonna create with the following command:
docker run -dit -v myrootdir:/usr/local/apache2/htdocs/ --name mycontainer2

Step 19: Check Container Status
Check the status created container with running the following command:
docker ps
- The following screenshot illustrates the container is running successfully without any errors.

Step 20: Inspect The Container
Now, to verify the docker container whether is mounted or not using inspect command with docker as provided below:
docker inspect mycontainer2
On the checking Output details of the container in the mount section you can see that "myrootdir" is specified. It means that volume mount is sucessful. The following illustrates it clearly.

Conclusion
By following these steps, you can include files located outside of Docker's build context within your Docker container running on an AWS EC2 instance. This approach allows for greater flexibility in managing and deploying applications using Docker while utilizing resources outside of the container environment.
Similar Reads
How to Create a Dockerfile in Node.js ?
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is something like a shellscript that gathers multiple commands into a single document to fulfill a single task. Prerequisite: Before you can Dockerize any application, you ne
4 min read
How to Mount a File in Docker?
A popular technology called Docker lets you package and execute programs in isolated environments known as containers. These containers ensure consistency across many contexts by incorporating all the components required to run the application, such as the runtime, code, system tools, and libraries.
6 min read
How to See RUN Command Output in Docker BuildKit
Docker build is one of the most crucial parts of the operation of containerizing applications. Docker's traditional build system has served developers well, but with projects growing in complexity, the need for better builds that are more efficient and flexible was well apparent. Enter BuildKit, the
6 min read
How To Comment In Dockerfile?
The purpose of comments in a Dockerfile is to both clarify and improve the readability of the instructions. It can also be used to stop execution when testing other code. The comments are meant to serve as a source of code line information. Comments are a frequent way for programmers to document the
3 min read
How do you Mount Volume in Docker Compose File ?
Docker is a containerization tool that encapsulates the application along with its dependencies and runtime into compact units called docker containers. On the other hand, Docker Compose is a service through which multiple Docker container applications can be managed easily. In this guide I will fir
4 min read
How To See Docker Image Contents?
In the evolving world of containerization, Docker has emerged as a tool for packaging and deploying applications. While creating and sharing Docker images has streamlined the development and deployment process. Contents of a Docker image are important for troubleshooting, optimizing, and ensuring se
3 min read
How to Link Multiple Container in Docker ?
Docker is a free software created by Docker Inc. and it enables clients to create free and confined environments where the users can deploy their applications. A container is a standard unit of software that packages up code and all its dependencies from one computing environment and runs it quickly
2 min read
How to Use AWS CLI in Docker Container ?
The AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal. Integrating AWS CLI within a Docker container can significantly streamline workflows, especially for development and deployment processes that rely on cloud infrastruct
4 min read
How to Use Ansible for Docker Container Management
Containerization has become one of the foundations for realizing scalable, reliable, and efficient application deployments in modern DevOps practice. Docker, as a leading containerization platform, enables developers to package applications and all dependencies into containers for consistency across
9 min read
How to Load a Docker Image: Step-by-Step Guide
Docker is essential for managing and deploying apps in containerized environments. One of Docker's core functions is working with images, which are standalone, portable, and executable software packages. The 'docker image load' command is a crucial tool for loading Docker images from a tarball into
4 min read