Can docker compose the skip assembly if the image is present?

Is it possible to run docker-compose up

in such a way that it only builds if the image is missing from the repository?

I am working on a test script that will run in two different environments, locally on a laptop and on a server as part of test automation. My docker compose file looks something like this:

services:
  my-service1:
    build: "./local-repo1"
    image: "image1"
  my-service2:
    build: "./local-repo2"
    image: "image2"
  ...

      

If I run it locally where the local repo directories exist, it works fine. If I try to run it on a server where it pulls from the docker repository instead, it complains that it cannot find the build path. If I post the property build

it works fine on the server, but then it won't run locally unless I create the images beforehand.

Is there a way to do this, only try to build if the image doesn't already exist? If not, I can try some workarounds, but I'd rather only use one docker-compose file that handles each case.

+7


source to share


2 answers


You can use docker-compose pull

to extract images. Then, if they are already present, Compose will not try to build them again.

To really avoid re-assemblies, you can use --no-build

.

docker-compose pull
docker-compose up -d --no-build

      

Your real problem is that you are setting a build context, but then trying to use docker-compose without that build context present.



When docker-compose starts, even if it doesn't plan to build, it checks that the build context at least exists. If this does not happen, then it will fail.

All you have to do to satisfy this requirement is create an empty directory for any missing build context. This should make docker-compose happy enough to run.

mkdir -p local-repo1 local-repo2

      

+9


source


i am using docker-compose v2.1

when we run docker compose up, we check if the image exists or not. if it doesn't exist, then just build this image, otherwise it will skip this part

docker-compose



version: '2.1'

services:
  devimage:
    image: nodedockertest
    build: .
    environment:
      NODE_ENV: development
    ports:
      - 8000:8000

      

enter image description here

+1


source







All Articles