Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
Docker Compose not starting mongo service even main service depends on it

I am trying to construct ci process to build, test and publish my .NET Core app using Docker-Compose and bash scripts.

I have UnitTests, IntegrationTests and XApi projects in a folder and have created DockerFile and docker-compose.yml like below.

IntegrationTests are dependent to mongointegration, so I added links and depends_on attributes to testandpublish service in docker-compose.yml.

When I try to docker-compose up or docker-compose up testandpublish, it fails to connect mongo. (DockerFile - step 10), mongo service has not been started yet (Don't understand why)

In step 10, if I change RUN to CMD, it can connect to mongo, docker-compose works fine. But this time I cannot detect tests are failed or succeed in my sh script, because now it does not break docker-compose up command..

My question is: Why docker compose does not start service mongointegration? And if it is impossible, how can I understand that service testandpublish failed? Thanks.

Structure:

XProject
  -src
    -Tests
      -UnitTests
      -IntegrationTests
    -Dockerfile
    -docker-compose.yml
    -XApi

My Dockerfile content is (I have added line numbers to explain problem here):

1.FROM microsoft/dotnet:1.1.0-sdk-projectjson
2.COPY . /app
3.WORKDIR /app/src/Tests/UnitTests
4.RUN ["dotnet", "restore"]
5.RUN ["dotnet", "build"]
6.RUN ["dotnet", "test"]
7.WORKDIR /app/src/Tests/IntegrationTests
8.RUN ["dotnet", "restore"]
9.RUN ["dotnet", "build"]
10.RUN ["dotnet", "test"]
11.WORKDIR /app/src/XApi
12.RUN ["dotnet", "restore"]
13.RUN ["dotnet", "build"]
14.CMD ["dotnet", "publish", "-c", "Release", "-o", "publish"]

and my docker-compose.yml

version: "3"
services:
  testandpublish:
    build: .
    links:
      - mongointegration
    depends_on:
      - mongointegration
  mongointegration:
    image: mongo
    ports: 
      - "27017:27017"
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The image build phase and the container run phase are two very seperate steps in docker-compose.

Build and Run Differences

The build phase creates each of the image layers from the steps in the Dockerfile. Each happens in standalone containers. None of your service config, apart from the build: stanza specific to a services build, is available during the build.

Once the image is built, it can be run as a container with the rest of your docker-compose service config.

Instead of running tests in your Dockerfile, you could create a script to use as the CMD that runs all your test steps in the container.

#!/bin/sh
set -uex
cd /app/src/Tests/UnitTests
dotnet restore
dotnet build
dotnet test
cd /app/src/Tests/IntegrationTests
dotnet restore
dotnet build
dotnet test"
cd /app/src/XApi
dotnet restore
dotnet build
dotnet publish -c Release -o publish

If the microsoft/dotnet:1.1.0-sdk-projectjson image is Windows based you might need to convert this to the equivalent CMD or PS commands.

Container Dependencies

depends_on doesn't work quite as well as most people assume it will. In it's simple form, depends_on only waits for the container to launch before moving onto starting the dependent container. It's not smart enough to wait for the process inside the container be ready. Proper dependencies can be done with a healthcheck and a condition.

services:
  testandpublish:
    build: .
    links:
      - mongointegration
    depends_on:
      mongointegration:
        condition: service_healthy
  mongointegration:
    image: mongo
    ports:
      - "27017:27017"
    healthcheck:
      test: ["CMD", "docker-healthcheck"]
      interval: 30s
      timeout: s
      retries: 3

Using the Docker health check script, after it's been copied into the container via a Dockerfile.

#!/bin/bash
set -eo pipefail

host="$(hostname --ip-address || echo '127.0.0.1')"

if mongo --quiet "$host/test" --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 1)'; then
    exit 0
fi

exit 1
over 4 years ago · Santiago Trujillo Relatório

0

RUN steps are executed when Docker builds the image and no containers are available yet. Instead CMD step is executed on the run time and Docker Compose has already started depending mongointegration container.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda