Reputation: 13908
Here's my compose file:
version: '2'
services:
web:
build: ./web
ports:
- "3000:3000"
links:
- api
api:
build: ./api
links:
- db
db:
image: postgres
Here is my Dockerfile for the web
service:
FROM node:8.9.3
WORKDIR /app
COPY ./package.json /app
COPY ./server /app/server
COPY ./client /app/client
EXPOSE 3000
ENV PORT 3000
RUN ["npm", "install"]
ENTRYPOINT ["npm", "start"]
And for the api
project:
FROM python:3.6
WORKDIR /api
COPY ./requirements.txt /api/requirements.txt
COPY ./src /api/src
EXPOSE 80
ENV PORT 80
RUN ["pip", "install", "-r", "requirements.txt"]
RUN ["python", "src/main.py"]
When I run docker-compose up
, only the api
service starts. However, if I comment out the api
service altogether and run docker-compose up
again, both the web
and db
services start. Is there something wrong with how I've set up the api
project? What am I doing wrong?
Just for completeness, here is the starting file for the web
project (uses Express)
http.createServer(app).listen(app.get("port"), function() {
console.log("Express server listening on port " + app.get("port"));
});
and the api
proj (uses Flask):
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)
UPDATE
I commented out the main line of my flask server and all 3 services are able to start. The downside is... now i dont have an api server. What is it about Flask that makes the other services unusable?
UPDATE 2
Some have requested to see the logs.
Here are the logs if i start without flask:
Building web
Step 1/10 : FROM node:8.9.3
---> 2eeae8debf3d
Step 2/10 : WORKDIR /app
---> Using cache
---> 02199a27dafb
Step 3/10 : COPY ./package.json /app
---> Using cache
---> fafac64ad492
Step 4/10 : COPY ./app.js /app
---> Using cache
---> 0b1e3067451b
Step 5/10 : COPY ./controllers /app/server
---> Using cache
---> ff00fa864078
Step 6/10 : COPY ./client /app/client
---> Using cache
---> 587214c84267
Step 7/10 : EXPOSE 3000
---> Using cache
---> ddd5b795fcf5
Step 8/10 : ENV PORT 3000
---> Using cache
---> ca4c37f63468
Step 9/10 : RUN npm install
---> Using cache
---> 74b289885447
Step 10/10 : ENTRYPOINT npm start
---> Using cache
---> 9c5ec6770c47
Successfully built 9c5ec6770c47
Successfully tagged testcomposemachine_web:latest
Recreating testcomposemachine_web_1
Starting testcomposemachine_db_1
Attaching to testcomposemachine_db_1, testcomposemachine_web_1
db_1 | 2017-12-28 23:11:55.861 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2017-12-28 23:11:55.861 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2017-12-28 23:11:55.874 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2017-12-28 23:11:55.907 UTC [24] LOG: database system was shut down at 2017-12-28 22:59:27 UTC
db_1 | 2017-12-28 23:11:55.919 UTC [1] LOG: database system is ready to accept connections
web_1 |
web_1 | > [email protected] start /app
web_1 | > node app.js
web_1 |
web_1 | Express Web server listening on port 3000
Here are the logs if i include flask:
Building api
Step 1/8 : FROM python:3.6
---> c1e459c00dc3
Step 2/8 : WORKDIR /api
---> Using cache
---> 8c9da963377c
Step 3/8 : COPY ./requirements.txt /api/requirements.txt
---> Using cache
---> 654c815801f6
Step 4/8 : COPY ./src /api/src
---> 5627279f7323
Removing intermediate container 453bf031c2f7
Step 5/8 : EXPOSE 80
---> Running in efb18f950f7d
---> 1389c63a0bcb
Removing intermediate container efb18f950f7d
Step 6/8 : ENV PORT 80
---> Running in 2ad88bed6343
---> fa85201cc165
Removing intermediate container 2ad88bed6343
Step 7/8 : RUN pip install -r requirements.txt
---> Running in e19107f5a449
Collecting flask==0.12.2 (from -r requirements.txt (line 1))
Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting itsdangerous>=0.21 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Werkzeug>=0.7 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading Werkzeug-0.13-py2.py3-none-any.whl (311kB)
Collecting click>=2.0 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting Jinja2>=2.4 (from flask==0.12.2->-r requirements.txt (line 1))
Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask==0.12.2->-r requirements.txt (line 1))
Downloading MarkupSafe-1.0.tar.gz
Building wheels for collected packages: itsdangerous, MarkupSafe
Running setup.py bdist_wheel for itsdangerous: started
Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
Successfully built itsdangerous MarkupSafe
Installing collected packages: itsdangerous, Werkzeug, click, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.13 click-6.7 flask-0.12.2 itsdangerous-0.24
---> a7be838d3a6e
Removing intermediate container e19107f5a449
Step 8/8 : RUN python src/main.py
---> Running in e86c8adf46f0
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
Notice how when flask is included the other containers dont even build?
Upvotes: 2
Views: 1264
Reputation: 1399
You have a RUN command that can not finish in your app dockerfile. This is preventing your dockerfile from ever finishing building. It's why it says step 8/8 and never says "successfully built" like your web container. I'd move that last run command into an entrypoint or CMD, like you have it in your web docker file, so that way the docker build command can actually finish.
IE CMD ["python", "src/main.py"]
or ENTRYPOINT ["python", "src/main.py"]
That is most likely what is preventing your docker images from being built. Is because of that one last line in APP where you are starting a webserver in the foreground which never closes. Best bet is to place the command into entrypoint so it does not actually run during the build process, but will run when the image is started.
this is the line I'm talking about:
Step 8/8 : RUN python src/main.py
---> Running in e86c8adf46f0
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
VS this one out of the web build.
Step 10/10 : ENTRYPOINT npm start
---> Using cache
---> 9c5ec6770c47
Successfully built 9c5ec6770c47
Successfully tagged testcomposemachine_web:latest
Recreating testcomposemachine_web_1
Starting testcomposemachine_db_1
Attaching to testcomposemachine_db_1, testcomposemachine_web_1
the build will never finish if the command never completes. So yes, in a way the Flask API is preventing your docker-compose command from building all the other files.
Upvotes: 1