0

I've created a container from ubuntu:latest image. For this I've created this docker-compose.yml:

version: '2'

services:
  test_port:
    image: ubuntu:latest
    container_name: test_port
    tty: true
    privileged: true

Now I enter into container with docker exec -it <test_port_id> bash and I do some work into. Suddenly I realize that I need bind ports with host port, so I stop the container and I add the port section to my docker-compose.yml like this:

version: '2'

services:
  test_port:
    image: ubuntu:latest
    container_name: test_port
    tty: true
    privileged: true
    ports:
        - "12345:12345"

If now I run docker-compose up again, Docker recreates container test_port, giving it a new ID, this is, Docker create a new container and I lose all my work done in test_port.

My question is: is there any way to bind ports (or make changes in general) in existing container from docker-compose.yml without recreating container but changes taking effect?

EDIT: Creating an image from container is not an option. Original problem has volume section for time synchronization (npt) and if I construct a new container from this image it throws an error because of this.

Thanks in advance!

Carlos
  • 675
  • 1
  • 7
  • 27
  • Possible duplicate of [How do I assign a port mapping to an existing Docker container?](https://stackoverflow.com/questions/19335444/how-do-i-assign-a-port-mapping-to-an-existing-docker-container) – Zac Delventhal Jan 23 '18 at 18:00
  • Not exactly. Creating an image from a container is not possible because in the original problem I have `volumes`. This procedure generates errors, so this is not an option. – Carlos Jan 23 '18 at 18:03
  • The question is still a duplicate. There are other answers below the top one. I don't think you are going to get any answers besides what is there. – Zac Delventhal Jan 23 '18 at 18:08

1 Answers1

0

You have to understand that a container can be created or deleted at any times. So if you have to modify files in your container, you have to inject them during the build (dockerfile) or with a volume. This is how you can persist your changes.

Fiber Optic
  • 130
  • 5