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
Related
I want to access the db directly through command prompt.
I run the command:
docker exec -it container_name -u user_name -p
Instead of a line asking me the user password I get the following error message:
OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: \"-u\": executable file not found in $PATH": unknown
I've tried several other commands of the same type:
docker exec -it container_name -u -p
docker exec -it container_name -u user_name
docker exec -it container_name -u root
I don't use a Dockerfile but the image directly :
version: '3'
services:
frontend:
build: ./frontend
volumes:
- ./frontend/:/app
ports:
- ${PORT_FRONTEND}:${PORT_FRONTEND}
container_name: dev_frontend
database:
image: mariadb
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- ALLOW_EMPTY_PASSWORD=${ALLOW_EMPTY_PASSWORD}
ports:
- "${PORT_MARIADB}:${PORT_MARIADB}"
volumes:
- ./volumes/database/:/var/lib/mysql:rw
container_name: dev_mariadb
I use a .env file to store sensible data instead of placing them in the docker-compose.yml
If anyone has any idea on how to proceed to find the issue I'd be grateful.
Thanks!
Simply correct the command above with :
docker exec -it container_name mysql -u{username} -p{userpassword}
I want to create isolated environment to run modified python interpreter in it.
I have docker-compose.yaml:
version: '3.5'
services:
cpython:
image: ubuntu
ports:
- "7777:7777"
volumes:
- /home/myuser/workspace/cpython:/docker
working_dir: /docker
# stdin_open: true
# tty: true
command: /docker/python /docker/Tools/scripts/pydoc3 -p 7777
But when starting docker-compose up i get:
myuser#myuser-laptop:~/workspace/cpython$ docker-compose up
Recreating cpython_cpython_1 ...
Recreating cpython_cpython_1 ... done
Attaching to cpython_cpython_1
cpython_1 | Server ready at http://localhost:7777/
cpython_1 | Server commands: [b]rowser, [q]uit
cpython_1 | server>
cpython_1 | Server stopped
cpython_cpython_1 exited with code 0
Why command /docker/python /docker/Tools/scripts/pydoc3 -p 7777 is being stopped? In my host terminal it's working fine.
Looks like it's not opening an interactive terminal. Try:
docker run -it -p 7777:7777 -v /home/myuser/workspace/cpython:/docker ubuntu /docker/python /docker/Tools/scripts/pydoc3 -p 7777
I am facing an issue with connect local redis with docker container, this is my docker compose file.
version: "3"
services:
test:
build: .
stdin_open: true
tty: true
command: nodemon --delay 6 index.js
volumes:
- .:/opt/test
ports:
- "5007:5007"
links:
- redis
redis:
image: redis:latest
container_name: qbo_redis
restart: always
ports:
- "6379:6379"
But it is not working.
Getting error
bash
TI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"3ed84a23c52d\": executable file not found in $PATH": unknown
If i remove the redis entries from docker-compose.yml file, its working without any error.
docker ps -q returns a list of containers and cannot be used to access the shell. Use docker ps -ql to work with the last container created.
redis images don't contain Bash. They're based on Alpine Linux, but can you use /bin/sh. Try this way:
docker exec -it qbo_redis /bin/sh
or
docker exec -it $(docker ps -ql) /bin/sh
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.
I'm running several containers in daemon mode: docker-compose up -d.
One of them recently crashed.
I'd like to investigate what happened. Where can I find the app logs?
Here's the docker-compose.yml (nothing special regarding logging):
mongodb:
image: mongo
command: "--smallfiles --logpath=/dev/null"
web:
build: .
command: npm start
volumes:
- .:/myapp
ports:
- "3001:3000"
links:
- mongodb
environment:
PORT: 3000
NODE_ENV: 'production'
seed:
build: ./seed
links:
- mongodb
You can get logs via docker-compose logs or you could exec to attach (docker >= 1.3) to the running instance via
$ docker exec -i -t 6655b41beef /bin/bash #by ID
or
$ docker exec -i -t my_www /bin/bash #by Name