Context
I am trying to create a Dart server with the library shelf.
I want it a bit scalable in order to get to production when my project will be ready.
And to facilitate the production release, I decided to use Docker in order to deploy it easily.
Dart Shelf Server
Here is my Dockerfile :
# Use latest stable channel SDK.
FROM dart:stable AS build
# Resolve app dependencies.
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get
# Copy app source code (except anything in .dockerignore) and AOT compile app.
COPY . .
RUN dart compile exe bin/server.dart -o bin/server
# Build minimal serving image from AOT-compiled `/server`
# and the pre-built AOT-runtime in the `/runtime/` directory of the base image.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/
# Start server.
EXPOSE 8080
CMD ["/app/bin/server"]
(it is the basic dockerfile provided by Shelf readme)
Docker-compose file with Database
Now, I need a database.
I put it in a separate container.
Here is my docker-compose.yaml
file :
version: "3.7"
services:
db:
restart: always
image: mysql
environment:
MYSQL_USER: admin
MYSQL_PASS: secret
MYSQL_ROOT_PASSWORD: secret
MYSQL_PASSWORD: secret
MYSQL_DATABASE: mydatabase
# volumes:
# - toast-datavolume:/var/lib/mysql
ports:
- "3306:3306"
networks:
- mynetwork
server:
build: .
ports:
- "8000:8080"
depends_on:
- db
environment:
- MYSQL_HOST=db
links:
- db:mysql
networks:
- mynetwork
networks:
mynetwork:
# volumes:
# toast-datavolume:
(I commented out the persistence part, as I just want this to work first)
Dart code
Finally, here is the dart code responsible of the connection :
try {
print('try');
var settings = ConnectionSettings(
host: 'db',
port: 3306,
user: 'admin',
password: 'secret',
db: 'mydatabase',
timeout: Duration(seconds: 30),
);
var conn = await MySqlConnection.connect(settings);
var result = await conn.query('insert into users (name, email, age) values (?, ?, ?)', ['Bob', 'bob@bob.com', 25]);
return 1;
} catch (exception) {
print(exception);
return -1;
}
Error
Now, when I launch my containers, everything goes correctly. However, when I try to launch this part of the code (with a curl request, redirected here by some code above), here is my exception (on the server
docker logs) :
2023-06-07 11:45:14 2023-06-07T09:45:14.465441 0:00:00.011712 POST [400] /register
2023-06-07 11:46:42 try
2023-06-07 11:46:42 SocketException: Broken pipe (OS Error: Broken pipe, errno = 32), address = db, port = 33206
The POST
line shows my curl
command.
The second line is my print('try')
line.
Here are what I already tried :
- Remove the
network
indocker-compose.yaml
to use default network - Use
mysql://db:3306
as host in my dart code - Log into my mysql container from my local machine (works perfectly, so the issue is the connection between dart server and mysql server)
- I tried a lot of things I don't recall, as I spent 2 days tweaking around to make this work.
Thanks for your help !