ubuntu docker环境配置 https://blog.csdn.net/fareast_mzh/article/details/88820445
代码以及命令行下载
git clone git://github.com/luksa/kubernetes-in-action
Once Docker is installed, you need to start the Docker daemon. Most Linux distributions use systemctl
to start services. If you do not have systemctl
, use the service
command.
systemctl
:
$ sudo systemctl start docker
service
:
$ sudo service docker start
docker run busybox echo "Hello world"
* kubernetes代码下载
git clone https://github.com/kubernetes/kubernetes
* 创建一个简单的node.js docker应用
app.js
const http = require('http');
const os = require('os');
console.log("Kubia server starting...");
var handler = function(request, response) {
console.log("Received require from " + request.connection.remoteAddress);
response.writeHead(200);
response.end("You'v hit " + os.hostname() + "\n");
}
var www = http.createServer(handler);
www.listen(8080);
* 直接在主机上运行
node app.js > ./local.log 2>&1 &
curl http://localhost:8080/
You'v hit chenhuimingdeMacBook-Pro.local
fg %1
Ctrl + C kill process
* Dockerfile
FROM node:7
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]
L1 使用的是node镜像的tag 7版本
L2 把app.js文件从本地文件夹添加到镜像的根目录, 保持app.js这个文件名
L3 定义了当镜像被运行时需要被执行的命令 node app.js
* 构建容器镜像
$ tree kubia/
kubia/
|-- Dockerfile
|-- Dockerfile~
|-- app.js
`-- local.log
0 directories, 4 files
cd kubia
docker build -t kubia .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
启动docker服务, 再次执行 docker build -t kubia .
# 以下展示镜像构建的过程。
# 用户告诉Docker需要机遇当前目录 . 构建一个叫kubia的镜像, Docker会在目录中寻找Dockerfile,
# 然后基于其中的指令构建镜像
ending build context to Docker daemon 5.12kB
Step 1/3 : FROM node:7
Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
需要Shadowsocks,开启global mode.
* 列出本地存储的镜像
docker images
* 运行容器镜像
docker run --name kubia-container -p 8080:8080 -d kubia
# 基于kubia镜像创建新容器named kubia-container
# -d --deamon
# 本机上的8080端口会映射到容器内的8080端口(-p 8080:8080)
# 所以可以通过http://localhost:8080访问这个应用
095e11426a7d2b3fe108e3e218a40313318ca0a102963ac6715fafeb733f3bb0
$ curl http://localhost:8080
You'v hit 095e11426a7d
# 主机名现在不是宿主的主机名, 而是container ID
$ hostname
chenhuimingdeMacBook-Pro.local
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
095e11426a7d kubia "node app.js" 2 minutes ago Up 2 minutes 0.0.0.0:8080->8080/tcp kubia-container
$ docker inspect kubia-container
# 查看container底层信息的长json
* 在已有容器内部运行shell
$ docker exec -it kubia-container bash
root@095e11426a7d:/# cat app.js
const http = require('http');
const os = require('os');
console.log("Kubia server starting...");
var handler = function(request, response) {
console.log("Received require from " + request.connection.remoteAddress);
response.writeHead(200);
response.end("You'v hhit " + os.hostname() + "\n");
}
var www = http.createServer(handler);
www.listen(8080);
root@095e11426a7d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 1.2 614432 26340 ? Ssl 06:35 0:00 node app.js
root 28 0.4 0.1 20244 3232 pts/0 Ss 06:42 0:00 bash
root 34 0.0 0.1 17500 2064 pts/0 R+ 06:42 0:00 ps aux
容器内的进程仍然运行在主机操作系统上 (Linux上可以查看,Mac/Windows需要登录到Docker守护进程的vm中查看)
sudo ps aux | grep app.js
Password:
Mch 14789 0.0 0.0 4277236 796 s002 S+ 2:46PM 0:00.00 grep app.js
* 停止和删除容器
$ docker stop kubia-container
$ docker rm kubia-container