0

Two weeks ago I created a docker-compose.yml file to start two services, but this week when I try to start those services Docker appends a "-1" to the service name. I am using Docker Desktop on a Windows 10 machine. Here is my yml file:

services:
  pgdatabase:
    image: postgres:13
    environment:
      - POSTGRES_USER=####
      - POSTGRES_PASSWORD=####
      - POSTGRES_DB=ny_taxi
    volumes:
      - "./ny_taxi_postgres_data:/var/lib/postgresql/data:rw"
    ports:
      - "5432:5432"
  pgadmin:
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=####@####.com
      - PGADMIN_DEFAULT_PASSWORD=####
    ports:
      - "8080:80"

This worked perfectly when I created it, but now when I run docker-compose up the containers that get created are pgadmin-1 and pgdatabase-1.

If I then run docker-compose down, and do a docker ps the output shows that no containers are running. However, if I run docker-compose config --services I get the following:

pgadmin
pgdatabase

Restarting Docker does nothing, and the issue occurs even if I delete all containers and all volumes from Docker Desktop.

docker-compose start returns service "pgadmin" has no container to start. If I run docker-compose up and then docker-compose start pgadmin I get no output from the command line. However, listing the active containers after doing this still only shows pgadmin-1. Running docker-compose down after these steps does not resolve the issue.

docker rm -f pgadmin returns Error: No such container: pgadmin.

docker service rm pgadmin returns Error: No such service: pgadmin.

docker-compose up -d --force-recreate --renew-anon-volumes just creates pgadmin-1 and pgdatabase-1 again.

PaxUltra
  • 3
  • 1
  • Does this answer your question? [docker-compose: remove default suffix on container name?](https://stackoverflow.com/questions/64976083/docker-compose-remove-default-suffix-on-container-name) – HF CHIU Feb 12 '23 at 14:37
  • Unfortunately, no. – PaxUltra Feb 14 '23 at 05:24
  • Can you set the container name explicitly? e.g., `container_name: pgdatabase` – HF CHIU Feb 15 '23 at 00:57
  • Using `container_name:` in the yml file does circumvent the naming problem, but I still have those two services when I run `docker-compose config --services`. – PaxUltra Feb 16 '23 at 23:14

1 Answers1

0

The container and service names are different things in Docker Compose terminologies.

Given your docker-compose.yml file:

services:
  pgdatabase:
    # ...
  pgadmin:
    # ...

The command docker-compose config --services lists the service names, BUT NOT the container names. A service name is a key you use in your docker-compose.yml file immediately under the key services:, e.g., pgdatabase: and pgadmin:.

Therefore, the output of docker-compose config --services:

pgadmin
pgdatabase

is expected behavior, whether any containers are running. Since the command docker-compose config only parses your compose file, you can get the same output even when Docker itself is not running.

The default container name scheme of a container created by docker-compose is

<project>_<service>_<index>_<slug>

Assuming that your compose file is located in a directory postgresql, i.e.,

postgresql/docker-compose.yml

Then the default container names created by docker-compose are:

  • postgresql_pgdatabase_1
  • postgresql_pgadmin_1

or in Docker Compose V2, they are

  • postgresql-pddatabase-1
  • postgresql-pgadmin-1

Of course you can override the default naming convention using the container_name: key in your Compose file.

HF CHIU
  • 678
  • 6
  • 8