A Dockerfile consists of instructions to be followed to build a Docker image.
A Docker image is a self-contained package. It contains all the components, such as libraries, configurations, environment variables, required to run the code within. The image is built once, and if any file that is stored in the local machine needs to be used in the image, it needs to be copied into the image using commands in the Dockerfile.
A container is a running instance of the image. Containers utilize the host machine’s resources, but operate within their own dependencies and environment.
Let’s take the example of a C++ file with the following code, which outputs Hello World
.
#include <iostream> // Installing necessary librariesusing namespace std;int main() {// your code goes herecout << "Hello World" << endl; // Output of Hello World and end of line.return 0;}
If we want to run this in Docker, we would need the Docker container to have this file inside it’s environment.
The code, to be written in Dockerfile, for copying a file into an image, is as follows:
COPY <source-file-path-on-host> <destination-file-path-inside-image>
In the command above:
In source-file-path-on-host
, we need to give the path, on the host machine, of the file we want to copy.
In destination-file-path-on-host
, we need to give the path, inside Docker image, where we want to copy the file to. This path will include the directory, file name and extension, with which the file will be saved within image’s root directory.
We will add this line in our Dockerfile, alongside commands for running a C++ file, so that ans.cpp
(which is in the same directory) is copied into the Docker image. The C++ file is inside a folder named test
.
FROM ubuntu:latestRUN apt-get update && apt-get install sudo && \ #Installing libraries for running c++ filesudo apt install build-essential -y && \sudo apt -y install g++-10COPY test/ans.cpp ans.cpp ## File will be copied into the root directory of the imageRUN g++ -o new ans.cpp ## Syntax for running a cpp file. 'new' is the compiled code.#CMD command will be executed when image container is run.#Following command will prove that 'ans.cpp' file has been uploaded in image directory#It will also run the compiled code.CMD ls ans.cpp; ./new
In the following terminal, we build a Docker image using the previous Dockerfile and we run the image as a container.
The code, to be written in Dockerfile, for copying a folder into an image is as follows:
COPY <folder-destination-on-host> <folder-destination-inside-image>
In the command above:
In folder-destination-on-host
, we need to give the path, on the host machine, of the folder we want to copy.
In folder-destination-inside-image
, we need to give the path, inside Docker image, where we want to copy the folder to. This path will include the directory and folder name, with which the folder will be saved within image’s root directory.
Instead of copying the C++ file, we will be copying the test
folder itself. All contents of the folder will be copied into the image. Our Dockerfile will be as follows:
FROM ubuntu:latestRUN apt-get update && apt-get install sudo && \ #Installing libraries for running c++ filesudo apt install build-essential -y && \sudo apt -y install g++-10COPY test test ## Whole 'test' folder will be copied into the root directory of the imageRUN g++ -o new test/ans.cpp ## Syntax for running a cpp file. 'new' is the compiled code.#CMD command will be executed when image container is run.#Following command will prove that test folder, with the 'ans.cpp' file has been uploaded in image directory#It will also run the compiled code.CMD ls test/ans.cpp; ./new
In the following terminal, we build a Docker image using the previous Dockerfile and then run the image as a container.
If we need to copy all the files, from the folder where our Dockerfile is, to our Docker image, one method is to copy each file and folder one by one. This can be a very tedious and redundant task, so another method will be to use just one command. The code, to be written in Dockerfile, for copying all folders and files from the current directory into our Docker image is as follows:
COPY . <folder-destination-inside-image>
In the command above:
In folder-destination-inside-image
, we need to give the path which will specify the directory where we want to copy all our files and folders to.
The first .
will simply mean the current directory on host machine. If we want to copy all folders and files into our image’s root directory, we can do so by the command COPY .
.
For this example, we will have two empty text files, namely Guide.txt
and Guide22.txt
, along with the test
folder from our previous example, in the directory of the Dockerfile. We will be copying all these files and folders to our images root directory, inside a folder named guideFolder
. Our Dockerfile will be as follows:
FROM ubuntu:latestRUN apt-get update && apt-get install sudo && \ #Installing libraries for running c++ filesudo apt install build-essential -y && \sudo apt -y install g++-10COPY . guideFolder ## All files and folders will be copied to guideFolder folder.RUN g++ -o new guideFolder/test/ans.cpp ## Syntax for running a cpp file in this example. 'new' is the compiled code.#CMD command will be executed when image container is run.#Following command will prove that all files and folders are in the GuideFolder folder.#It will also run the compiled code.CMD ls guideFolder; ./new
In the following terminal, we build a Docker image using the previous Dockerfile and then run the image as a container.
We can copy both, folders and files, into our image’s directory. This is essential whenever we need to use any file from the local host in our container.
Free Resources