Docker: how to modify an existing container

This is a problem I had lots of times, and it is possible that I’m not the only one. Scenario:

  1. I create a container;
  2. I do some work in the container;
  3. I need to connect from outside, but I realize that I didn’t map the port.

Or maybe I did, but I need to map it to another port. Or maybe I want to have a volume.

Docker doesn’t natively allow to remap ports or create volumes if a container is already running. When using Docker on localhost for testing, this sucks.

But don’t cry: you don’t need to create a new container.

All you need to do is:

  1. docker stop container bad_container
  2. docker commit bad_container good_image
  3. docker run --name good_container -d -p ... -v ... good_image

Enjoy!

Advertisements

2 thoughts on “Docker: how to modify an existing container

  1. This is great tip. But I had a setup in which I linked another container to this one using it’s name. So unless I dropped the old (stopped) container I could not start the new one because docker (obviously) does not allow to have 2 running instances to have the same name.

    • Sorry for being so late in approving the comment and answering. But I don’t have a suggestion. Honestly I only use Docker to perform simple tests without installing software directly in my system.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s