Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random container names when building from the same docker-compose file

I have the following docker-compose.yml file:

version: '2'
services:
    php-apache:
        image: reynierpm/php55-dev
        ports:
            - "80:80"
        environment:
            PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
        volumes:
            - ~/data:/data

Each time I run the following command docker-compose up -d it creates a container with the same name all the time as for example php55devwork_php-apache_1 which is the folder where the file is plus the service name. Ex:

[rperez@dev php55-dev-work] $ tree
.
├── composer.json
├── docker-compose.yml
├── Dockerfile

Is there any way to change this name randomly? Now I want to test a different application in the same container but another instance and the only solution I have is to copy the content in another folder.

What would be the best solution?

like image 508
ReynierPM Avatar asked Oct 26 '16 17:10

ReynierPM


People also ask

Where do docker container names come from?

However, if no name is provided by the user while creating/running a Docker container, Docker automatically assigns the container a name. If you look at them carefully, you could see a pattern emerge. The first name is an adjective while the second name is the surname of a notable scientist or hacker.

Does Docker compose create multiple containers?

The docker-compose. yml file allows you to configure and document all your application's service dependencies (other services, cache, databases, queues, etc.). Using the docker-compose CLI command, you can create and start one or more containers for each dependency with a single command (docker-compose up).

Can two docker containers have the same name?

Note: Container names must be unique. That means you can only call one container web . If you want to re-use a container name you must delete the old container (with docker container rm ) before you can create a new container with the same name. As an alternative you can use the --rm flag with the docker run command.

Can two containers have same name?

A container with the same name is still existing. Containers can exist in following states, during which the container name can't be used for another container: created.


1 Answers

Since there is a container_name: that you can add to your docker-compose.yml (since PR 1711), one possible workaround would be to:

  • use your current docker-compose.yml as a template: copy it and add a random container name in a new container_name: xxx directive.
  • run your docker compose up on that temporary docker-compose.yml.
like image 138
VonC Avatar answered May 16 '23 09:05

VonC