Docker server
On our platform you can deploy any web application using Docker so you can access it with traditional HTTP requests. You can use your favorite programming language (PHP, Java, Go, ...) and any web framework to host all your website or CMS (Wordpress, Drupal, Django, ...) or microservice without managing the cloud itself (provisioning, deployment, setup, HTTPS certificates, security patches, cloud change...)
In this quick tutorial, you'll learn how to create and deploy a basic web server with Docker you can call from HTTP.
1. Prepare the project
Let's start from our ready-to-use project. Select your favorite web technology and git clone
the project.
Then go directly to the step 4 to prepare the deployment.
- Node
- Deno
- PHP
- Python
- Go
- Java
git clone https://github.com/ScaleDynamics/docker-node my-docker
cd my-docker/
npm install
git clone https://github.com/ScaleDynamics/docker-deno my-docker
cd my-docker/
git clone https://github.com/ScaleDynamics/docker-php my-docker
cd my-docker/
git clone https://github.com/ScaleDynamics/docker-python my-docker
cd my-docker/
git clone https://github.com/ScaleDynamics/docker-go my-docker
cd my-docker/
git clone https://github.com/ScaleDynamics/docker-java my-docker
cd my-docker/
or,
Let's create manually your own my-docker
working directory, and follow the next steps
mkdir my-docker
cd my-docker/
2. Create a basic HTTP web server with Docker
- Node
- Deno
- PHP
- Python
- Go
- Java
Create a package.json
file
{
"name": "my-docker",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node ."
}
}
Install Express
npm install express
Init a new HTTP server
Create a new index.js
file in the project directory, and copy-paste the following code into it:
const express = require("express");
const app = express();
const port = process.env.PORT || 8080;
app.get("/:name?", (req, res) => {
const name = req.params.name || "World";
const version = process.version;
res.send(`Hello ${name} from Node.js ${version}`);
});
app.listen(port, () => {
console.log(`HTTP server listening on port ${port}`);
});
Init a new Dockerfile
FROM node:lts
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --production --silent
COPY . ./
EXPOSE 8080
CMD [ "node", "index.js" ]
Init a new HTTP server
Create a new index.ts
file in the project directory, and copy-paste the following code into it:
import { serve } from "https://deno.land/std@0.130.0/http/server.ts";
const port = 8080;
const handler = (request: Request): Response => {
const url = new URL(request.url);
const name = url.pathname.slice(1) || "World";
const version = Deno.version.deno;
const body = `Hello ${name} from Deno v${version}`;
return new Response(body, { status: 200 });
};
console.log(`HTTP server listening on port ${port}`);
await serve(handler, { port });
Init a new Dockerfile
FROM denoland/deno:latest
WORKDIR /usr/src/app
COPY . .
EXPOSE 8080
CMD ["deno", "run", "--allow-net", "index.ts"]
Init a new HTTP server
Create a new index.php
file in the project directory, and copy-paste the following code into it:
<?php
$name = $_GET["name"] ?? "World";
$version = phpversion();
echo "Hello ".$name." from PHP v".$version;
?>
Init a new Dockerfile
FROM php:apache
COPY . /var/www/html
EXPOSE 80
Install Flask
python3 -m pip install flask
Init a new HTTP server
Create a new index.py
file in the project directory, and copy-paste the following code into it:
import platform
from flask import Flask
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name='World'):
version = platform.python_version()
return 'Hello {} from Python v{}'.format(name, version)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Init a new Dockerfile
FROM python:latest
COPY . .
RUN python3 -m pip install flask
EXPOSE 8080
CMD ["python", "-u", "index.py"]
Init a new HTTP server
Create a new index.go
file in the project directory, and copy-paste the following code into it:
package main
import (
"fmt"
"net/http"
"runtime"
)
func handler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
if name == "" {
name = "World"
}
version := runtime.Version()[2:]
fmt.Fprintf(w, "Hello %s from Go v%s", name, version)
}
func main() {
port := 8080
fmt.Printf("HTTP server listening on port %d", port)
http.HandleFunc("/", handler)
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
Init a new Dockerfile
FROM golang:latest
WORKDIR /usr/src/app
COPY . .
RUN go build -o /usr/bin/app index.go
EXPOSE 8080
CMD ["/usr/bin/app"]
Init a new HTTP server
Create a new Main.java
file in the project directory, and copy-paste the following code into it:
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class Main {
static final int PORT = 8080;
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null);
server.start();
System.out.println("Server started on port " + PORT);
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
Map<String, String> params = Main.parseQueryString(exchange.getRequestURI().getQuery());
String version = System.getProperty("java.version");
String name = params.get("name") != null ? params.get("name") : "unknown";
String response = "Hello " + name + " from Java " + version;
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
System.out.println("Response: " + response + " sent to " + exchange.getRemoteAddress());
os.close();
}
}
public static Map<String, String> parseQueryString(String qs) {
Map<String, String> result = new HashMap<>();
if (qs == null)
return result;
int last = 0, next, l = qs.length();
while (last < l) {
next = qs.indexOf('&', last);
if (next == -1)
next = l;
if (next > last) {
int eqPos = qs.indexOf('=', last);
try {
if (eqPos < 0 || eqPos > next)
result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), "");
else
result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
last = next + 1;
}
return result;
}
}
Init a new Dockerfile
FROM openjdk:17
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN javac Main.java
EXPOSE 8080
CMD ["java", "Main"]
3. Install the ScaleDynamics SDK
To install the SDK you need Node.js installed on your computer.
Look at https://nodejs.org/en/download/ to install it.
Install the SDK to access all CLI commands:
npm install warp --save-dev
To access our CLI, use npx warp
. You can have the list of available commands and help with
npx warp help
4. Create the project and the environment
The deployment of a Docker server requires to indicate in which project and which environment you want to deploy and execute your module.
A project is a name that identifies a website, a web app, a microservice or an API.
An environment is a name that identifies a cloud execution configuration to run modules. For example you can have 'pre-prod', 'demo', 'staging' or 'production' environments. Each one will have it's own cloud resource configuration.
Login to your account
To access projects and deployment resources you need a ScaleDynamics account. You can sign up here to create your account. Subscription is FREE, no credit card required.
Once your account is created, you can login to your account with your email and password:
npx warp login
Create a project
A project identifies a website, a web app, a microservice or an API.
Let's create a docker
project, and let's indicate the SDK we are working in it.
npx warp project create docker
npx warp project select docker
Create an environment
Let's create a demo
environment, and let's indicate the SDK we are working in it.
npx warp env create demo
npx warp env select demo
5. Setup services for the environment
Before deploying you need to enable the Managed HTTP docker service and assign cloud resources to run it. To do that, open the console, select the organization, the docker project and the demo environment. Then enable the Managed HTTP docker service and assign a Shared Free cloud resource on the provider and the region you choose.
After subscribing resources, you will see in the console your services configurations.
6. Deploy the server
You're now ready to deploy the Docker server.
npx warp deploy
During the deployment, you will have to indicate the url where you want to access your server after deployment.
✔ Enter a hostname for 'docker' (fully qualified or not), leave blank for random: …
You can enter a name or press return to get a random one.
After deployment the url to use to call your module is dumped on the terminal.
if you want to know the deployment url, you can access the console or use the following command to get it:
npx warp env info
7. Call the server
Now the server is deployed, let's call it from HTTP using curl for example:
curl https://DEPLOYMENT_URL
Chapeau!
Congrats', you deployed your first Managed HTTP docker. Want to continue?