Open In App

How To Include Files Outside Of Docker’s Build Context ?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

AWS-login-in

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

Login_password

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

Create-Instance

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

Choosing-AMI

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

Create-Key-Pair

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.

Choose-pem-key

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.

Choosing-Created-Key-Pair

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.

Configuring-Security-groups

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

Launching-Instance

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

Connect-Instance

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

EC2-Console

Step 12: Switch Root with the following command:

sudo su -

Switching-Root

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

Installing-Docker

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

Outside-Dir-File

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

Configure-Dockerfile

Step 15: Build and Run Docker Container

  • Build your Docker image using the Dockerfile:
    docker build -t my-apache-server .

Successfully-Build-DockerImage

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

Successfully-Deployed-Docker-Container

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.

Accessing-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

Creating-Directory

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 

volume-mount

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.

Checking container status

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.

Docker inspect

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.

Docker Build – FAQ’s

What is Docker build context?

Docker build context is a set of files and directories that sent to the Docker daemon for building Docker images.

Can I include files from outside the build context in my Docker image?

No, Docker’s build context supports in including files and directories within its specified path.

How can I prevent sensitive files from being included in the Docker build context?

By excluding sensitive files by specifying in a .dockerignore file listing files and directories to be ignored during the build process.

What is the purpose of a Dockerfile?

A Dockerfile is a text document file that contains assemble instructions for building a Docker image.

Can I use environment variables in a Dockerfile?

Yes, You can use environment variables in a Dockerfile for configuring and customizing Docker image builds.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads