Docker shared container within multiple docker compose projects
I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: ...
container_name: app-${ENV}
depends_on:
- db
expose:
- 80
db:
image: ...
container_name: my-cool-db
ports:
- "3306:3306"
The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:
ENV=$1 docker-compose -p $1 up -d
So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.
If I have a single container this works great, and I end up with say:
app-first
app-second
app-third
What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.
Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?
Can this be made without bringing up 2 separate DB containers?
docker docker-compose dockerfile
add a comment |
I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: ...
container_name: app-${ENV}
depends_on:
- db
expose:
- 80
db:
image: ...
container_name: my-cool-db
ports:
- "3306:3306"
The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:
ENV=$1 docker-compose -p $1 up -d
So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.
If I have a single container this works great, and I end up with say:
app-first
app-second
app-third
What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.
Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?
Can this be made without bringing up 2 separate DB containers?
docker docker-compose dockerfile
add a comment |
I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: ...
container_name: app-${ENV}
depends_on:
- db
expose:
- 80
db:
image: ...
container_name: my-cool-db
ports:
- "3306:3306"
The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:
ENV=$1 docker-compose -p $1 up -d
So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.
If I have a single container this works great, and I end up with say:
app-first
app-second
app-third
What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.
Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?
Can this be made without bringing up 2 separate DB containers?
docker docker-compose dockerfile
I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: ...
container_name: app-${ENV}
depends_on:
- db
expose:
- 80
db:
image: ...
container_name: my-cool-db
ports:
- "3306:3306"
The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:
ENV=$1 docker-compose -p $1 up -d
So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.
If I have a single container this works great, and I end up with say:
app-first
app-second
app-third
What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.
Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?
Can this be made without bringing up 2 separate DB containers?
docker docker-compose dockerfile
docker docker-compose dockerfile
edited Nov 16 '18 at 15:31
Siyu
3,20411231
3,20411231
asked Nov 16 '18 at 11:16
NorgulNorgul
1,64011845
1,64011845
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A hacky solution:
change your compose file to
services:
app:
image: ...
container_name: app-${ENV}
networks:
- shared
expose:
- 80
db:
image: ...
container_name: my-cool-db
networks:
- shared
ports:
- "3306:3306"
networks:
shared:
external: true
Then first create the network docker network create shared
Bring up db: docker-compose up -d db
First app: ENV=first docker-compose -p first up -d app
Second app: ENV=second docker-compose -p second up -d app
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336787%2fdocker-shared-container-within-multiple-docker-compose-projects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A hacky solution:
change your compose file to
services:
app:
image: ...
container_name: app-${ENV}
networks:
- shared
expose:
- 80
db:
image: ...
container_name: my-cool-db
networks:
- shared
ports:
- "3306:3306"
networks:
shared:
external: true
Then first create the network docker network create shared
Bring up db: docker-compose up -d db
First app: ENV=first docker-compose -p first up -d app
Second app: ENV=second docker-compose -p second up -d app
add a comment |
A hacky solution:
change your compose file to
services:
app:
image: ...
container_name: app-${ENV}
networks:
- shared
expose:
- 80
db:
image: ...
container_name: my-cool-db
networks:
- shared
ports:
- "3306:3306"
networks:
shared:
external: true
Then first create the network docker network create shared
Bring up db: docker-compose up -d db
First app: ENV=first docker-compose -p first up -d app
Second app: ENV=second docker-compose -p second up -d app
add a comment |
A hacky solution:
change your compose file to
services:
app:
image: ...
container_name: app-${ENV}
networks:
- shared
expose:
- 80
db:
image: ...
container_name: my-cool-db
networks:
- shared
ports:
- "3306:3306"
networks:
shared:
external: true
Then first create the network docker network create shared
Bring up db: docker-compose up -d db
First app: ENV=first docker-compose -p first up -d app
Second app: ENV=second docker-compose -p second up -d app
A hacky solution:
change your compose file to
services:
app:
image: ...
container_name: app-${ENV}
networks:
- shared
expose:
- 80
db:
image: ...
container_name: my-cool-db
networks:
- shared
ports:
- "3306:3306"
networks:
shared:
external: true
Then first create the network docker network create shared
Bring up db: docker-compose up -d db
First app: ENV=first docker-compose -p first up -d app
Second app: ENV=second docker-compose -p second up -d app
edited Nov 16 '18 at 13:36
answered Nov 16 '18 at 13:27
SiyuSiyu
3,20411231
3,20411231
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336787%2fdocker-shared-container-within-multiple-docker-compose-projects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown