skip to Main Content

I have a composer file with four services. I need to OPEN one of them to outside by settings ports.

After changing .yml file, do I need to ‘rebuild the container’ (docker-compose down/up) or do I just need to stop/start? (docker-compose stop/start)?

Specifically, what I neet to do accesible to outside is a Posgree Server. This is my actual postgres service definition in .yml:

mydb:
    image: postgres:9.4
    environment:
      - POSTGRES_PASSWORD=myPassword
    volumes:
      - db-data:/var/lib/postgresql/data

I think I just need to change it to:

  mydb:
    image: postgres:9.4
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=myPassword
    volumes:
      - db-data:/var/lib/postgresql/data

I’m worried of loosing data on ‘db-data’ volume, or connection to the other services, if I use down/up.

Also, there are 3 other services specified in the .yml file. If it is necessary to REBUILD (without loosing data in db-data, of course), I don’t want to touch these other containers. In this case, which would be the steps?:

  • First, rebuild ‘mydb’ container with ‘docker run’ (Could you provide me the right command, please?)
  • Modify the .yml as stated before, just adding the ports
  • Perform a simple docker-compose stop/start

Could you help me, please?

2

Answers


  1. You have to rebuild it.

    For that reason the best practice is to map all the mount points and resources externally, so you can recreate the container (with changed parameters) without any loss of data.

    In your scenario I see that you put all the data in an external docker volume, so I think you could recreate it with changed ports in a safe way.

    Login or Signup to reply.
  2. If you’re only changing settings like ports:, it is enough to re-run docker-compose up -d again. Compose will figure out which things are different from the existing containers, and destroy and recreate only those specific containers.

    If you’re changing a Dockerfile or your application code you may specifically need to docker-compose build your application or use docker-compose up -d --build. But you don’t specifically need to rebuild the images if you’re only changing runtime settings like ports:.

    docker-compose down tears down your entire container stack. You don’t need it for routine rebuilds or container updates. You may want to intentionally shut down the container system (and free up host ports, memory, and other resources) and it’s useful then.

    docker-compose stop leaves the containers in an unusual state of existing but without a running process. You almost never need this. docker-compose start restarts containers in this unusual state, and you also almost never need it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search