Docker compose is not starting all containers (possibly Flask related?) - docker

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 | > docker-web#1.0.0 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?

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.

Related

Docker and Angular prod `file does not exist` Error on stage build COPY command

i do not understand why docker cannot get my angular build folder in container.
Can you see that to help me?
If i build with docker compose command i have this error.
Below are all the steps to build my image and launch my container until the error.
WARNING: The Docker Engine you're using is running in swarm mode.
Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
To deploy your application across the swarm, use `docker stack deploy`.
Building linking-front
Sending build context to Docker daemon 425.6MB
Step 1/9 : FROM node:16.19.0 AS build
---> b22f8aab05da
Step 2/9 : WORKDIR /usr/src/app
---> Using cache
---> 5e2431455b65
Step 3/9 : COPY package.json package-lock.json ./
---> Using cache
---> 11d677269b0e
Step 4/9 : RUN npm install
---> Using cache
---> b5544be9159b
Step 5/9 : COPY . .
---> Using cache
---> 3403bfda57ca
Step 6/9 : RUN npm run build
---> Using cache
---> ae8e7960ac33
Step 7/9 : FROM nginx:1.23.3-alpine
---> 2bc7edbc3cf2
Step 8/9 : COPY nginx.conf /etc/nginx/nginx.conf
---> Using cache
---> beca38c7be94
Step 9/9 : COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
COPY failed: stat usr/src/app/dist/linkingEducationSecurity-front: file does not exist
ERROR: Service 'linking-front' failed to build : Build failed
### STAGE 1: Build ###
FROM node:16.19.0 AS build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build
### STAGE 2: Run ###
FROM nginx:1.23.3-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
I use also docker-compose
version: '3.9'
services:
linking-front:
build: ./linkingEducationSecurity-front/
ports:
- "8080:80"
volumes:
- type: bind
source: ./linkingEducationSecurity-front/src/
target: /app/src
since in the comments you tried to do RUN cd dist && ls which gave you this output :
Step 7/10 : RUN cd dist && ls ---> Running in e8f002e82f3a linking-education-security-front
The steps and dockerfile are perfect. the COPY command from build folder is missing its spell
update this line :
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
to this :
COPY --from=build /usr/src/app/dist/linking-education-security-front /usr/share/nginx/html
and try rebuilding , this might work.

Failing to build docker container on remote host

I have a set of docker containers that are generated from yml files and work ok.
I now want to deploy them to another machine. In another post here I was advised to build the container on the remote host using one of 2 options:
option1 - copy the code to the remote host and apply a modified docker-compose, and build from source using a modified docker-compose
option2 - create an image on local machine, push it to a docker repository, pull it from there, using a modified docker-compose
I'm trying to follow option1 (as a start). I copied the code into the remote host, and modified the Dockerfile, and docker-compose.yml files (snippet1)
The build goes partway through. It downloads the python and postgres images, and starts to process the Dockerfile, but it fails to install the requirements (it fails to install the first requirement - see snippet2)
What can be the problem?
snippet1 - Dockerfile.dair, and docker-compose.yml
cat /home/ubuntu/webServer/web/Dockerfile.dair
FROM python:3.6.1
MAINTAINER User4 <user4#gmail.com>
# Create the group and user to be used in this container
RUN groupadd flaskgroup && useradd -m -g flaskgroup -s /bin/bash flask
# Create the working directory (and set it as the working directory)
RUN mkdir -p /home/flask/app/web
WORKDIR /home/flask/app/web
# Install the package dependencies (this step is separated
# from copying all the source code to avoid having to
# re-install all python packages defined in requirements2.txt
# whenever any source code change is made)
COPY requirements2.txt /home/flask/app/web
RUN pip install --no-cache-dir -r requirements2.txt
# Copy the source code into the container
COPY . /home/flask/app/web
RUN chown -R flask:flaskgroup /home/flask
USER flask
EXPOSE 8000
CMD /usr/local/bin/gunicorn -w 2 -t 3600 -b :8000 project:app
ENV PYTHONUNBUFFERED=1
ENV FLASK_APP=run.py
ENV FLASK_DEBUG=1
,
cat /home/ubuntu/construction_overlay/webServer/docker-compose.dair.yml
version: '3'
services:
web:
restart: always
build:
context: ./web
dockerfile: Dockerfile.dair
volumes:
- /home/ubuntu/construction_overlay/webServer/web:/home/flask/app/web
depends_on:
- postgres
nginx:
restart: always
build: ./nginx
ports:
- "80:80"
volumes:
- /home/ubuntu/construction_overlay/webServer/web:/home/flask/app/web
depends_on:
- web
postgres:
restart: always
build: ./postgresql
volumes:
- data1:/var/lib/postgresql
expose:
- "5432"
volumes:
data1:
snippet2 - the build fails to install the requirements
cat /home/ubuntu/webServer/web/requirements.txt
alembic==0.8.8
atomicwrites==1.3.0
attrs==19.1.0
...
,
docker-compose -f /home/ubuntu/webServer/docker-compose.dair.yml up --build -d;
Building postgres
Step 1/4 : FROM postgres:11.3
---> 4e045cb8eecd
Step 2/4 : ENV POSTGRES_USER postgres_user
---> Using cache
---> 400023c58607
Step 3/4 : ENV POSTGRES_PASSWORD postgres_user
---> Using cache
---> 0cf91f314380
Step 4/4 : ENV POSTGRES_DB construction-overlay-db
---> Using cache
---> 151106ecf13b
Successfully built 151106ecf13b
Successfully tagged webserver_postgres:latest
Building web
Step 1/10 : FROM python:3.6.1
---> 955d0c3b1bb2
Step 2/10 : MAINTAINER User4 <user4#gmail.com>
---> Using cache
---> 128d55ddb4e7
Step 3/10 : RUN groupadd flaskgroup && useradd -m -g flaskgroup -s /bin/bash flask
---> Using cache
---> e2c30915fcf5
Step 4/10 : RUN mkdir -p /home/flask/app/web
---> Using cache
---> 8f44dada5953
Step 5/10 : WORKDIR /home/flask/app/web
---> Using cache
---> 04a895c3fe27
Step 6/10 : COPY requirements.txt /home/flask/app/web
---> Using cache
---> 13ab37e789f8
Step 7/10 : RUN pip install --no-cache-dir -r requirements.txt
---> Running in 61475950cd73
Collecting alembic==0.8.8 (from -r requirements.txt (line 1))
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f1e979262b0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/alembic/
The solution was to add a rule for UDP outbound at the cloud provider
After adding this rule, the pre-requirements install ok
Egress IPv4 UDP 1 - 65535 0.0.0.0/0

Tutorial for running flask on docker fails to override localhost

Following a very helpful tutorial from https://blog.jetbrains.com/pycharm/2017/03/docker-compose-getting-flask-up-and-running/
Take the sample app, run docker-compose up, and everything works perfectly, with the URL running as http://0.0.0.0:5000
However after following the setup for PyCharm to set the project interpreter to docker-compose, I can run, but cannot connect to the server (connection was reset).
I get the same result attempting to run the application through a Docker (rather than Docker Compose) configuration.
Is there an area in PyCharm where I need to specify the port forwarding?
Results from docker-compose up --build:
Building web
Step 1/8 : FROM python:3
---> 1e80caffd59e
Step 2/8 : EXPOSE 5000
---> Using cache
---> fde844406ea1
Step 3/8 : RUN mkdir /app
---> Using cache
---> 4f00b490f3bf
Step 4/8 : WORKDIR /app
---> Using cache
---> 032085080f06
Step 5/8 : COPY requirements.txt /app/requirements.txt
---> Using cache
---> 34bd25f608ad
Step 6/8 : RUN pip install -r requirements.txt
---> Using cache
---> 6162dfe81ac1
Step 7/8 : COPY . /app
---> 4af1cdc75437
Step 8/8 : CMD python flask-compose.py
---> Running in 9ca4e6f7e8cd
Removing intermediate container 9ca4e6f7e8cd
---> 4887b2161461
Successfully built 4887b2161461
Successfully tagged flask-compose_web:latest
Recreating flask-compose_web_1 ... done
Attaching to flask-compose_web_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | 172.22.0.1 - - [14/Dec/2018 14:45:32] "GET / HTTP/1.1" 200 -
web_1 | 172.22.0.1 - - [14/Dec/2018 14:45:32] "GET /favicon.ico HTTP/1.1" 404 -
Log output when running from the IDE with a docker-compose configuration:
FLASK_APP = flask-compose.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder /Users/mathieson/code/flask-compose
/usr/local/bin/docker-compose -f /Users/someone/code/flask-compose/docker-compose.yml -f /Users/someone/code/flask-compose/docker-compose.dev.yml -f /Users/someone/Library/Caches/PyCharm2018.2/tmp/docker-compose.override.102.yml up --exit-code-from web --abort-on-container-exit web
Recreating flask-compose_web_1 ...
Attaching to flask-compose_web_1
web_1 | * Serving Flask app "flask-compose"
web_1 | * Forcing debug mode off
web_1 | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Why can't I access container with Dockerfile and but can access with docker-compose?

I am trying to learn Docker. I have a Hello World Django server application. When I try to run my server using a Dockerfile, my server is unreachable. But when I use docker-compose, I am able to access it.
My question is why, especially when they are quite similar.
My Dockerfile:
FROM python:3
# Set the working directory to /app
WORKDIR /bryne
# Copy the current directory contents into the container at /app
ADD . /bryne
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# done!
Commands used when running server using Dockerfile:
docker build -t swyne-latest
docker run swyne-latest
Result: Cannot access server at 127.0.0.1:8000
My docker-compose.yml:
version: '3'
services:
web:
build: .
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
container_name: swyne
volumes:
- .:/bryne
ports:
- "8000:8000"
Commands used when running server using docker-compose:
docker-compose up
Result: Able to access my server at 127.0.0.1:8000
Thanks
Edit: Output from Dockerfile build:
$ docker build -t swyne-latest .
Sending build context to Docker daemon 60.15MB
Step 1/6 : FROM python:3
3: Pulling from library/python
05d1a5232b46: Already exists
5cee356eda6b: Already exists
89d3385f0fd3: Already exists
80ae6b477848: Already exists
28bdf9e584cc: Already exists
523b203f62bd: Pull complete
e423ae9d5ac7: Pull complete
adc78e8180f7: Pull complete
60c9f1f1e6c6: Pull complete
Digest: sha256:5caeb1a2119661f053e9d9931c1e745d9b738e2f585ba16d88bc3ffcf4ad727b
Status: Downloaded newer image for python:3
---> 7a35f2e8feff
Step 2/6 : WORKDIR /bryne
---> Running in 9ee8283c6cc6
Removing intermediate container 9ee8283c6cc6
---> 5bbd14170c84
Step 3/6 : ADD . /bryne
---> 0128101457f5
Step 4/6 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
---> Running in 55ab661b1b55
Collecting Django>=2.1 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/32/ab/22530cc1b2114e6067eece94a333d6c749fa1c56a009f0721e51c181ea53/Django-2.1.2-py3-none-any.whl (7.3MB)
Collecting pytz (from Django>=2.1->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
Installing collected packages: pytz, Django
Successfully installed Django-2.1.2 pytz-2018.5
Removing intermediate container 55ab661b1b55
---> dce5400552b2
Step 5/6 : EXPOSE 8000
---> Running in c74603a76b54
Removing intermediate container c74603a76b54
---> ee5ef2bf2999
Step 6/6 : CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]
---> Running in 4f5ea428f801
Removing intermediate container 4f5ea428f801
---> 368f73366b69
Successfully built 368f73366b69
Successfully tagged swyne-latest:latest
$ docker run swyne-latest
(no output)
I guess it's normal that unlike docker-compose up, docker run swyne-latest does not allow you to access the web application at 127.0.0.1:8000.
Because the docker-compose.yml file (which is read by docker-compose but not by docker itself) specifies many parameters, in particular the port mapping, which should otherwise be passed as CLI parameters of docker run.
Could you try running docker run -p 8000:8000 instead?
Also, I guess that the line command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000" should probably put inside the Dockerfile itself with a CMD or ENTRYPOINT directive, not in the docker-compose.yml file.
Actually, I've just taken a look at the output of your docker build command and there is an orthogonal issue:
the command
CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]
should be replaced with
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
(See this SO answer for more feedback on this issue, albeit in another language, Java instead of Python.)
As an aside, the complete command to compile the Dockerfile is not docker build -t swyne-latest but docker build -t swyne-latest . (with the final dot corresponding to the folder of the Docker build context).

Can't access running Docker container in browser

I'm working behind a corporate proxy. I'm on Windows 10 machine but I'm also running VMware. Inside VMware I'm running a Fedora 28 image. Because I'm behind a corporate proxy I have to run an NTLM proxy server on Windows 10 so that Fedora can connect to the internet.
I have everything set up and I can access the internet and pull docker images perfectly. I'm following this tutorial here on docker-compose.
This is my directory structure:
docker-compose-tut
├── commander
│ └── Dockerfile
└── docker-compose.yml
Dockerfile
FROM node:latest
ENV http_proxy=http://prx:3128
ENV https_proxy=http://prx:3128
ENV ftp_proxy=http://prx:3128
ENV no_proxy=localhost,127.0.0.1
RUN curl -L https://github.com/joeferner/redis-commander/tarball/v0.4.5 | tar zx
RUN npm install -g redis-commander
ENTRYPOINT ["redis-commander"]
CMD ["--redis-host", "redis"]
EXPOSE 8081
docker-compose.yml
backend:
image: redis:latest
restart: always
frontend:
build: commander
links:
- backend:redis
environment:
- ENV_VAR1 = some_value
ports:
- 8081:8081
environment:
- VAR1=value
restart: always
When I run the command docker-compose up -d I get the following output
[root#localhost docker-compose-tut]$ docker-compose up -d
Building frontend
Step 1/10 : FROM node:latest
---> b064644cf368
Step 2/10 : ENV http_proxy http://prx:3128
---> Using cache
---> f70ae2e24003
Step 3/10 : ENV https_proxy http://prx:3128
---> Using cache
---> 12a4e65a3874
Step 4/10 : ENV ftp_proxy http://prx:3128
---> Using cache
---> 77abdce2f8d7
Step 5/10 : ENV no_proxy localhost,127.0.0.1
---> Using cache
---> 467c4f25e4f7
Step 6/10 : RUN curl -L https://github.com/joeferner/redis-commander/tarball/v0.4.5 | tar zx
---> Using cache
---> e3f8b2d8ad64
Step 7/10 : RUN npm install -g redis-commander
---> Running in 3189b0fa1086
npm WARN deprecated ejs#0.8.8: Critical security bugs fixed in 2.5.5
When I enter the command docker-compose ps this is literally the output
Name Command State Ports
------------------------------
When I enter the command docker-compose --services the output is the following:
backend
frontend
If I open the browser and enter http://localhost:8081 the browser says that it can't establish a connection to http://localhost:8081.
What am I doing wrong?

Resources