✅ Need help with docker deployment
I have an ASP.NET REST API that i want to deploy on my server but i dont understand how this works at all
My image is already pushed but when i try to access my API via CURL request nothing happens
Im using docker-compose on my server
87 Replies
so what happens when you do
docker compose up
?it pulls the thing and starts it
at least from what it looks like
yep, seems fine
and the image itself, you are sure it listens on the correct ports? you should be able to look in the logs of the container and see the ASP.NET logging output, like "listening on ..."
i just dont know what im missing, im also trying to bind 3306 to a db. subdomain but nothing works on there either my connection requests time out
how can i access the logs
my only deployment experience was pulling everything to root and manually executing it :stare:
so
docker logs <container id>
i only have images
i dont know why there is no container
docker-compose-your_service-1
is your container name herethe thing crashes cause i cant connect to the db
i set a reverse proxy using NGINX on port 3306 idk whats up with that
wait what
thats a database port
yes
why on earth would you set a HTTP server as a reverse proxy for a database?
i want to bind 3306 to db.mydomain
you should not expose your database publicly wtf
isnt it public either way if its accessible from 3306
only if you expose port 3306 to the internet
but you dont do that.
then what do i do
cause my backend requires a server address
slow down a sec
is this database only for the containerized app, or is it something you want on the host-server itself, ie, available for all apps on that server?
the database runs on the actual server
which allows me to use it for all applications on that server
its just a few so i just give every program its own database
ok
sure
so like this?
yes
ok great
:SCcrying:
ok so to access the host machine from inside the container, you can either use the internal IP address of the server, or set up an
extra_hosts
directive in your compose file
does my domain count
no, as I would assume that points to your external IP address
the thing is that the DB host is passed as env variable by the docker-compose file which is then read by the backend
yeah
tahts fine
i include this right?
put that in your compose file, yes
and then change the "DB host" env var to be
host.docker.internal
and your db is listening on 3306 locally, right?ye
but its still crashing
:SCgetoutofmyhead:
start it without
-d
so we can see whyi can see the error it cant connect to the specified mysql host
can i see the env variables with some command
maybe something isnt getting passed properly
hm, can we go to voice/screenshare?
i cant at the moment
it must be getting passed properly
likely
can you show your new compose file?
ok, looks fine at a glance
ok lets try this
replace
host-gateway
with 172.17.0.1
thats the hosts internal IP inside dockers network
actually, some people are saying you should be able to use the hosts hostnamestill cant connect
i used host.docker.internal on my debugging .env file too and it worked
on my PC i have XAMPP for mysql and on WSL i have mariaDB which the backend was both able ot connect to
yeah its enabled by default on docker desktop
:SCcrying:
hm
is your database binding to
localhost
or 0.0.0.0
?Uh
Where can I check that
sudo ss -tulpn | grep LISTEN
Is there any reason your database isn't hosted in docker?
alternatively
sudo lsof -nP -iTCP -sTCP:LISTEN
since im pretty sure mysql uses TCP
actually, might as well target the port since its a known one: sudo lsof -nP -i:3306
cause if its not binding to 0.0.0.0, we will have to add a relay
and honestly, since its on the host, it would probably be a bad idea to bind to 0.0.0.0 anyways
depends on how the server is exposed to the internetNot rly
I just had it installed like that
127.0.0.1
and 0.0.0.0
oh, so both?
thats not what that means :p
thats listening only on
127.0.0.1
ah
:SCWHATISGOINGON:
https://dev.to/mjnaderi/accessing-host-services-from-docker-containers-1a97
check this blogpost, scroll down to "solution using socat"
that should do the trick for you
i need to set it to 3306 instead of the example 5432 right?
it runs
ya
:plink:
so it works now?
yes
yay
but i have another question
ye
how can i change the port of my ASP.NET backend? 8080 is in use for a temp backend i cant dispose atm
you can do that with an env var
is it that EXPOSE param in the dockerfile?
no, that tells docker to expose that port on the container
so you must change that too, ofc, to match whatever port your backend wants to use
but uh
you do know that whatever port is used inside the container dont need to match the outside, right?
i want it to use port 712
i thought this would bind the port
ports:
- "712:8080"
correct
so to the outside my app is running on :712?
it seems to be
yes
wait
left side is "docker host", right side is "inside container"
i got 401 on my test script which is the expected result
yeees
it works
also ive been getting into EF Core insead of writing SQL queries i love it :stare:
yeah its good. very convention-based, and if your app needs to run a lot of very complicated queries it might become hard thou
i do have a question tho
I'd recommend using EF for most code, and use the raw underlying connection if you ever need to make a 4-way join or something 🙂
my bigger projects dont always involve only one database
for example id have a database for things like settings and another one for user accounts
for this id be able to not select a db on my connection string and just go
SELECT * FROM Settings.local_settings;
for example, on EF Core i did that by making a DBContext Factory that takes a database as input and sets it, is that how it works?there are many ways to do it
one would be to have more than one context
you could have a
UserDataDbContext
and a SettingsDbContext
for exampleye its what i thought of when i saw how EF Core works
Id say multiple contexts is the "standard" way, assuming there is no cross-concern
i just didnt have the chance to test it yet cause my current project (first with EF Core) only involves a single DB
btw when i push a new image to docker and run that docker compose command, itt will update and deploy the image right?
I think so yeah.
if you dont specify an image version, it assumes latest
oh and btw, I would recommend moving away from mysql if I were you
its not a very good database engine. postgres is just superior in every way these days
originally i didnt like the way PSQL worked but i dont think with EF core thats an issue anymore