I'm running a Docker compose file main services with three dependent, sub-services as follows:
docker-compose run -p hlp-api --service-ports web
This spins everything up nicely. However, while the sub-services end up with the name I intended, the main service has a name based on the project token (hlp-api
) I fed in plus an ugly auto-generated appendage.
Why is the main 'web' service not ending up with the name I specify in its container_name
param?
docker-compose.yml:
version: '2'
services:
# web service
web:
container_name: hlp-api-web
build:
context: .
dockerfile: Dockerfile
command: /bin/sh -c "gem install bundler && bundle install && rails server --port 3000 --binding 0.0.0.0"
ports:
- 3000:3000
- 5432:5432
- 2222:2222
volumes:
- ./webapp:/opt/webapp
- ../:/app:cached
- bundle:/usr/local/bundle
- rails_cache:/app/tmp/cache
- node_modules:/app/node_modules
depends_on:
- postgres
- ngrok
# database
postgres:
container_name: hlp-api-db
image: postgres:14.2
volumes:
- .psqlrc:/root/.psqlrc:ro
- dbdat:/var/lib/postgresql/data
- ./log:/root/log:cached
- ./latest.dump:/latest.dump
environment:
POSTGRES_DB: hlp-api-dev
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
DB_PORT: 5432
ports:
- 5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s
# ngrok
ngrok:
container_name: hlp-api-ngrok
image: ngrok/ngrok:latest
environment:
- NGROK_AUTHTOKEN
command: http host.docker.internal:3000
volumes:
web:
bundle:
dbdat:
node_modules:
rails_cache:
CodePudding user response:
I would guess, for the same reason you have to explicitly use --service-ports
, they decided that they didn't want name collisions as they didn't wanted to have ports collisions.
If the name would be respected from the docker compose run
command, that would means that you would always have to specify a name when you want to run the command, if the container is already running, as it will keep on trying to assign the same name over and over again.
The good thing is that you can provide a name, as you can ask it to respect the published ports:
docker-compose run -p hlp-api --service-ports --name hlp-api-web web
Related:
- https://github.com/docker/compose/issues/2061, and more specifically: this comment
CodePudding user response:
docker-compose run
is intended to spin up a one-off container to run a temporary command. It will start the services the named container depends_on:
. It doesn't actually launch the specific container described in the docker-compose.yml
, but a related container with a different name and a different command.
If you want to actually start the containers listed in the docker-compose.yml
file, use docker-compose up
.
A typical use of docker-compose run
would be to invoke database migrations, for example:
# Start the entire system
docker-compose up -d
# Start a new temporary container, with the same image and setup
# as the normal Rails container, to run migrations
docker-compose run web \
bundle exec rails db:migrate