I have my flask webapp up and running in Docker and trying to implement some unit tests and having trouble executing the tests. While my containers are up and running, I run the following:
docker-compose run app python3 manage.py test
to try to execute my test function in manage.py:
import unittest
from flask.cli import FlaskGroup
from myapp import app, db
cli = FlaskGroup(app)
#cli.command()
def recreate_db():
db.drop_all()
db.create_all()
db.session.commit()
#cli.command()
def test():
""" Runs the tests without code coverage"""
tests = unittest.TestLoader().discover('myapp/tests', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
return 1
if __name__ == '__main__':
cli()
But since I have a start.sh in my Dockerfile it just executes my Gunicorn start.sh but it doesn't run my test. I just see the following in the console, but no trace of my test() function.
[2018-12-08 01:21:08 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2018-12-08 01:21:08 +0000] [1] [DEBUG] Arbiter booted
[2018-12-08 01:21:08 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2018-12-08 01:21:08 +0000] [1] [INFO] Using worker: sync
[2018-12-08 01:21:08 +0000] [9] [INFO] Booting worker with pid: 9
[2018-12-08 01:21:08 +0000] [11] [INFO] Booting worker with pid: 11
[2018-12-08 01:21:08 +0000] [13] [INFO] Booting worker with pid: 13
[2018-12-08 01:21:08 +0000] [1] [DEBUG] 3 workers
start.sh:
#!/bin/sh
# Wait until MySQL is ready
while ! mysqladmin ping -h"db" -P"3306" --silent; do
echo "Waiting for MySQL to be up..."
sleep 1
done
source venv/bin/activate
# Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn -b 0.0.0.0:5000 wsgi --reload --chdir usb_app --timeout 9999 --workers 3 --access-logfile - --error-logfile - --capture-output --log-level debug
Does anyone know why or how I can execute the test function in an existing container without having to start the Gunicorn workers again?
I presume start.sh is your entrypoint. If not, you can make it as an entrypoint instead of putting it in CMD.
We can override the entrypoint script using --entrypoint argument, I do it as below -
docker-compose run --rm --entrypoint "python3 manage.py test" app
Related
I have docker image which locally listens port 5000.
I have deployed images to Azure Container Registry.
I have then created Azure Container Instance with:
az container create --resource-group myResourceGroup --name aci-tutorial-app --image <acrLoginServer>/aci-tutorial-app:v1 --cpu 1 --memory 1 --registry-login-server <acrLoginServer> --registry-username <service-principal-ID> --registry-password <service-principal-password> --dns-name-label <aciDnsLabel> --ports 80
Container was succesfully created, but browser does not respond with DNS name. Logs look like following.
PS C:\azure\ACIPythonAPI> az container logs --resource-group My-Container-RG --name mycontainer
[2022-01-10 08:33:36 +0000] [18] [INFO] Starting gunicorn 20.1.0
[2022-01-10 08:33:36 +0000] [18] [INFO] Listening at: http://0.0.0.0:5000 (18)
[2022-01-10 08:33:37 +0000] [18] [INFO] Using worker: sync
[2022-01-10 08:33:37 +0000] [25] [INFO] Booting worker with pid: 25
What could be problem? Port definitions are incorrect in docker? How to troubleshoot?
I tested in my environment, and it is working fine for me. I am using nginx images and pushed to container registry.
Created container instance using image from container registry which recently pushed.I am using the same command which you have used to create container.
Would suggest you use FDQN or Public IPAddress of container instance to hit on browser and able to see response from container instance.
I know that (many) versions of this question have been asked, but none of them have solved my problem. I have a Dash app that is running with Docker on an AWS EC2 instance. I would like to access it from my browser (Firefox), but I keep getting the Firefox can’t establish a connection to the server at 17.67.12.567:8085 (I have changed the public IPv4 address, 17.67.12.567 from the actual one I am using, but that shouldn't matter?).
I run the app with docker run -t -i -p 80:80 app_name, which outputs:
[2021-10-27 08:15:00 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2021-10-27 08:15:00 +0000] [1] [INFO] Listening at: http://0.0.0.0:8085 (1)
[2021-10-27 08:15:00 +0000] [1] [INFO] Using worker: threads
[2021-10-27 08:15:00 +0000] [8] [INFO] Booting worker with pid: 8
[2021-10-27 08:15:00 +0000] [9] [INFO] Booting worker with pid: 9
[2021-10-27 08:15:00 +0000] [10] [INFO] Booting worker with pid: 10
[2021-10-27 08:15:00 +0000] [11] [INFO] Booting worker with pid: 11
So, I would expect to be able to access the app at http:17.67.12.567:8085, but, when I do this, I get the Firefox can’t establish a connection to the server at 17.67.12.567:8085 error.
I have read a lot about firewalls and security group settings, and the EC2 instance's security group settings are as open as possible, I think (I know this is a bad idea, but I will narrow them down once I can access the app!); below is a screenshot of the EC2 instance's security group settings.
That is all the information that I can think to give, thanks in advance for the help!
Well, I figured this out about five minutes after posting, sorry, but hopefully this helps some other poor soul that is working on a project that they do not totally understand!
The problem was the with the port mapping; it needs to match exactly the port shown in Listening at: http://0.0.0.0:8085 (1). So, instead of using docker run -t -i -p 80:80 app_name I needed to use docker run -t -i 8085:8085 app_name.
Once I ran the app with docker run -t -i 8085:8085 app_name I was able to access it on http:17.67.12.567:8085, as expected.
Thank you to this answer for guidance!
Being new to Docker, I am following a tutorial but using my own personal MERN-stack project. My personal project folder structure consists of a frontend folder and a backend folder and my Dockerfile placed in the root directory. My frontend is uses localhost:3000 and my backend at localhost:5000. I am trying to view my application in the browser; however, it takes me to a page that states this site can't be reached for http://localhost:3000 and http://localhost:5000 and for http://172.17.0.3:3000 its just a blank forever loading page.
If it helps, i'm using a macOS.
steps i've taken:
docker build -t foodcore:1.0 .
docker run -p 3001:3000 -p 5001:5000 foodcore:1.0
outcome in my terminal:
> server#1.0.0 dev
> concurrently "nodemon server.js" "npm run client"
[0] [nodemon] 2.0.6
[0] [nodemon] to restart at any time, enter `rs`
[0] [nodemon] watching path(s): *.*
[0] [nodemon] watching extensions: js,mjs,json
[0] [nodemon] starting `node server.js`
[1]
[1] > server#1.0.0 client
[1] > cd .. && cd client && npm start
[1]
[1]
[1] > client#0.1.0 start
[1] > react-scripts start
[1]
[0] Thu, 07 Jan 2021 01:15:15 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at server.js:12:9
[0] Thu, 07 Jan 2021 01:15:15 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
[0] Listening at: http://localhost:5000
[1] ℹ 「wds」: Project is running at http://172.17.0.3/
[1] ℹ 「wds」: webpack output is served from
[1] ℹ 「wds」: Content not from webpack is served from /FoodCore/client/public
[1] ℹ 「wds」: 404s will fallback to /
[1] Starting the development server...
[1]
[1] Compiled successfully!
[1]
[1] You can now view client in the browser.
[1]
[1] Local: http://localhost:3000
[1] On Your Network: http://172.17.0.3:3000
[1]
[1] Note that the development build is not optimized.
[1] To create a production build, use npm run build.
docker container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c5abad55b1b foodcore:1.0 "npm run dev" 32 minutes ago Up 32 minutes 0.0.0.0:3001->3000/tcp, 0.0.0.0:5001->5000/tcp optimistic_chandrasekhar
Dockerfile
FROM node:latest
RUN mkdir -p /FoodCore
COPY . /FoodCore
WORKDIR /FoodCore/client
RUN npm install
WORKDIR /FoodCore/server
RUN npm install
EXPOSE 3000 5000
ENTRYPOINT [ "npm", "run", "dev" ]
Thank you very much for taking your time reading this.
UPDATE
Turns out i was trying to access http://localhost:3000 but i set my application to run at 3001.
UPDATE
Turns out by through this outline
docker run -p <host_port>:<container_port>
i was initially setting my host port to 3001 instead of 3000. Thus, accessing the wrong port
Kind of a newbie question, hopefully I'm on the right tracks :)
I have a FastAPI web server hosting some endpoint APIs for some IOS app I'm working on.
My webserver is running on an AWS EC2 machine using GUNICORN & Docker.
At first, I've hosted my webserver running simple HTTP, with the following Dockerfile:
WORKDIR /app
COPY . /app
# add files to Docker environment
...
# run app
RUN pip install -r requirements.txt
EXPOSE 80
CMD ["gunicorn", "-b", "0.0.0.0:80", "-k", "uvicorn.workers.UvicornWorker", "main:app"]
This worked perfectly, and I was able to access my APIs using http://<ec2-machine-public-ip>/...
However, I want to make sure all communications between a client (app-user) and my server are secure using HTTPS.
Since I only want to use my webserver for hosting APIs (and don't actually want anyone accessing the routes via browser), I figured a self-signed certificate would suffice (despite browser warnings).
To do that, I've generated self-signed certificates with OpenSSL using the following command:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
This created cert.pem and key.pem files.
Then, I attempted running with HTTPS with the following Dockerfile:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
WORKDIR /app
COPY . /app
# add files to Docker environment
...
# run app
RUN pip install -r requirements.txt
EXPOSE 433
CMD ["gunicorn", "-b", "0.0.0.0:433", "--keyfile", "key.pem", "--certfile", "cert.pem", "-k", "uvicorn.workers.UvicornWorker", "main:app"]
Unfortunately, when I try to access my APIs using https://<ec2-machine-public-ip>/... - I get an error: "This site can't be reached".
I should mention that everything looks normal in my container's logs:
[2020-11-13 17:02:56 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-11-13 17:02:56 +0000] [1] [INFO] Listening at: https://0.0.0.0:433 (1)
[2020-11-13 17:02:56 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-11-13 17:02:56 +0000] [8] [INFO] Booting worker with pid: 8
[2020-11-13 17:02:56 +0000] [8] [INFO] Started server process [8]
2020-11-13 17:02:56,490 Started server process [8]
[2020-11-13 17:02:56 +0000] [8] [INFO] Waiting for application startup.
2020-11-13 17:02:56,491 Waiting for application startup.
[2020-11-13 17:02:56 +0000] [8] [INFO] Application startup complete.
2020-11-13 17:02:56,491 Application startup complete.
BTW, my EC2 machine has port 443 open for HTTPS from all IP addresses (Here's a screenshot from my machine's security group inbound-rules).
What am I doing wrong?
Any help is appreciated!
I'm running RabbitMQ locally using:
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
Some log:
narley#brittes ~ $ docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
2020-01-08 22:31:52.079 [info] <0.8.0> Feature flags: list of feature flags found:
2020-01-08 22:31:52.079 [info] <0.8.0> Feature flags: [ ] drop_unroutable_metric
2020-01-08 22:31:52.079 [info] <0.8.0> Feature flags: [ ] empty_basic_get_metric
2020-01-08 22:31:52.079 [info] <0.8.0> Feature flags: [ ] implicit_default_bindings
2020-01-08 22:31:52.080 [info] <0.8.0> Feature flags: [ ] quorum_queue
2020-01-08 22:31:52.080 [info] <0.8.0> Feature flags: [ ] virtual_host_metadata
2020-01-08 22:31:52.080 [info] <0.8.0> Feature flags: feature flag states written to disk: yes
2020-01-08 22:31:52.160 [info] <0.268.0> ra: meta data store initialised. 0 record(s) recovered
2020-01-08 22:31:52.162 [info] <0.273.0> WAL: recovering []
2020-01-08 22:31:52.164 [info] <0.277.0>
Starting RabbitMQ 3.8.2 on Erlang 22.2.1
Copyright (c) 2007-2019 Pivotal Software, Inc.
Licensed under the MPL 1.1. Website: https://rabbitmq.com
## ## RabbitMQ 3.8.2
## ##
########## Copyright (c) 2007-2019 Pivotal Software, Inc.
###### ##
########## Licensed under the MPL 1.1. Website: https://rabbitmq.com
Doc guides: https://rabbitmq.com/documentation.html
Support: https://rabbitmq.com/contact.html
Tutorials: https://rabbitmq.com/getstarted.html
Monitoring: https://rabbitmq.com/monitoring.html
Logs: <stdout>
Config file(s): /etc/rabbitmq/rabbitmq.conf
Starting broker...2020-01-08 22:31:52.166 [info] <0.277.0>
node : rabbit#1586b4698736
home dir : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.conf
cookie hash : bwlnCFiUchzEkgAOsZwQ1w==
log(s) : <stdout>
database dir : /var/lib/rabbitmq/mnesia/rabbit#1586b4698736
2020-01-08 22:31:52.210 [info] <0.277.0> Running boot step pre_boot defined by app rabbit
...
...
...
2020-01-08 22:31:53.817 [info] <0.277.0> Setting up a table for connection tracking on this node: tracked_connection_on_node_rabbit#1586b4698736
2020-01-08 22:31:53.827 [info] <0.277.0> Setting up a table for per-vhost connection counting on this node: tracked_connection_per_vhost_on_node_rabbit#1586b4698736
2020-01-08 22:31:53.828 [info] <0.277.0> Running boot step routing_ready defined by app rabbit
2020-01-08 22:31:53.828 [info] <0.277.0> Running boot step pre_flight defined by app rabbit
2020-01-08 22:31:53.828 [info] <0.277.0> Running boot step notify_cluster defined by app rabbit
2020-01-08 22:31:53.829 [info] <0.277.0> Running boot step networking defined by app rabbit
2020-01-08 22:31:53.833 [info] <0.624.0> started TCP listener on [::]:5672
2020-01-08 22:31:53.833 [info] <0.277.0> Running boot step cluster_name defined by app rabbit
2020-01-08 22:31:53.833 [info] <0.277.0> Running boot step direct_client defined by app rabbit
2020-01-08 22:31:53.922 [info] <0.674.0> Management plugin: HTTP (non-TLS) listener started on port 15672
2020-01-08 22:31:53.922 [info] <0.780.0> Statistics database started.
2020-01-08 22:31:53.923 [info] <0.779.0> Starting worker pool 'management_worker_pool' with 3 processes in it
completed with 3 plugins.
2020-01-08 22:31:54.316 [info] <0.8.0> Server startup complete; 3 plugins started.
* rabbitmq_management
* rabbitmq_management_agent
* rabbitmq_web_dispatch
Then I go to http:localhost:15672 and page doesn't load. No error is displayed.
Interesting thing is that it worked last time I used it (about 3 weeks ago).
Can anyone give me some help?
Cheers!
have a try:
step 1, going into docker container
docker exec -it rabbitmq bash
step 2, run it in docker container
rabbitmq-plugins enable rabbitmq_management
is work for me
I got it working by simply upgrading docker.
Was running docker 18.09.7 and upgrade to 19.03.5.
In my case, clearing the cookies up has fixed this issue instantly.