Docker

How to Modify Docker Image Without Dockerfile?

Dockerfile is the text document that includes commands and instructions to build a Docker image. The Docker image is essentially the template for creating the Docker containers. It is built from the Dockerfile and includes all the files, and dependencies essential to execute the application. Sometimes, users want to modify the Docker image without Dockerfile. In this situation, Docker allows them to perform this operation.

This article will demonstrate how to modify a Docker image without Dockerfile.

How to Modify Docker Image Without Dockerfile?

To modify the Docker image without the Dockerfile, check out the provided instructions:

    • Create a program file.
    • Create a Dockerfile.
    • Build a Docker image.
    • Build and Run a Docker container.
    • Make changes in the program file.
    • Copy the program file to a container using the “docker cp <program-file-path> <container-name>:<container-path>” command.

Step 1: Create Program File

First, create an “index.html” program file and paste the below snippet into it:

<!DOCTYPE html>
<html>
<body>

<h1>LinuxHint</h1>

</body>
</html>

 
Step 2: Create Dockerfile

Next, create a new file named “Dockerfile” and paste the below code into it:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

 
In the above code:

    • FROM” command is utilized to specify a base image for the container.
    • COPY” instruction pastes the “index.html” file into the container path.
    • ENTRYPOINT” sets the execution point for the container.

Step 3: Build Docker Image

Then, build the Docker image from Dockerfile using the provided command:

docker build -t new-img .

 
Here, “-t” is utilized to specify the image name, i.e., “new-img”:


Step 4: Build Docker Container

Now, utilize the following command to build and run the Docker container using Docker image:

docker run --name new-cont -p 80:80 new-img

 
Here:

    • –name” is used to specify the container name, i.e., “new-cont”.
    • -p” is utilized to assign a port which is “80:80”.
    • new-img” is the Docker image:

 

Step 5: Verification

Finally, view the deployed application by redirecting to the allocated port on the browser.

In the below screenshot, the deployed application can be seen:


To modify the Docker image without the Dockerfile, follow the provided steps.

Step 6: Make Changes in the Program File

Now, make the desired modification or changes in the “index.html” program file. For instance, we have added one more line in the program file:

<!DOCTYPE html>
<html>
<body>

<h1>LinuxHint</h1>
<h2>This is our website</h2>

</body>
</html>

 
Step 7: Copy Program File to Container

Then, execute the “docker cp <program-file-path> <container-name>:<container-path>” command to copy the program file to the container:

docker cp ./index.html new-cont://usr/share/nginx/html

 
Here:

    • ./index.html” is the path of the program file.
    • new-cont” is the container name.
    • //usr/share/nginx/html” is the container’s path.

The above-stated command will copy the “index.html” file from the current directory on the host machine to the “/usr/share/nginx/html” directory inside the “new-cont” container:


Step 8: Verification

Lastly, verify changes on the browser:


In the above output, the updated content can be seen. We have successfully modified the Docker image without Dockerfile.

Conclusion

To modify the Docker image without the Dockerfile, first, make changes in the program file. Then, utilize the “docker cp <program-file-path> <container-name>:<container-path>” command. This command will copy the program file changes to the container. This article demonstrated the method to modify a Docker image without Dockerfile.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.