Slack icon GitHub icon StackOverflow icon

Running inside a Docker container

How to run your Kitura application inside a Docker container.

Prerequisities:

warning Make sure you have installed the command-line tools as described in Installation before you begin.


Install Docker on your development system.

Generating the scaffolded application

First, start the Kitura server generation (see Command line tools):

$ kitura create

Enter docker-getting-started as the application name.

? What's the name of your application? docker-getting-started

info Note: You can use a different name for the application, but if you do, be sure to substitute your name for docker-getting-started throughout the rest of this tutorial.

Press Enter to accept the default directory for the project (the same as the application name).

? Enter the name of the directory to contain the project: (docker-getting-started)

Select Scaffold a starter at the type of project prompt and press Enter.

? Select type of project: (Use arrow keys)
❯ Scaffold a starter
  Generate a CRUD application

Select Basic at the application pattern prompt (this determines the default set of capabilities) and press Enter.

? Select capability presets for application pattern: (Use arrow keys)
❯ Basic
  Web
  Backend for frontend

Press Space to select the capabilities for the Basic application pattern.

Use the spacebar to select or deselect any capabilities. For this tutorial you must select Docker files.

? Select capabilities: (Press <space> to select, <a> to toggle all, <i> to inverse selection)
❯ ◯ Static web file serving
  ◯ Swagger UI
  ◯ Embedded metrics dashboard
  ◉ Docker files

Press Enter to accept the default of not generating code from a swagger specification in the scaffolding.

? Select endpoints to generate: (Press <space> to select, <a> to toggle all, <i> to inverse selection)
❯ ◯ Swagger file serving endpoint
  ◯ Endpoints from a swagger file

Press Enter to accept the default of not generating a Swift server SDK from a swagger file in the scaffolding.

? Would you like to generate a Swift server SDK from a Swagger file? (y/N)

Press Enter to accept the default of not including any boilerplate for services in the scaffolding.

? Generate boilerplate for services: (Press <space> to select, <a> to toggle all, <i> to inverse selection)
❯ ◯ Cloudant / CouchDB
  ◯ Redis
  ◯ MongoDB
  ◯ PostgreSQL
  ◯ Object Storage
  ◯ AppID
  ◯ Auto-scaling ---

The generator will display messages as it scaffolds and builds the application including:

  1. Initializing the project folder structure.

  2. Creating and compiling default Swift files.

  3. Downloading and installing dependent Swift modules (as if you had manually run swift build).

Now you should have a generated application directory similar to this:

docker-getting-started/
├── Sources/
│   ├── Application
│   │   ├── Application.swift
│   │   └── Routes/
│   └── docker-getting-started
│       └── main.swift
├── Package.swift
├── README.md
├── config.json
├── Dockerfile
├── Dockerfile-tools
├── .dockerignore
└── ...

info For a description of the generated files, take a look at the structure of a generated project in the Project layout reference.

Change to the application directory:

$ cd docker-getting-started

Build the Docker run and Docker build tools images:

$ docker build -t myapp-run .
$ docker build -t myapp-build -f Dockerfile-tools .

You may customize the names of these images by specifying a different value after the -t option.

To compile the application using the build tools Docker image, run:

$ docker run -v $PWD:/root/project -w /root/project myapp-build /swift-utils/tools-utils.sh build release

To run the application in the Docker container:

$ docker run -it -p 8080:8080 -v $PWD:/root/project -w /root/project myapp-run sh -c .build-ubuntu/release/docker-getting-started

Confirm the application is running locally by opening the URL http://localhost:8080 in your browser. You’ll see something like this:

Welcome to Kitura
Your Kitura based server is up and running!

Congratulations, you now have a simple Kitura web application in a Docker image which can be distributed and deployed to other Docker servers.

Next Steps

Take a look at the other tutorials.