Dockerize

Docker Installation

You can skip this if Docker is already installed on your machine.

Docker client for macOS is available at the link below:

How to Dockerize?

In order to ainize (i.e., deploy on Ainize) applications, the applications need to be dockerized first. Here you'll find an instruction on how to dockerize with step-by-step examples for two major programming frameworks:

Only Dockerfile at repo's root directory is supported currently.

The filename of Dockerfile should be case-sensitive and not be included file extensions.

Node.js

Create a simple web server

We will go through how to dockerize a simple web server from scratch. Before getting started, Node.js and Docker need to be installed in advance.

1. Create a Node.js project.

npm init

2. Initialize package.json file.

{
  "name": "ainize-docker-nodejs",
  "version": "1.0.0",
  "description": "tutorial for dockerize",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "ainize",
  "license": "ISC"
}

3. Generate index.js file.

touch index.js

4. Fill in the simple code snippet.

console.log('hello');

5. Run the simple code and it will show the result.

npm start
> ainize-docker-nodejs@1.0.0 start /Users/minsulee/comcom/ainize-docker-nodejs
> node index.js

hello

6. Update index.js to a simple web server.

const express = require('express');

const PORT = 80;
const app = express();

app.get('/', (req, res) => {
  res.json('Hello World');
});

app.listen(PORT, () => {
  console.log(`Listening to requests on http://localhost:${PORT}`);
});

7. Install express package.

npm install express

8. Run npm start and check the server is running.

> ainize-docker-nodejs@1.0.0 start /Users/minsulee/comcom/ainize-docker-nodejs
> node index.js

Listening to requests on http://localhost:80

Dockerize

9. Create Dockerfile.

touch Dockerfile

10. Open Dockerfile and edit the file like below.

FROM node:10
WORKDIR /workspace
COPY package*.json ./
RUN npm install
COPY . .

EXPOSE 80

CMD [ "node", "index.js" ]

11. Build your Dockerfile.

docker build -t webapp .

12. Check the docker image.

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webapp              latest              1e087c9266fa        24 minutes ago      907MB

13. Now run dockerized webapp.

docker run -p 80:80 -d webapp

14. Check docker is running on localhost via curl.

curl -i localhost
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 13
ETag: W/"d-9lxqrhD86L6inwvTBMrBS03gqRk"
Date: Fri, 15 Nov 2019 02:49:52 GMT
Connection: keep-alive

"Hello World"

Python

Create a simple script

We provide how to dockerize the simple python application. Before getting started this tutorial, Python3 and Docker need to be installed in advance.

1. Generate python script file.

touch main.py

2. Write the sample code.

def main():
  print("Hello World")

if __name__ == '__main__':
  main()

3. Test the script with Mersenne prime number.

python main.py
Hello World

Dockerize

4. Create Dockerfile.

touch Dockerfile

5. Open Dockerfile and edit the file like below.

FROM python:3

ADD main.py /

CMD [ "python", "./main.py" ]

6. Build your Dockerfile.

docker build -t hello-world .

7. Check the docker image.

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              214daa68ebbe        30 seconds ago      932MB

8. Run and test the docker image.

docker run -it hello-world
Hello World

Last updated