How can I translate this run docker command into docker-compose? - docker

Trying to setup Redis from this image Redismod and struggle to translate the following code into docker-compose
$ docker run \
-p 6379:6379 \
-v /home/user/data:/data \
-v /home/user/redis.conf:/usr/local/etc/redis/redis.conf \
redislabs/redismod \
/usr/local/etc/redis/redis.conf
What I have done till now:
version: "3.2"
services:
redis:
image: "redislabs/redismod"
container_name: 'redis-local'
hostname: 'redis-local'
volumes_from:
- redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
args:
- /usr/local/etc/redis/redis.conf
restart: always
ports:
- "6379:6379"
volumes:
redis_data:
But I get the following error ERROR: Service "redis" mounts volumes from "redis_data", which is not the name of a service or container. obviously because I didn't pass the last line /usr/local/etc/redis/redis.conf
And second question, how do I translate --loadmodule and --dir from below, these aren't Redis command:
$ docker run \
-p 6379:6379 \
-v /home/user/data:/data \
redislabs/redismod \
--loadmodule /usr/lib/redis/modules/rebloom.so \
--dir /data
UPDATE
I changed my docker-compose.yml file to the following and it started to work, but it seems that Redis doesn't see the redis.conf file and continue to run in default mode, what I do wrong?
version: "3.2"
services:
redis:
image: "redislabs/redismod"
container_name: 'redis-local'
hostname: 'redis-local'
volumes:
- redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
build:
context: .
args:
- /usr/local/etc/redis/redis.conf
restart: always
ports:
- "6379:6379"

The first error was because you used volumes_from instead of volumes. The first one is intended to get the volumes configuration from an existing container. The second one to define the volumes. In your last version redis_data is a docker volume and redis.conf is a bind mount. Your second problem is that you are using build and args that are intended to be used for building images but looks like you wanted to run a command.
Try:
version: "3.2"
services:
redis:
image: "redislabs/redismod"
container_name: 'redis-local'
hostname: 'redis-local'
volumes:
- redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: usr/local/etc/redis/redis.conf
restart: always
ports:
- "6379:6379"
For more info about volumes, bind mounts and docker compose reference see:
https://docs.docker.com/storage/volumes/
https://docs.docker.com/storage/bind-mounts/
https://docs.docker.com/compose/compose-file/compose-file-v3/#command

Related

translate network option in docker run to docker-compose?

I would like to translate the following docker command to a docker-compose file:
docker run -d --restart=always -v /var/run/docker.sock:/var/run/docker.sock --net shinyproxy-net -p 8080:8080 imshinyproxy
This is the docker-compose.yml that I wrote:
version: "3.7"
services:
shinyproxy:
image: imshinyproxy
container_name: imshinyproxy
environment:
- PUID=1000
- PGID=65537
- TZ=america/new_york
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 8080:8080
networks:
- shinyproxy-net
restart: unless-stopped
Alas, when I try to run docker-compose up I get the following error:
$ docker-compose up
ERROR: Service "shinyproxy" uses an undefined network "shinyproxy-net"
I know the network exist:
$ sudo docker network create shinyproxy-net
Error response from daemon: network with name shinyproxy-net already exists
What am I doing wrong?
You must declare an external network in the networks section of your docker-compose.yml :
version: "3.7"
services:
shinyproxy:
[...]
networks:
- shinyproxy-net
networks:
shinyproxy-net:
external:
name: shinyproxy-net
networks.shinyproxy-net.external.name should correspond to the name of your previously created network.

Docker Compose Flags

I don't how to run the docker-compose equivalent of my code
docker run -d --name=server --restart=always --net network --ip 172.18.0.5 -p 5003:80 -v $APP_PHOTO_DIR:/app/mysql-data -v $APP_CONFIG_DIR:/app/config webserver
I've done this:
version: '3'
services:
server:
image: app-dependencies
ports:
- "5003:80"
volumes:
- ./app:/app
command: python /app/app.py
restart: always
networks:
app_net:
ipv4_address: 172.18.0.5
Are you sure you need an IP address for container? It is not recommended practice, why do you want to set it explicitly?
docker-compose.yml
version: '3'
services:
server: # correct, this would be container's name
image: webserver # this should be image name from your command line
ports:
- "5003:80" # correct, but only if you need to communicate to service from ouside
volumes: # volumes just repeat you command line, you can use Env vars
- $APP_PHOTO_DIR:/app/mysql-data
- $APP_CONFIG_DIR:/app/config
command: ["python", "/app/app.py"] # JSON notation strongly recommended
restart: always
Then docker-compose up -d and that's it. You can access your service from host with localhost:5003, no need for internal IP.
For networks, I always include in the docker-compose file, the network specification. If the network already exists, docker will not create a new one.
version: '3'
services:
server:
image: app-dependencies
ports:
- "5003:80"
volumes:
- ./app:/app
command: python /app/app.py
restart: always
networks:
app_net:
ipv4_address: 172.18.0.5
networks:
app_net:
name: NETWORK_NAME
driver: bridge
ipam:
config:
- subnet: NETWORK_SUBNET
volumes:
VOLUME_NAME:
driver:local
And you will need to add the volumes separately to match the docker run command.

How to persist data in a dockerized DynamoDB using volumes

My docker compose file has two containers and looks like this
version: '3'
services:
dynamodb:
image: amazon/dynamodb-local
ports:
- '8000:8000'
networks:
- testnetwork
audit-server:
image: audit-dynamo
environment:
DYNAMO_URL: 'http://0.0.0.0:8000'
command: node app.js
ports:
- '3000:3000'
depends_on:
- dynamodb
# restart: always
networks:
- testnetwork
networks:
testnetwork:
My goal is to mount local data to some volume. currently losing data on docker-compose down
So that image uses by default in-memory dynamodb (what you can find by running docker inspect on that image)
"CMD [\"-jar\" \"DynamoDBLocal.jar\" \"-inMemory\"]"
So if you want to keep your data, you need to do something like this in your docker-compose:
version: '3'
volumes:
dynamodb_data:
services:
dynamodb:
image: amazon/dynamodb-local
command: -jar DynamoDBLocal.jar -sharedDb -dbPath /home/dynamodblocal/data/
volumes:
- dynamodb_data:/home/dynamodblocal/data
ports:
- "8000:8000"
You can try this docker-compose config:
version: '3'
volumes:
dynamodb_data:
services:
dynamodb:
image: amazon/dynamodb-local
command: -jar DynamoDBLocal.jar -sharedDb -dbPath /home/dynamodblocal
volumes:
- dynamodb_data:/home/dynamodblocal
ports:
- "8000:8000"
To preserve data across docker installations create volume using docker.
docker volume create --driver local --opt type=none \
--opt device=/var/opt/dynamodb_data --opt o=bind dynamodb_data
use external option:
version: "3"
volumes:
dynamodb_data:
external: true
services:
dynamodb-local:
image: amazon/dynamodb-local
command: ["-jar", "DynamoDBLocal.jar", "-sharedDb", "-dbPath", "/home/dynamodblocal/data"]
volumes:
- dynamodb_data:/home/dynamodblocal/data

How to see other container from one container in docker?

I have compose file as follows;
redis:
image: redis
ports:
- "6379:6379"
php:
build: .
image: php:fpm
volumes:
- ./code:/var/www/html
links:
- redis:redis
networks:
- code-network
I'm entering into php container with the following command.
docker exec -it php_id /bin/bash
but I can't run "redis-cli" command in this container. What do I need to do to run it.
I added "links" parameter to compose file but it didn't.
You are putting the php-fpm container in a network of its own. Here is a fixed compose file:
version: "3"
services:
redis:
image: redis
ports:
- "6379:6379"
php:
build: .
image: php:fpm
volumes:
- ./code:/var/www/html
networks:
- code-network
- default
networks:
code-network:
See this for more info on compose networking.
About the redis-cli issue: You'd need to add the appropriate repository on the php-fpm container and then install it. As you are using the php:fpm image, you propably want to use redis with some php-application, therefore you don't need debians redis-cli package, but rather the php-extension.
See this post for more info.

Cannot launch django with celery in Docker Compose v3

Here is my docker-compose.yml:
version: '3.4'
services:
nginx:
restart: always
image: nginx:latest
ports:
- 80:80
volumes:
- ./misc/nginx.conf:/etc/nginx/conf.d/default.conf
- /static:/static
depends_on:
- web
web:
restart: always
image: celery-with-docker-compose:latest
build: .
command: bash -c "python /code/manage.py collectstatic --noinput && python /code/manage.py migrate && /code/run_gunicorn.sh"
volumes:
- /static:/data/web/static
- /media:/data/web/media
- .:/code
env_file:
- ./.env
depends_on:
- db
volumes:
- ./app:/deploy/app
worker:
image: celery-with-docker-compose:latest
restart: always
build:
context: .
command: bash -c "pip install -r /code/requirements.txt && /code/run_celery.sh"
volumes:
- .:/code
env_file:
- ./.env
depends_on:
- redis
- web
db:
restart: always
image: postgres
env_file:
- ./.env
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
redis:
restart: always
image: redis:latest
privileged: true
command: bash -c "sysctl vm.overcommit_memory=1 && redis-server"
ports:
- "6379:6379"
volumes:
pgdata:
When I run docker stack deploy -c docker-compose.yml cryptex I got
Non-string key at top level: true
And docker-compose -f docker-compose.yml config gives me
ERROR: In file './docker-compose.yml', the service name True must be a quoted string, i.e. 'True'.
I'm using latest versions of docker and compose. Also I'm new to compose v3 and started to use it for getting availability of docker stack command. If you see any mistakes or redudants in config file please, let me know. Thanks
you have to validate you docker compose file, is probably that the have low value inside
Validating your file now is as simple as docker-compose -f
docker-compose.yml config. As always, you can omit the -f
docker-compose.yml part when running this in the same folder as the
file itself or having the

Resources