1、安装(参考https://docs.docker.com/install/linux/docker-ce/ubuntu/

1)Uninstall old versions

sudo apt-get remove docker docker-engine docker.io containerd runc

2)Install using the repository

a.Update the apt package index:

sudo apt-get update

b.Install packages to allow apt to use a repository over HTTPS:

sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

c.Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

sudo apt-key fingerprint 0EBFCD88

d. set up the stable repository

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

3)INSTALL DOCKER ENGINE - COMMUNITY

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo docker run hello-world

2、创建一个简单的Node.js应用

1)编写app.js

const http = require('http');
const os = require('os');
console.log("Kubia server starting...");
var handler = function(request, response) {
  console.log("Received request from " + request.connection.remoteAddress);
  response.writeHead(200);
        response.end("You've hit " + os.hostname() + "\n");
};
var www = http.createServer(handler);
www.listen(8080);

2)在app.js同目录下创建Dockerfile

FROM node:7
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]

3)构建容器镜像

sudo docker build -t kubia .

4)列出本地存储的镜像

sudo docker images

5)运行容器镜像

sudo docker run --name kubia-container -p 8080:8080 -d kubia

6)访问应用

curl localhost:8080

7)列出所有运行中的容器

sudo docker ps

8)获取容器更多信息

sudo docker inspect kubia-container

9)在容器内部运行Shell

sudo docker exec -it kubia-container bash

10)退出容器

exit

11)停止和删除容器

sudo docker stop kubia-container
sudo docker rm kubia-container

12)向镜像仓库推送镜像

a.注册Docker Hub ID

b.给镜像添加额外标签

sudo docker tag kubia 使用自己的Docker Hub ID/kubia

c.向Docker Hub推送镜像

sudo docker push 使用自己的Docker Hub ID/kubia
11-20 02:30