Loading...

  • 17 Apr, 2026

Deploying a Dart (Flutter) API on Docker

Deploying a Dart (Flutter) API on Docker

Docker simplifies the deployment of applications by packaging them with all their dependencies in lightweight containers. In this guide, we’ll explore how to deploy a Dart API (built with shelf) in a Docker container for easy scalability and deployment.

Step 1: Create a Dart API Project

First, if you haven’t already, create a Dart API using shelf :

dart create dart_api
cd dart_api

Edit pubspec.yaml to include:

dependencies:
  shelf: ^1.4.0
  shelf_router: ^1.1.4

Run:

dart pub get

Create a server.dart file inside bin/ :

import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';

void main() async {
  final router = Router()
    ..get('/', (Request request) => Response.ok('Hello, Docker!'));

  final handler = Pipeline().addMiddleware(logRequests()).addHandler(router);
  final server = await io.serve(handler, InternetAddress.anyIPv4, 8080);
  print('Server running on http://${server.address.host}:${server.port}');
}

Step 2: Create a Dockerfile

Inside your project root, create a Dockerfile :

# Use Dart official image
FROM dart:stable AS build

# Set working directory
WORKDIR /app

# Copy files and get dependencies
COPY . .
RUN dart pub get

# Expose port 8080
EXPOSE 8080

# Run the application
CMD ["dart", "bin/server.dart"]

Step 3: Build and Run the Docker Container

1. Build the Docker Image

Run the following command to build the Docker image:

docker build -t dart_api .

2. Run the Docker Container

Run the container with:

docker run -p 8080:8080 dart_api

Now, the API should be accessible at:

http://localhost:8080/

To verify, run:

curl http://localhost:8080/

Expected Output:

Hello, Docker!

Step 4: Deploy to a Cloud Provider

To deploy your containerized API, push it to a container registry like Docker Hub, AWS, or Google Cloud.

1. Tag and Push to Docker Hub

docker tag dart_api yourdockerhubusername/dart_api
docker push yourdockerhubusername/dart_api

2. Deploy to a Cloud Provider

You can use Kubernetes, AWS ECS, or Google Cloud Run to deploy your API easily.

For example, deploying with Docker Compose:

Create a docker-compose.yml file:

version: '3.8'
services:
  dart_api:
    image: yourdockerhubusername/dart_api
    ports:
      - "8080:8080"

Then run:

docker-compose up -d

Conclusion

Dockerizing a Dart API makes it easy to deploy and scale across different cloud platforms. By using Docker, you ensure consistency across environments and simplify dependency management.

Now, your Dart API is running in a Docker container and ready for production deployment! 🚀

 

John Smith

How puzzling all these changes are! I'm never sure what I'm going to turn into a tidy little room.