micahhausler / container-transform

Transforms docker-compose, ECS, and Marathon configurations
MIT License
1.41k stars 148 forks source link

Container names are not unique #61

Open andreaskoch opened 8 years ago

andreaskoch commented 8 years ago

It would be great if you could add a project-name parameter to container-transform.

Currently the container names created by container-transform don't include the project name of the docker-compose file. This leads to collisions with other project using the same container name (e.g. "nginx").

I believe that usually one would not try to make the service names (or container names) globally unique like "projectxynginx" inside a docker-compose.yml.

Example

Run two similar projects on the same server: Both projects have a frontend named web and a backend named phpfpm:

Project A

version: '2'

services:
  web:
    image: nginx:1-alpine
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d:ro
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    volumes_from:
      - phpfpm:ro
    networks:
      - front
      - back
    labels:
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:wambo.co,www.wambo.co"
      - "traefik.protocol=http"
      - "traefik.frontend.entryPoints=http,https"
      - "traefik.frontend.passHostHeader=true"

  phpfpm:
    image: php:7-fpm-alpine
    volumes:
      - ./web:/var/www/html
    networks:**phpfpm**
      - back

networks:
  front:
    driver: bridge
  back:
    driver: bridge

Project B

version: '2'

services:
  web:
    image: nginx:1-alpine
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d:ro
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    volumes_from:
      - phpfpm:ro
    networks:
      - front
      - back
    labels:
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:beta.wambo.co"
      - "traefik.protocol=http"
      - "traefik.frontend.entryPoints=http,https"
      - "traefik.frontend.passHostHeader=true"

  phpfpm:
    image: php:7-fpm-alpine
    volumes:
      - ./web:/var/www/html
    networks:
      - back

networks:
  front:
    driver: bridge
  back:
    driver: bridge

If I now create systemd services for both projects using container-tranform I will get collisions because the docker containers of both projects have the same names:

Project A

# web.service #######################################################################
[Unit]
Description=Web
After=docker.service 
Requires=docker.service 

[Service]
ExecStartPre=-/usr/bin/docker kill web
ExecStartPre=-/usr/bin/docker rm web
ExecStartPre=/usr/bin/docker pull nginx:1-alpine
ExecStart=/usr/bin/docker run \
    --name web \
    -p 9080:80 \
    --net ['front', 'back'] \
    -v ./config/nginx/conf.d:/etc/nginx/conf.d:ro \
    -v ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
    --label traefik.frontend.entryPoints="http,https" \
    --label traefik.frontend.passHostHeader="true" \
    --label traefik.frontend.rule="Host:wambo.co,www.wambo.co" \
    --label traefik.port="80" \
    --label traefik.protocol="http" \
    --volumes-from phpfpm:ro \
    nginx:1-alpine 
ExecStop=/usr/bin/docker stop web
# phpfpm.service #######################################################################
[Unit]
Description=Phpfpm
After=docker.service 
Requires=docker.service 

[Service]
ExecStartPre=-/usr/bin/docker kill phpfpm
ExecStartPre=-/usr/bin/docker rm phpfpm
ExecStartPre=/usr/bin/docker pull php:7-fpm-alpine
ExecStart=/usr/bin/docker run \
    --name phpfpm \
    --net ['back'] \
    -v ./web:/var/www/html \
    php:7-fpm-alpine 
ExecStop=/usr/bin/docker stop phpfpm

Project B

# server.service #######################################################################
[Unit]
Description=Server**phpfpm**
After=docker.service 
Requires=docker.service 

[Service]
ExecStartPre=-/usr/bin/docker kill server
ExecStartPre=-/usr/bin/docker rm server
ExecStartPre=/usr/bin/docker pull nginx:1-alpine
ExecStart=/usr/bin/docker run \
    --name server \
    --net ['front', 'back'] \
    -v ./config/nginx/conf.d:/etc/nginx/conf.d:ro \
    -v ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
    --label traefik.frontend.entryPoints="http,https" \
    --label traefik.frontend.passHostHeader="true" \
    --label traefik.frontend.rule="Host:beta.wambo.co" \
    --label traefik.port="80" \
    --label traefik.protocol="http" \
    --volumes-from phpfpm:ro \
    nginx:1-alpine 
ExecStop=/usr/bin/docker stop server
# phpfpm.service #######################################################################
[Unit]
Description=Phpfpm
After=docker.service 
Requires=docker.service 

[Service]
ExecStartPre=-/usr/bin/docker kill phpfpm
ExecStartPre=-/usr/bin/docker rm phpfpm
ExecStartPre=/usr/bin/docker pull php:7-fpm-alpine
ExecStart=/usr/bin/docker run \
    --name phpfpm \
    --net ['back'] \
    -v ./web:/var/www/html \
    php:7-fpm-alpine 
ExecStop=/usr/bin/docker stop phpfpm

... I always wanted to learn Python. Maybe this is the time for me so I can create a pull request for your project :smile:

eedwardsdisco commented 7 years ago

@andreaskoch what would the project name end up contributing to the final output? would it simply be prepending to the service name? can you provide an example?

If you were running both projects locally (using docker-compose, pre-transform), would it still conflict? (e.g. is this an actual problem with the transform project, or are the namespaces conflicting regardless and you just want a utility for automatically changing the names during transform?)