Is it better to stop a container before it runs?

Hello,
Consider a runner with the following configuration:

stages:
  - build
  - deploy
cache:
  paths:
    - node_modules/
build-portal:
  stage: build
  script:
    ...

deploy-portal:
  stage: deploy
  script:
    - cd /home/Projects/
    - docker compose up -d portal

Wouldn’t it be better to run the docker compose down -d portal command before running the docker compose up -d portal command? Something like the following:

deploy-portal:
  stage: deploy
  script:
    - cd /home/Projects/
    - docker compose down -d portal
    - docker compose up -d portal

Thank you.

1 Like

What’s a runner? By default when using docker compose up, if something changed in the compose file, Docker will automatically stop the container an start a new container with the updated configuration.

1 Like

Hello,
Thank you so much for your reply.
I mean GitLab Runner. When a change is made to the website code, the runner updates the changes, but since the old website is already running in Docker, will the docker compose up -d portal command show the new website?

It depends on your compose file. If params within change, it will restart.

If the image changes und you use latest tag, you need docker compose up -d --pull to always use the latest image. I think also compose has an option like pull: always.

Hello,
Thanks again.
No, I haven’t included an option in my compose file to restart the container:

services:
  portal:
    container_name: Portal
    build:
      context: /home/Projects/
      dockerfile: Dockerfile
    ports:
      - "127.0.0.1:3000:3000"

Do I need to do that?

Yes, but docker compose down -d portal is incorrect; -d isn’t valid with down. Use:

deploy-portal:
  stage: deploy
  script:
    - cd /home/Projects/
    - docker compose down portal
    - docker compose up -d portal

This ensures the old container stops before starting a new one. :rocket:

1 Like

Hi,
Thank you so much for your reply.
You right. The correct command is as follows:

- docker compose down -v portal
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.