nginx gunicorn with docker: how to use multiple dockerfiles
I'm using the following example to build a django-postgres-nginx-gunicorn web server. I'd like to have separated folders for each container. The following structure of the project works correctly. The main Dockerfile is used for the hello app (the django project):
svm3_03
|____Dockerfile
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| |__Dockerfile
| |__dataForDB
|__docker-compose.yml
|__hello
the docker-compose.yml looks like this:
version: '3'
...
services:
djangoapp:
build:
context: .
...
and the Dockerfile has the following line:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
Everything works. Now I tried to have the same structure as the DB image also for the django app so I move the main Dockerfile and the hello directory inside the docker folder... I created a new folder inside the docker folder with name djangoapp and I moved there the Dockerfile and the hello folder. The new structure is the following:
svm3_03
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| | |__Dockerfile
| | |__dataForDB
| |__djangoapp
| |__Dockerfile
| |__hello
|__docker-compose.yml
All I did then was to change the context line in the docker-compose.yml file like this:
version: '3'
...
services:
djangoapp:
build:
context: ./docker/djangoapp
...
Now I get the following error:
djangoapp_1_7b3c77a4b939 | Error: can't chdir to 'hello'...
which is due to gunicorn and the command line CMD inside the djangoapp Dockerfile. Clearly changing the context in docker-compose.yml is not enough... any idea what am I missing?
Thanks
UPDATE:
Here is the full dockerfile:
FROM python:2
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/djangoapp/src
WORKDIR /opt/services/djangoapp/src
COPY . /opt/services/djangoapp/src
RUN pip install --no-cache-dir -r hello/requirements.txt
RUN cd hello && python manage.py collectstatic --no-input
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
django docker nginx docker-compose gunicorn
add a comment |
I'm using the following example to build a django-postgres-nginx-gunicorn web server. I'd like to have separated folders for each container. The following structure of the project works correctly. The main Dockerfile is used for the hello app (the django project):
svm3_03
|____Dockerfile
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| |__Dockerfile
| |__dataForDB
|__docker-compose.yml
|__hello
the docker-compose.yml looks like this:
version: '3'
...
services:
djangoapp:
build:
context: .
...
and the Dockerfile has the following line:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
Everything works. Now I tried to have the same structure as the DB image also for the django app so I move the main Dockerfile and the hello directory inside the docker folder... I created a new folder inside the docker folder with name djangoapp and I moved there the Dockerfile and the hello folder. The new structure is the following:
svm3_03
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| | |__Dockerfile
| | |__dataForDB
| |__djangoapp
| |__Dockerfile
| |__hello
|__docker-compose.yml
All I did then was to change the context line in the docker-compose.yml file like this:
version: '3'
...
services:
djangoapp:
build:
context: ./docker/djangoapp
...
Now I get the following error:
djangoapp_1_7b3c77a4b939 | Error: can't chdir to 'hello'...
which is due to gunicorn and the command line CMD inside the djangoapp Dockerfile. Clearly changing the context in docker-compose.yml is not enough... any idea what am I missing?
Thanks
UPDATE:
Here is the full dockerfile:
FROM python:2
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/djangoapp/src
WORKDIR /opt/services/djangoapp/src
COPY . /opt/services/djangoapp/src
RUN pip install --no-cache-dir -r hello/requirements.txt
RUN cd hello && python manage.py collectstatic --no-input
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
django docker nginx docker-compose gunicorn
better to post your dockerfile here
– Siyu
Nov 22 '18 at 12:33
add a comment |
I'm using the following example to build a django-postgres-nginx-gunicorn web server. I'd like to have separated folders for each container. The following structure of the project works correctly. The main Dockerfile is used for the hello app (the django project):
svm3_03
|____Dockerfile
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| |__Dockerfile
| |__dataForDB
|__docker-compose.yml
|__hello
the docker-compose.yml looks like this:
version: '3'
...
services:
djangoapp:
build:
context: .
...
and the Dockerfile has the following line:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
Everything works. Now I tried to have the same structure as the DB image also for the django app so I move the main Dockerfile and the hello directory inside the docker folder... I created a new folder inside the docker folder with name djangoapp and I moved there the Dockerfile and the hello folder. The new structure is the following:
svm3_03
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| | |__Dockerfile
| | |__dataForDB
| |__djangoapp
| |__Dockerfile
| |__hello
|__docker-compose.yml
All I did then was to change the context line in the docker-compose.yml file like this:
version: '3'
...
services:
djangoapp:
build:
context: ./docker/djangoapp
...
Now I get the following error:
djangoapp_1_7b3c77a4b939 | Error: can't chdir to 'hello'...
which is due to gunicorn and the command line CMD inside the djangoapp Dockerfile. Clearly changing the context in docker-compose.yml is not enough... any idea what am I missing?
Thanks
UPDATE:
Here is the full dockerfile:
FROM python:2
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/djangoapp/src
WORKDIR /opt/services/djangoapp/src
COPY . /opt/services/djangoapp/src
RUN pip install --no-cache-dir -r hello/requirements.txt
RUN cd hello && python manage.py collectstatic --no-input
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
django docker nginx docker-compose gunicorn
I'm using the following example to build a django-postgres-nginx-gunicorn web server. I'd like to have separated folders for each container. The following structure of the project works correctly. The main Dockerfile is used for the hello app (the django project):
svm3_03
|____Dockerfile
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| |__Dockerfile
| |__dataForDB
|__docker-compose.yml
|__hello
the docker-compose.yml looks like this:
version: '3'
...
services:
djangoapp:
build:
context: .
...
and the Dockerfile has the following line:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
Everything works. Now I tried to have the same structure as the DB image also for the django app so I move the main Dockerfile and the hello directory inside the docker folder... I created a new folder inside the docker folder with name djangoapp and I moved there the Dockerfile and the hello folder. The new structure is the following:
svm3_03
|____config
| |____gunicorn
| | |____conf.py
| |____nginx
| | |____conf.d
| | |____local.conf
| |____db
| |____db_env
|____docker
| |__db
| | |__Dockerfile
| | |__dataForDB
| |__djangoapp
| |__Dockerfile
| |__hello
|__docker-compose.yml
All I did then was to change the context line in the docker-compose.yml file like this:
version: '3'
...
services:
djangoapp:
build:
context: ./docker/djangoapp
...
Now I get the following error:
djangoapp_1_7b3c77a4b939 | Error: can't chdir to 'hello'...
which is due to gunicorn and the command line CMD inside the djangoapp Dockerfile. Clearly changing the context in docker-compose.yml is not enough... any idea what am I missing?
Thanks
UPDATE:
Here is the full dockerfile:
FROM python:2
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/djangoapp/src
WORKDIR /opt/services/djangoapp/src
COPY . /opt/services/djangoapp/src
RUN pip install --no-cache-dir -r hello/requirements.txt
RUN cd hello && python manage.py collectstatic --no-input
EXPOSE 8000
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "hello", "hello.wsgi:application"]
django docker nginx docker-compose gunicorn
django docker nginx docker-compose gunicorn
edited Nov 22 '18 at 12:43
Attilio
asked Nov 22 '18 at 11:11
AttilioAttilio
356
356
better to post your dockerfile here
– Siyu
Nov 22 '18 at 12:33
add a comment |
better to post your dockerfile here
– Siyu
Nov 22 '18 at 12:33
better to post your dockerfile here
– Siyu
Nov 22 '18 at 12:33
better to post your dockerfile here
– Siyu
Nov 22 '18 at 12:33
add a comment |
2 Answers
2
active
oldest
votes
I think the problem is not with /hello
but with config/
, it doesn't appear that it has been copied inside the container. To verify, exec -it
into the container and ls
.
You need to pass that folder inside, one solution is to use volume:
services:
djangoapp:
volume:
- ./config:/opt/services/djangoapp/src
Another solution is to copy it in the image, you will need to change your build context or docker will complain about copying file from outside the context
services:
djangoapp:
build:
context: .
dockerfile: ./docker/djangoapp/Dockerfile
and change paths in your Dockerfile to be relative to the new context
COPY ./docker/djangoapp /opt/services/djangoapp/src
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
are you sure yourconfig/
andhello/
are at the same level? the latter is insrc/
– Siyu
Nov 22 '18 at 22:59
add a comment |
Finally I was able to track the problem. My error was in the Dockerfile of the django app. The command to run gunicorn has to point to the right directory. Here is the correct command:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "docker/djangoapp/hello", "hello.wsgi:application"]
Note the argument of the --chdir option.
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%2f53429676%2fnginx-gunicorn-with-docker-how-to-use-multiple-dockerfiles%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the problem is not with /hello
but with config/
, it doesn't appear that it has been copied inside the container. To verify, exec -it
into the container and ls
.
You need to pass that folder inside, one solution is to use volume:
services:
djangoapp:
volume:
- ./config:/opt/services/djangoapp/src
Another solution is to copy it in the image, you will need to change your build context or docker will complain about copying file from outside the context
services:
djangoapp:
build:
context: .
dockerfile: ./docker/djangoapp/Dockerfile
and change paths in your Dockerfile to be relative to the new context
COPY ./docker/djangoapp /opt/services/djangoapp/src
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
are you sure yourconfig/
andhello/
are at the same level? the latter is insrc/
– Siyu
Nov 22 '18 at 22:59
add a comment |
I think the problem is not with /hello
but with config/
, it doesn't appear that it has been copied inside the container. To verify, exec -it
into the container and ls
.
You need to pass that folder inside, one solution is to use volume:
services:
djangoapp:
volume:
- ./config:/opt/services/djangoapp/src
Another solution is to copy it in the image, you will need to change your build context or docker will complain about copying file from outside the context
services:
djangoapp:
build:
context: .
dockerfile: ./docker/djangoapp/Dockerfile
and change paths in your Dockerfile to be relative to the new context
COPY ./docker/djangoapp /opt/services/djangoapp/src
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
are you sure yourconfig/
andhello/
are at the same level? the latter is insrc/
– Siyu
Nov 22 '18 at 22:59
add a comment |
I think the problem is not with /hello
but with config/
, it doesn't appear that it has been copied inside the container. To verify, exec -it
into the container and ls
.
You need to pass that folder inside, one solution is to use volume:
services:
djangoapp:
volume:
- ./config:/opt/services/djangoapp/src
Another solution is to copy it in the image, you will need to change your build context or docker will complain about copying file from outside the context
services:
djangoapp:
build:
context: .
dockerfile: ./docker/djangoapp/Dockerfile
and change paths in your Dockerfile to be relative to the new context
COPY ./docker/djangoapp /opt/services/djangoapp/src
I think the problem is not with /hello
but with config/
, it doesn't appear that it has been copied inside the container. To verify, exec -it
into the container and ls
.
You need to pass that folder inside, one solution is to use volume:
services:
djangoapp:
volume:
- ./config:/opt/services/djangoapp/src
Another solution is to copy it in the image, you will need to change your build context or docker will complain about copying file from outside the context
services:
djangoapp:
build:
context: .
dockerfile: ./docker/djangoapp/Dockerfile
and change paths in your Dockerfile to be relative to the new context
COPY ./docker/djangoapp /opt/services/djangoapp/src
answered Nov 22 '18 at 15:29
SiyuSiyu
2,98911227
2,98911227
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
are you sure yourconfig/
andhello/
are at the same level? the latter is insrc/
– Siyu
Nov 22 '18 at 22:59
add a comment |
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
are you sure yourconfig/
andhello/
are at the same level? the latter is insrc/
– Siyu
Nov 22 '18 at 22:59
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
Thanks for the comment... however it looks like it doesn't solve the problem. I tried both your suggestions but the problem is still there. I also copied the full config folder within docker/djangoapp and with "docker run -it svm3_03_djangoapp /bin/bash" I checker its existence in the container... in fact it was there but the problem persisted...
– Attilio
Nov 22 '18 at 20:44
are you sure your
config/
and hello/
are at the same level? the latter is in src/
– Siyu
Nov 22 '18 at 22:59
are you sure your
config/
and hello/
are at the same level? the latter is in src/
– Siyu
Nov 22 '18 at 22:59
add a comment |
Finally I was able to track the problem. My error was in the Dockerfile of the django app. The command to run gunicorn has to point to the right directory. Here is the correct command:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "docker/djangoapp/hello", "hello.wsgi:application"]
Note the argument of the --chdir option.
add a comment |
Finally I was able to track the problem. My error was in the Dockerfile of the django app. The command to run gunicorn has to point to the right directory. Here is the correct command:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "docker/djangoapp/hello", "hello.wsgi:application"]
Note the argument of the --chdir option.
add a comment |
Finally I was able to track the problem. My error was in the Dockerfile of the django app. The command to run gunicorn has to point to the right directory. Here is the correct command:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "docker/djangoapp/hello", "hello.wsgi:application"]
Note the argument of the --chdir option.
Finally I was able to track the problem. My error was in the Dockerfile of the django app. The command to run gunicorn has to point to the right directory. Here is the correct command:
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "docker/djangoapp/hello", "hello.wsgi:application"]
Note the argument of the --chdir option.
answered Nov 27 '18 at 21:43
AttilioAttilio
356
356
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%2f53429676%2fnginx-gunicorn-with-docker-how-to-use-multiple-dockerfiles%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
better to post your dockerfile here
– Siyu
Nov 22 '18 at 12:33