Pass parameters and execute scripts in docker-compose - docker

I would like to make a service like this :
datastax:
image: luketillman/datastax-enterprise:5.1.0
ports:
- "9042:9042"
volumes:
- /datasets:/tmp/scripts
command: [ "-s",
"bash -c \"sleep 40 &&
cqlsh -f /tmp/scripts/init.cql\""]
Starts Cassandra in search mode (-s) and then (when it's up), execute init.cql via cqlsh.
Is it possible to do with compose ? How to proceed ?

You can run a command having multiple sub-commands like this:
datastax:
image: luketillman/datastax-enterprise:5.1.0
ports:
- "9042:9042"
volumes:
- /datasets:/tmp/scripts
command: bash -c "dse cassandra -s; sleep 40; cqlsh -f /tmp/scripts/init.cql"
Note that you must use the full dse cassandra -s command, you can't reuse the default command from the images AFAIK.

Related

When scaling, run some docker container startup commands only once

I have the following docker-compose.yml.
version: "3.1"
services:
db:
container_name: ${MYSQL_CONTAINER}
image: mysql:5.7.30
volumes:
- ${VOLUMES_DIR}/mysql_data:/var/lib/mysql
- ./slow_log.cnf:/etc/mysql/my.cnf
- ${VOLUMES_DIR}/mysql_logs:/var/log/mysql
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
ports:
- ${MYSQL_PORT}:3306
entrypoint: ""
command: bash -c "chown -R mysql:mysql /var/log/mysql && exec /entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
restart: on-failure
backend:
container_name: ${BACKEND_CONTAINER}
image: ${BACKEND_IMAGE}
depends_on:
- db
ports:
- ${BACKEND_PORT}
command: >
bash -c "command A
&& command B
&& ... "
restart: unless-stopped
I am scaling backend service so my startup command is sudo docker-compose -p ${COMPOSE_PROJECT_NAME} up -d --scale backend=10.
The problem I am facing is command A, command B in service backend was running for all 10 containers startup(means they were being run 10 times).
But I want command A to run only once for all the backend service-related containers but Command B should run for all containers.
Any suggestions in accomplishing this?
I'm not entirely sure that there would be an out-of-the-box solution for your requirement.
However, I can suggest you a workaround like this. You can duplicate your backend service in docker-compose and run one backend service with both Command A and Command B, while the other backend has only Command B.
Then when you want to scale, you scale the backend which has only Command B.
version: "3.1"
services:
db:
container_name: ${MYSQL_CONTAINER}
image: mysql:5.7.30
volumes:
- ${VOLUMES_DIR}/mysql_data:/var/lib/mysql
- ./slow_log.cnf:/etc/mysql/my.cnf
- ${VOLUMES_DIR}/mysql_logs:/var/log/mysql
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
ports:
- ${MYSQL_PORT}:3306
entrypoint: ""
command: bash -c "chown -R mysql:mysql /var/log/mysql && exec /entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
restart: on-failure
backend_default:
container_name: ${BACKEND_CONTAINER}
image: ${BACKEND_IMAGE}
depends_on:
- db
ports:
- ${BACKEND_PORT}
command: >
bash -c "command A
&& command B
&& ... "
restart: unless-stopped
backend:
container_name: ${BACKEND_CONTAINER}
image: ${BACKEND_IMAGE}
depends_on:
- db
ports:
- ${BACKEND_PORT}
command: >
bash -c "command B
&& ... "
restart: unless-stopped
Now you can use the scale option like below:
sudo docker-compose -p ${COMPOSE_PROJECT_NAME} up -d --scale backend=9
Now if there happens to be a scenario, where you need only 1 backend to be run, you can use profiles in docker-compose to only run backend when there is a specific profile is given with docker-compose command. That means only default_backend will run if that profile is not given and hence the scale is 1.
Hope this helps you. Cheers 🍻 !!!
If BACKEND_IMAGE is being built by you, you should do RUN command A in your Dockerfile. The RUN line will be executed only once during build time — so you will also need to make sure that this meshes with your needs — while the ENTRYPOINT and CMD lines will only be run upon execution of the container. The command in the docker-compose file overrides the CMD line.

Creating default user for influxdb 2.0 in docker-compose

In previous versions of influxdb we could create an admin user by using the environment variables
influxdb:
...
environment:
INFLUXDB_HOSTNAME: "${INFLUXDB_HOSTNAME}"
INFLUXDB_USERNAME: "${INFLUXDB_USERNAME}"
INFLUXDB_PASSWORD: "${INFLUXDB_PASSWORD}"
...
But in version 2.0 these env. variables are removed
What I want to achieve is, when I run the docker-compose up my_influx_db
It should create a default admin user, if it doesn't exist
What I tried so far is:
my_influx_db:
image: quay.io/influxdb/influxdb:v2.0.3
hostname: my_influx_db
container_name: my_influx_db
ports:
- 8086:8086
command: /bin/sh -c "exec influxd && sleep 10 && influx setup -o test_org -b test_bucket -u user1 -p testpassword -f"
docker-compose up my_influx_db starts the influxdb, but doesn't run the setup script after 10 secs
What is the right way to create default admin user?
Edit:
I managed to run it with the following configuration, but is it the right way to do it?
command: /bin/sh -c "(sleep 10 && echo setting up user && influx setup -o test_org -b test-bucket -u influxdb -p influxdb -f) & influxd"
The latest version: 2.0.7 of influxdb made it easier providing the ability to create initial admin username and password
https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials
You can up another container which will try to create a user.
So it's my docker-compose.yml:
version: '3'
services:
influxdb:
image: quay.io/influxdb/influxdb:v2.0.4
container_name: influxdb
volumes:
- ./influxdbv2:/root/.influxdbv2
ports:
- "8086:8086"
influxdb_cli:
links:
- influxdb
image: quay.io/influxdb/influxdb:v2.0.4
entrypoint: influx setup --bucket test_bucket -t test_token -o test_org --username=test_username --password=test_password --host=http://influxdb:8086 -f
restart: on-failure:20
depends_on:
- influxdb
volumes:
influxdbv2:

Merge two docker run commands together

I have two docker run commands as given below but I would like to merge these two commands together and execute it.
1st command - Start orthanc just with web viewer enabled
docker run -p 8042:8042 -e WVB_ENABLED=true osimis/orthanc
2nd command - Start Orthanc with mount directory tasks
docker run -p 4242:4242 -p 8042:8042 --rm --name orthanc -v
$(pwd)/orthanc/orthanc.json:/etc/orthanc/orthanc.json -v
$(pwd)/orthanc/orthanc-db:/var/lib/orthanc/db jodogne/orthanc-plugins
/etc/orthanc --verbose
As you can see, in both the cases the Orthanc is being started but I would like to merge these into one and start Orthanc. When it is started Web viewer should also be enabled and mount directory should also have happened
Can you let me know on how can this be done?
Use docker-compose, it is specially targeted for running multiple containers.
docker-compose.yml
version: '3'
services:
osimis:
image: osimis/orthanc
environment:
WVB_ENABLED: 'true'
ports:
- 8042:8042
orthanc:
image: jodogne/orthanc-plugins
environment:
WVB_ENABLED: 'true'
ports:
- 4242:4242
- 8042:8042
volumes:
- ./orthanc/orthanc.json:/etc/orthanc/orthanc.json
- ./orthanc/orthanc-db:/var/lib/orthanc/db
command: /etc/orthanc --verbose
and docker-compose up to finish the work

How do I convert this docker command to docker-compose?

I run this command manually:
$ docker run -it --rm \
--network app-tier \
bitnami/cassandra:latest cqlsh --username cassandra --password cassandra cassandra-server
But I don't know how to convert it to a docker compose file, specially the container's custom properties such as --username and --password.
What should I write in a docker-compose.yaml file to obtain the same result?
Thanks
Here is a sample of how others have done it. http://abiasforaction.net/apache-cassandra-cluster-docker/
Running the command below
command:
Setting arg's below
environment:
Remember just because you can doesn't mean you should.. Compose is not always the best way to launch something. Often it can be the lazy way.
If your running this as a service id suggest building the dockerfile to start and then creating systemd/init scripts to rm/relaunch it.
an example cassandra docker-compose.yml might be
version: '2'
services:
cassandra:
image: 'bitnami/cassandra:latest'
ports:
- '7000:7000'
- '7001:7001'
- '9042:9042'
- '9160:9160'
volumes:
- 'cassandra_data:/bitnami'
volumes:
cassandra_data:
driver: local
although this will not provide you with your commandline arguments but start it with the default CMD or ENTRYPOINT.
As you are actually running another command then the default you might not want to do this with docker-compose. Or you can create a new Docker image with this command as the default and provide the username and password as ENV's
e.g. something like this (untested)
FROM bitnami/cassandra:latest
ENV USER=cassandra
ENV PASSWORD=password
CMD ["cqlsh", "--username", "$USER", "--password", "$PASSWORD", "cassandra-server"]
and you can build it
docker build -t mycassandra .
and run it with something like:
docker run -it -e "USER=foo" -e "PASSWORD=bar" mycassandra
or in docker-compose
services:
cassandra:
image: 'mycassandra'
ports:
- '7000:7000'
- '7001:7001'
- '9042:9042'
- '9160:9160'
environment:
USER:user
PASSWORD:pass
volumes:
- 'cassandra_data:/bitnami'
volumes:
cassandra_data:
driver: local
You might looking for something like the following. Not sure if it is going to help you....
version: '3'
services:
my_app:
image: bitnami/cassandra:latest
command: /bin/sh -c cqlsh --username cassandra --password cassandra cassandra-server
ports:
- "8080:8080"
networks:
- app-tier
networks:
app-tier:
external: true

Populate cassandra after Docker-compose finishes

Here's my docker-compose.yaml:
version: '3'
services:
cassandra:
container_name: cassandra
image: cassandra:3.10
ports:
- 9142:9042
volumes:
- $PWD/src/database/migrations:/migrations
depends_on:
- mysql
mysql:
image: mysql:5.7
volumes:
- ./src/database/migrations:/docker-entrypoint-initdb.d
ports:
- 3307:3306
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=leonardo
- MYSQL_ALLOW_EMPTY_PASSWORD=1
And a shell script bootstrap.sh to start:
#!/bin/bash
set -e
docker-compose -f $PWD/docker-compose.yaml down
docker-compose -f $PWD/docker-compose.yaml up -d
This is working and fires up the stack.
Now I'd like to add a keyspace to Cassandra, however, below this line throws an error:
docker-compose -f $PWD/docker-compose.yaml exec cassandra /bin/sh -c $'cqlsh cassandra -e "CREATE KEYSPACE mykeyspace WITH replication = {\'class\': \'SimpleStrategy\', \'replication_factor\': \'1\'} AND durable_writes = true;"'
Connection error: ('Unable to connect to any servers', {'172.18.0.2':
error(111, "Tried connecting to [('172.18.0.2', 9042)]. Last error:
Connection refused")})
Cassandra has probably not initialised yet.
What's a good approach for this?
If you try to execute a command in Cassandra in your bootstrap.sh script just after starting the containers, the Cassandra server won't have the time to start.
A simple solution would be to wait a few seconds in your script before executing the command, using an until loop:
#!/bin/bash
set -e
docker-compose -f $PWD/docker-compose.yaml down
docker-compose -f $PWD/docker-compose.yaml up -d
until docker-compose -f $PWD/docker-compose.yaml exec cassandra /bin/sh -c "cqlsh cassandra -e 'show version'";
do
docker-compose -f $PWD/docker-compose.yaml exec cassandra /bin/sh -c $'cqlsh cassandra -e "CREATE KEYSPACE mykeyspace WITH replication = {\'class\': \'SimpleStrategy\', \'replication_factor\': \'1\'} AND durable_writes = true;"'
done

Resources