Docker: Using Commit To Keep Container Changes

Docker: Using Commit To Keep Container Changes


When you have limited resources it can be useful to use docker containers to separate the servers. For example, one for backend and one for frontend development. This way we can keep things clean. We can make use of docker images to create a development server with necessary dependencies installed and run. However, if anything new deployed inside container, and we want to use different port for each service (not using proxy like Nginx) we should add port mapping between host and container. However, port mapping can be done only at the docker run point. For example, my backend server container was started using this command:

docker run --privileged --name backend-server -h backend-server -p 1001:1001 -d -t --cpus=2 -m=8g backend-server-centos

After running, I had many changes like uploading servers, changing configs and etc. Just stopping the container running again with the same command with different options don't work. For example mapping a new port:

docker run --privileged --name backend-server:v0.2 -h backend-server -p 1001:1001 -p 9000:9000 -d -t --cpus=2 -m=8g backend-server-centos

It doesn't work. We lose the changes that we made to container because we are still running the old image backend-server-centos but with just different options.


We can create a new image from the existing container by committing. Get the container id docker ps and create a new image

docker commit c3f279d17e0a  backend-server:v.02

Now you can run this container with different runtime options, such as new port mapping, without losing old configuration and changes:

docker run --privileged --name backend-server:v0.2 -h backend-server -p 1001:1001 -p 9000:9000 -d -t --cpus=2 -m=8g backend-server:v0.2


It can be useful to commit a container’s file changes or settings into a new image. This way you can keep your old changes and run with different options.