I'm using the certbot/certbot container as in:
docker-compose run -d --rm --entrypoint 'certbot certonly --webroot -w /var/www/certbot --staging --email example#domain.se -d example.com --rsa-key-size 4096 --agree-tos --force-renewal ; sleep 3600' certbot
on the following compose file:
version: '3.5'
services:
nginx:
image: nginx:1.15-alpine
restart: unless-stopped
volumes:
- "~/dev/docker/projects/common/volumes/letsencrypt/nginx:/etc/nginx/conf.d"
- "~/dev/docker/projects/common/volumes/letsencrypt/certbot/conf:/etc/letsencrypt"
- "~/dev/docker/projects/common/volumes/letsencrypt/certbot/www:/var/www/certbot"
- "~/dev/docker/projects/common/volumes/letsencrypt/nginx:/var/www/nginx"
- "~/dev/docker/projects/common/volumes/logs:/var/log/nginx"
ports:
- "80:80"
- "443:443"
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- "~/dev/docker/projects/common/volumes/letsencrypt/certbot/conf:/etc/letsencrypt"
- "~/dev/docker/projects/common/volumes/letsencrypt/certbot/www:/var/www/certbot"
- "~/dev/docker/projects/common/volumes/logs:/var/log/letsencrypt"
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
But it ignores the sleep command and the container goes away.
Whereas running the following:
docker-compose run -d --rm --entrypoint 'sleep 3600' certbot
keeps the container up and running.
I would like to keep the container up and running after the certbot failed.
You could move "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" into dedicated script for example start.sh.
Mount it with docker-compose volumes
volumes:
- "./start.sh:/start.sh
entrypoint: /start.sh
Related
Just sharing how I did to create automatically queues when RabbitMq starts:
version: "3.2"
services:
rabbitmq:
image: rabbitmq:3-management-alpine
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
volumes:
- ./queues/rabbitmq/init.sh:/init.sh
restart: "no"
entrypoint: [ "bash", "-c", "sleep 3 && ./init.sh"]
networks:
abinbev_net:
ipv4_address: 173.101.101.101
networks:
abinbev_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 173.101.101.0/24
And my init.sh
#!/bin/bash
echo "Generating a new queue through commandline"
#!/bin/sh
# Create Rabbitmq user
( sleep 5 ; \
rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD 2>/dev/null ; \
rabbitmqctl set_user_tags $RABBITMQ_USER administrator ; \
rabbitmqctl set_permissions -p / $RABBITMQ_USER ".*" ".*" ".*" ; \
rabbitmqadmin -u guest -p guest -V / declare queue name=otc-finance-reference-receive-queue ; \
echo "*** User '$RABBITMQ_USER' with password '$RABBITMQ_PASSWORD' completed. ***" ; \
echo "*** Log in the WebUI at port 15672 (example: http:/localhost:15672) ***") &
# $# is used to pass arguments to the rabbitmq-server command.
# For example if you use it like this: docker run -d rabbitmq arg1 arg2,
# it will be as you run in the container rabbitmq-server arg1 arg2
rabbitmq-server $#
So it's working fine but I'd like to know if you have another shape to make that.
Thanks, Raphael
I have setup kong docker container. it's starting with docker compose file:
kong:
image: "${KONG_DOCKER_TAG}"
user: ${KONG_USER}
depends_on:
- kong-database
- kong-migrations
environment:
KONG_ADMIN_ACCESS_LOG: /dev/stdout
KONG_ADMIN_ERROR_LOG: /dev/stderr
KONG_ADMIN_LISTEN: '0.0.0.0:8001'
KONG_CASSANDRA_CONTACT_POINTS: ${KONG_CASSANDRA_CONTACT_POINTS}
KONG_DATABASE: ${KONG_DATABASE}
KONG_PG_DATABASE: ${KONG_PG_DATABASE}
KONG_PG_HOST: ${KONG_PG_HOST}
KONG_PROXY_LISTEN: '0.0.0.0:8000'
KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443
KONG_NGINX_HTTP_INCLUDE: custom-nginx-kong.conf
KONG_PG_USER: ${KONG_PG_USER}
KONG_PROXY_ACCESS_LOG: /dev/stdout
KONG_PROXY_ERROR_LOG: /dev/stderr
KONG_PG_PASSWORD: ${KONG_PG_PASSWORD}
networks:
- external-network
configs:
- source: kong-config
target: /usr/local/kong/custom-nginx-kong.conf
# Permissions -r--r--r--
mode: 0444
healthcheck:
test: ["CMD", "curl", "-f", "http://kong:8001"]
interval: 5s
timeout: 2s
retries: 15
restart: on-failure
deploy:
restart_policy:
condition: on-failure
labels:
com.docker.lb.hosts: ${APP_URL_KONG}
com.docker.lb.port: 8080
com.docker.lb.network: external-network
com.docker.lb.backend_mode: vip
I want to execute curl commands eg. to add new service in kong like below post command immediately after my container is created automatically either through some script or using the below command. but How can I setup this automation through above docker-compose file? please help me to add services automatically on startup though docker compose!!!
curl -i -X POST http://<admin-hostname>:8001/services \
--data name=example_service \
--data url='http://mockbin.org'
Thanks in advance!
Our travis builds have started failing and I can't figure out why. Our app runs in docker-compose and then we run cypress to against it. This used to work perfectly. Now the host port for the web server is just unresponsive. I've removed cypress and am just trying to run curl http://localhost:3001 and it just hangs. Here's the travis.yml. Any suggestions would be highly appreciated. I have tried fiddling for several hours with the docker versions, distros, localhost vs 127.0.0.1, etc to no avail. All of this works fine locally on my workstation.
language: node_js
node_js:
- "12.19.0"
env:
- DOCKER_COMPOSE_VERSION=1.25.4
services:
- docker
sudo: required
# Supposedly this is needed for Cypress to work in Ubuntu 16
# https://github.com/cypress-io/cypress-example-kitchensink/blob/master/basic/.travis.yml
addons:
apt:
packages:
- libgconf-2-4
before_install:
# upgrade docker compose https://docs.travis-ci.com/user/docker/#using-docker-compose
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
# upgrade docker itself https://docs.travis-ci.com/user/docker/#installing-a-newer-docker-version
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- sudo apt-get update
- sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
# Put the .env file in place
- cp .env.template .env
install:
# Install node modules (for jest and wait-on) and start up the docker containers
- cd next
- npm ci
- cd ..
- cd e2e
- npm ci
- cd ..
script:
- docker --version
- docker-compose --version
- docker-compose up --build -d
# Run unit tests
# - cd next
# - npm run test
# Run e2e tests
# - cd ../e2e
# - npx cypress verify
# - CYPRESS_FAIL_FAST=true npx wait-on http://localhost:3001 --timeout 100000 && npx cypress run --config video=false,pageLoadTimeout=100000,screenshotOnRunFailure=false
- sleep 30
- curl http://127.0.0.1:3001 --max-time 30
- docker-compose logs db
- docker-compose logs express
- docker-compose logs next
post_script:
- docker-compose down
The logs look like this:
The command "docker-compose up --build -d" exited with 0.
30.01s$ sleep 30
The command "sleep 30" exited with 0.
93.02s$ curl http://127.0.0.1:3001 --max-time 30
curl: (28) Operation timed out after 30001 milliseconds with 0 bytes received
The command "curl http://127.0.0.1:3001 --max-time 30" exited with 28.
The docker compose logs show nothing suspicious. It's as if the network wasn't set up correctly and docker is not aware of any requests.
Here is the docker-compose.yml in case it's useful:
version: '3.7'
services:
db:
image: mg-postgres
build: ./postgres
ports:
- '5433:5432'
environment:
POSTGRES_HOST_AUTH_METHOD: 'trust'
adminer:
image: adminer
depends_on:
- db
ports:
- '8080:8080'
express:
image: mg-server
build: ./express
restart: always
depends_on:
- db
env_file:
- .env
environment:
DEBUG: express:*
volumes:
- type: bind
source: ./express
target: /app
- /app/node_modules
ports:
- '3000:3000'
next:
image: mg-next
build: ./next
depends_on:
- db
- express
env_file:
- .env
volumes:
- type: bind
source: ./next
target: /app
- /app/node_modules
ports:
- '3001:3001'
command: ['npm', 'run', 'dev']
I've in Dockerfile service which depends on another service, but I'd like to negate the condition when not service_healthy. So opposite of the following:
service1:
depends_on:
service2:
condition: service_healthy
So basically I'd like to start service1 when service2 is not healthy.
Secondly, based on the documentation for depends_on, the condition option has been removed and it is no longer supported in version 3 of Compose file format.
So how the above logic can be achieved?
Here is the workaround where main container waits for other hosts to exit, by pinging the other hosts and waiting when both are off-line:
version: '3'
services:
main:
image: bash
depends_on:
- test01
- test02
command: bash -c "sleep 2 && until ! ping -qc1 test01 && ! ping -qc1 test02; do sleep 1; done &>/dev/null"
networks:
intra:
ipv4_address: 172.10.0.254
test01:
image: bash
hostname: test01
command: bash -c "ip route && sleep 10"
networks:
intra:
ipv4_address: 172.10.0.11
test02:
image: bash
hostname: test02
command: bash -c "ip route && sleep 20"
networks:
intra:
ipv4_address: 172.10.0.12
networks:
intra:
driver: bridge
ipam:
config:
- subnet: 172.10.0.0/24
See also: Docker compose - Start service only when other service had completed
I've added
command: bash -c './wait-for-it.sh -t 4 -s php:9000 -- bash run-ssh-on-php.sh'
to my docker-compose.yml
php:
build: docker/php
user: "$LOCAL_USER_ID:$LOCAL_GROUP_ID"
depends_on:
- mysql
- rabbitmq
- mail
- phantomjs
- data
volumes_from:
- data
ports:
- "9000:9000"
environment:
- SYMFONY_ENV
command: bash -c './wait-for-it.sh -t 4 -s php:9000 -- bash run-ssh-on-php.sh'
and it seems that it wasn't executed at all, how can I check if it was? I tried adding "touch somefile" but nothing was created