pass argument to docker compose service - docker

I have docker compose defied with 2 service,
I need 1st to start with --dev command line option,
but I cannot find this in file format spec. https://docs.docker.com/compose/compose-file/compose-file-v3/
version: "3.9"
services:
polkadot:
image: parity/polkadot:latest
command: --dev
ports:
- "9944:9944"
sidecar:
image: parity/substrate-api-sidecar:latest
ports:
- "8080:8080"
running with docker-compose up
For comparison, when simply running docker adding --dev is straightforward:
docker run --rm -it -p 9944:9944 parity/polkadot:latest --dev
But how to within docker-compose file?

command: is the right way to go
It is possible to pass many arguments as:
version: "3.9"
services:
polkadot:
container_name: polkadotdev
image: parity/polkadot:latest
ports:
#- 30333:30333 # p2p port
- 9933:9933 # rpc port
- 9944:9944 # ws port
command: [
"--dev",
"--name", "polkadotdevnode",
"--ws-external",
"--rpc-external",
"--rpc-cors", "all"
]
sidecar:
container_name: sidecardev
image: parity/substrate-api-sidecar:latest
ports:
- "8080:8080"
environment:
SAS_SUBSTRATE_WS_URL: ws://polkadot:9944

Related

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

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

Make Traefik see containers from other networks?

I'm trying to make Traefik notice a container that belongs to a different network. Consider the following docker-compose.yml, which is the only file in that directory:
version: '3.7'
services:
traefik:
image: "traefik:v2.1"
container_name: "traefik"
hostname: "traefik"
ports:
- "80:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
command:
- '--providers.docker.defaultRule=Host(`{{ index .Labels "com.docker.compose.service" }}.docker.localhost`)'
- '--providers.docker.exposedbydefault=false'
- '--entrypoints.web.address=:80'
And the following project, sitting in a directory flask:
flask/docker-compose.yml
version: '3.7'
services:
flaskapp:
container_name: flaskapp
build: flask_app
expose:
- 5000
labels:
traefik.enable: true
traefik.docker.network: traefik_default
traefik.http.routers.flaskapp.rule: Host(`flaskapp.localhost`)
traefik.http.routers.flaskapp.entrypoints: web
flask/flask_app/Dockerfile
FROM python:3.8
RUN python3.8 -m pip install flask
ADD ./main.py .
EXPOSE 5000
ENTRYPOINT ./main.py
flask/flask_app/main.py
#!/usr/bin/env python3.8
import flask
app = flask.Flask(__name__)
#app.route('/')
def main():
return "hello, world"
app.run(host='0.0.0.0')
I basically did sudo docker-compose up in both of the directories and found that flaskapp.localhost receives the connection, but then times out. So I tried sudo docker network connect traefik_default flaskapp to connect flaskapp to the traefik_default network, but this didn't seem change anything. Why doesn't sudo docker network connect traefik_default flaskapp help? Is there a way to make Traefik see containers from all networks without plugging it in to theirs?
It appears that one way to make Traefik able to access all docker-compose networks is to run it in --net host mode, by adding network_mode: "host" to its flags. Here's the modified docker-compose.yml:
version: '3.7'
services:
traefik:
image: "traefik:v2.1"
container_name: "traefik"
hostname: "traefik"
network_mode: "host"
ports:
- "80:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
command:
- '--providers.docker.defaultRule=Host(`{{ index .Labels "com.docker.compose.service" }}.docker.localhost`)'
- '--providers.docker.exposedbydefault=false'
- '--entrypoints.web.address=:80'
I don't unfortunately understand the security implications of this setup and thus can't tell if it's fit for production usage, but it seems to solve this particular problem.

How to pass port mapping dynamically to the docker-compose up command?

I have two container images and doing compose and running the docker using "docker-compose up -d".
This works fine. I want to run the same container image in another port say 8081.
Can we pass port mapping as a command line parameter docker-compose up -port novnc :8081:8080?
How to pass port mapping dynamically to the docker-compose up command?
version: '2'
services:
ide:
image: myApp
image: myImage:latest
environment:
- DISPLAY=novnc:0.0
depends_on:
- novnc
networks:
- x11
novnc:
image: myImageTwo:latest
environment:
- DISPLAY_WIDTH=1600
- DISPLAY_HEIGHT=968
ports:
- "8080:8080"
networks:
- x11
networks:
x11:
use a ${VAR} in your docker-compose.yml
e.g.
version: '2'
services:
apache:
image: httpd:2.4
volumes:
- .:/usr/local/apache2/htdocs/
ports:
- ${APP_PORT}:80
then use environment variable:
$ export APP_PORT=8080
$ docker-compose up
or inline version:
$ APP_PORT=8080 docker-compose up
You can do it using the .env file.
For example, you'll have something like this :
$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
In the example, you can see that the tag value is in a variable that is set in the env file.
You can find out more in the official doc
Change your ports section to:
ports:
- "${MY_PORT}:8080"
and then just use MY_PORT=8081 docker-compose up -d
you can also use a port range
ports:
- "8080-8081:8080"
so you don't have to pass as parameter

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 setup PyCharm for Docker inside Vagrant?

I've a project that's running Docker in Vagrant.
The python interpreter is inside the Docker containter.
How do I set up PyCharm so that it can use this interpreter.
The Dockerfile is:
FROM python:3.5.1-onbuild
The docker-compose.yaml is the following:
web:
restart: always
build: .
ports:
- "80:80"
expose:
- "80"
links:
- postgres:postgres
volumes:
- .:/usr/src/app/
env_file: .env
command: /usr/local/bin/gunicorn --reload -w 2 -b :80 hello:app
data:
image: postgres:latest
volumes:
- /var/lib/postgresql
command: "true"
postgres:
restart: always
image: postgres:latest
volumes_from:
- data
ports:
- "5432:5432"
I already tried with the standard options but it seems that you only can choose either docker or vagrant:
docker or vagrant
Thanks
Docker-compose support is added to the last version of PyCharm. Here's how to solve the issue, using the latest version of PyCharm.
Configure PyCharm interpreter with docker-compose inside Vagrant

Resources