Prevent container startup - docker-compose

I have this docker compose:
myservice:
restart: "no"
With no the service will start anyway (but it won't restart)
How can I prevent the service to start at all?
Note: the reason I want to do this, for those curious, is that I want to make this flag configurable via an env var:
myservice:
restart: "${RESTART_SERVICE:-no}"
And then pass the right value to start the service.

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restart.
https://docs.docker.com/config/containers/start-containers-automatically/
So it's only when containers exit or when Docker restart.
But you have two options for what you want to do:
First only start the service you want:
docker-compose up other-service
This don't use ENV as you want (unless you have a script for run the docker-compose up ).
if [[ $START == true ]]; then
docker-compose up
else
docker-compose up other-service
fi
But as mentioned here and here, you can overwrite the entrypoint
So you can do something like:
services:
alpine:
image: alpine:latest
environment:
- START=false
volumes:
- ./start.sh:/start.sh
entrypoint: ['sh', '/start.sh']
And and start.sh like:
if [ $START == true ]; then
echo ok # replace with the original entrypoint or command
else
exit 0
fi
# START=false in the docker-compose
$ docker-compose up
Starting stk_alpine_1 ... done
Attaching to stk_alpine_1
stk_alpine_1 exited with code 0
$ sed -i 's/START=false/START=true/' docker-compose.yml
$ docker-compose up
Starting stk_alpine_1 ... done
Attaching to stk_alpine_1
alpine_1 | ok
stk_alpine_1 exited with code 0

Related

Run docker-compose command without exiting

I am using a postgres image and i need to start ssh service on start.
The problem is that if I run a command in docker-compose file the proccess exits with code 0.
How can I start ssh service but keep postgres serice active too?
DOCKER FILE:
FROM postgres:13
RUN apt update && apt install openssh-server sudo -y
RUN echo 'root:password' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
DOCKER-COMPOSE FILE:
postgres:
container_name: db_postgres
command: sh -c "service ssh start "
image: postgresc
build:
context: ../backend_apollo_server_express
dockerfile: Dockerfile.database
environment:
- "POSTGRES_USER=lims"
- "POSTGRES_PASSWORD=lims"
volumes:
- /home/javier/lims/dockerVolumes/db:/var/lib/postgresql/data
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
ports:
- 5434:5432
You can try to use run postgres after you command
command: sh -c "service ssh start & postgres"
Try
command: sh -c "nohup service ssh start && service postgres start &"
In order to leave the process running in the background. This way the process won't exit

docker-compose not sending signals to trap

I have a simple docker-compose.yml
version: '3.8'
services:
port-forwarder:
image: test
container_name: port-forwarder
volumes:
- "/path_to_scripts/port_forwarder.sh:/port_forwarder.sh"
command: "/port_forwarder.sh"
port_forwarder.sh
#!/usr/bin/env bash
trap 'echo exiting ; exit 0' SIGTERM TERM SIGINT INT EXIT WINCH SIGWINCH
tail -f /dev/null
Dockerfile
FROM alpine:3.12.0
RUN apk add --no-cache bash
If I build the image and run it using
docker build -t test .
docker run --name test -it --rm -v /path_to_scripts/port_forwarder.sh:/port_forwarder.sh test /port_forwarder.sh
and then hit ctrl-c, it catches the SIGTERM and writes "exiting" on the console. But when I run it using 'docker-compose up' and hit ctrl-c it hangs and is killed after 10 seconds. I am using docker on OSX. What could be the reason and how can I catch the termination signal from docker-compose?

Can't see environment variables set in docker-compose from sbt

I have a dockerfile:
FROM mozilla/sbt:8u212_1.3.4
WORKDIR /app
ADD . /app
RUN sbt compile
CMD sbt run
I have a docker-compose file:
version: '3'
services:
my-service:
build: .
environment:
- KEY=VALUE
My scala project looks like this:
object Main extends App {
println(System.getenv("KEY")
}
but when I run docker-compose up it just prints null, instead of VALUE
First, Check if the variable it's in the container.
Run de container and enter in it:
$ docker exec -it <IDcontainer> /bin/bash
# echo $KEY
Maybe the problem is in the program and not in the container.
Bye

Executing wait-for-it.sh in python Docker container

I have a Python docker container that needs to wait until another container (postgres server) finishes setup. I tried the standard wait-for-it.sh but several commands weren't included. I tried a basic sleep (again in an sh file) but now it's reporting exec: 300: not found when trying to finally execute the command I'm waiting on.
How do I get around this (preferably without changing the image, or having to extend an image.)
I know I could also just run a Python script, but ideally I'd like to use wait-for-it.sh to wait for the server to turn up rather than just sleep.
Dockerfile (for stuffer):
FROM python:2.7.13
ADD ./stuff/bin /usr/local/bin/
ADD ./stuff /usr/local/stuff
WORKDIR /usr/local/bin
COPY requirements.txt /opt/updater/requirements.txt
COPY internal_requirements.txt /opt/stuff/internal_requirements.txt
RUN pip install -r /opt/stuff/requirements.txt
RUN pip install -r /opt/stuff/other_requirements.txt
docker-compose.yml:
version: '3'
services:
local_db:
build: ./local_db
ports:
- "localhost:5432:5432"
stuffer:
build: ./
depends_on:
- local_db
command: ["./wait-for-postgres.sh", "-t", "300", "localhost:5432", "--", "python", "./stuffing.py", "--file", "./afile"]
Script I want to use (but can't because no psql or exec):
#!/bin/bash
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$#"
until psql -h "$host" -U "postgres" -c '\l'; do >&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
Sergey's comment. I had wrong argument order. This issue had nothing to do with docker and everything to do with my inability to read.
I made an example so you can see it working:
https://github.com/nitzap/wait-for-postgres
On the other hand also you can have errors inside the execution of the script to validate that the service is working. You should not refer as localhost .... because that is within the contexts of containers, if you want to point to another container has to be through the name of the service.

Docker Compose apparently ignores COMPOSE_FILE

I have two docker-compose config yamls that look like this:
docker-compose.yml :
version: '2'
services:
web: &web
build: .
environment:
FOO: bar
docker-compose.development.override.yml :
version: '2'
services:
web: &web
build: .
environment:
FOO: biz
When I take a look at the value of $FOO inside the container, I am not seeing the value I expect:
bdares$ COMPOSE_FILE=./docker-compose.yml:./docker-compose.development.override.yml
bdares$ docker-compose run --rm web bash
docker$ echo $FOO
bar
When I explicitly set the compose files, I see the value I expect:
bdares$ docker-compose -f ./docker-compose.yml \
> -f ./docker-compose.development.override.yml \
> run --rm web bash
docker$ echo $FOO
biz
This suggests to me that docker-compose is not respecting the COMPOSE_FILE environment variable as claimed here.
What might I be doing wrong?
Version info:
docker-compose version 1.8.0
Docker version 1.11.0
Use:
export COMPOSE_FILE=./docker-compose.yml:./docker-compose.development.override.yml
docker-compose run --rm web bash
or
COMPOSE_FILE=./docker-compose.yml:./docker-compose.development.override.yml docker-compose run --rm web bash