If you've made changes inside a running Docker container and want to save those changes, you can commit the container as a new image. In this short guide, we'll walk through how to do that:
Let's create a simple Express app with just one file, index.js, and then build a Docker image from it. After that, we'll run a container using that image. Once the container is up and running, we'll make changes to the code inside the container and then commit those changes into a new Docker image.
index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send("Welcome to Home Page");
});
app.listen(8080, () => console.log("Server Listening on 8080"));
Dockerfile
FROM node:14-alpine
WORKDIR '/usr/app/'
COPY './package.json' ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]
After creating the Dockerfile, run the build command:
docker build -t kevin3302/express-app .
Now, let's run the container:
docker run -p 5000:8080 kevin3302/express-app
We can access the website at http://localhost:5000
.
Now that our container is running, the next step is to access it and make the necessary code changes. To do that, we first need to find the container ID.
Step-1: Check the container ID
- To get the container ID of the running container, In a new terminal run:
docker ps
This will list all running containers with their IDs, names, and other details. We'll need the container ID to access the container and modify the code inside.
Now, we'll enter the shell of the running container to modify the code directly inside it.
Step 2: Enter the running container
- Use the following command to open a interactive shell inside the running container:
docker exec -it f72330919ee5 sh
Replace f72330919ee5 with your actual container ID if it's different.
Step 3: Modify the code inside the container
Once inside the container, open index.js using a text editor like vi or nano.
In the home route, change the response text from:
res.send("Welcome to Home Page");
to:
res.send("Hello World");
Step 4: Commit the running container as a new image
- In a new terminal (outside the container), run:
docker commit f72330919ee5
Using the running container ID with docker commit creates a new Docker image from the current state of the running container.
- We can tag the newly created image using:
docker tag <new-image-id> <tag-name>
- Also, we can change the startup command for the new image by specifying it with
-c flag
:
docker commit -c 'CMD <new-startup-command>' <new-image-id>
Step 5: Run the new container
- Now, run a container from the newly created image using:
docker run -p 5001:8080 <new-image-id or tag>
This will start the container with your modified code.
We can access the modified website at http://localhost:5001
Conclusion
Committing a running container to a new image is a quick way to save changes made inside a container. However, for development purposes, a better approach is to use Docker volumes, which allows us to modify the code on our host machine and see the changes reflected instantly inside the container - without needing to rebuild or commit.
Top comments (0)