Wimateeka
Wimateeka

Reputation: 2696

Docker-Compose - Randomized Network Name

I have a project that has 3 containers and will be run by our Jenkins build system. I've made it so that the 3 containers can communicate with each other, and now I need to figure out if simultaneous builds are happening how do I use some kinda of UUID or randomly generated network name with docker-compose? I need this so that they containers don't talk to other similarly named containers from a different build accidentally.

Is there a way to create a randomly generated name in the docker-compose file?

Upvotes: 2

Views: 2240

Answers (1)

thaJeztah
thaJeztah

Reputation: 29047

Assuming your CI does a checkout of your source from git, you could use the git-commit to use as a unique identifier for your containers/images/networks; e.g.

# get the sha of the commit that you're building from
export GIT_COMMIT=$(git rev-parse --short HEAD)

# build your image
docker build -t myimage:git-${GIT_COMMIT} .

# create your network
docker network create nw_${GIT_COMMIT}

# start your container
docker run -d --network=nw_${GIT_COMMIT} --name=foo_${GIT_COMMIT} myimage:git-${GIT_COMMIT}

With docker compose

Set the project-name based on the current git-commit; setting the COMPOSE_PROJECT_NAME environment variable will override the default project name (which is based on the name of the current directory). Also setting a GIT_COMMIT environment-variable so that it can be used separately.

export COMPOSE_PROJECT_NAME=myproject_$(git rev-parse --short HEAD)
export GIT_COMMIT=$(git rev-parse --short HEAD)

Create a docker-compose.yml and Dockerfile

version: "3.7"
services:
  web:
    build:
      context: .
      args:
          GIT_COMMIT:
    image: myproject/web:${GIT_COMMIT:-unknown}

Dockerfile:

FROM nginx:alpine
ARG GIT_COMMIT=unknown
RUN echo "This is build ${GIT_COMMIT}" > /usr/share/nginx/html/index.html

Cleanup everything before running (other instances of the same project; older versions of the image, volumes);

docker-compose down --rmi=all --volumes --remove-orphans

Stopping myproject_a9f48b5_web_1 ... done
Removing myproject_a9f48b5_web_1 ... done
Removing network myproject_a9f48b5_default
Removing image myproject/web:a9f48b5

Build the image(s) for your service

docker-compose build

Building web
Step 1/3 : FROM nginx:alpine
 ---> 315798907716
Step 2/3 : ARG GIT_COMMIT=unknown
 ---> Running in 78515fcdd331
Removing intermediate container 78515fcdd331
 ---> bb2414522a62
Step 3/3 : RUN echo "This is build ${GIT_COMMIT}" > /usr/share/nginx/html/index.html
 ---> Running in 9bf1f2023915
Removing intermediate container 9bf1f2023915
 ---> 3debb1a96b63

Successfully built 3debb1a96b63
Successfully tagged myproject/web:a9f48b5

And start your stack;

docker-compose up -d
Creating network "myproject_a9f48b5_default" with the default driver
Creating myproject_a9f48b5_web_1 ... done

Find the random port that was assigned to the web service;

docker-compose port web 80
0.0.0.0:32770

And connect to it:

curl localhost:32770
This is build a9f48b5

Upvotes: 4

Related Questions