docker cmd for | docker |
---|---|
image |
pull build images rmi
|
container |
run ps rm inspect
|
保存container的改动为新的image | commit |
在host和container之间拷贝文件 | cp |
try 环境 centos7
yum install docker
service docker start
chkconfig docker on `#开机启动`
docker pull nginx
docker images `#所有镜像`
docker run -d -p 8080:80 --name nginx nginx `#左边是宿主机端口n,右边是镜像端口1`
docker ps `#查看运行的容器`
hello docker
docker run -d -p 8080:80 docker.io/nginx
docker cp index.html <containerID>://usr/share/nginx/html #显示html
docker stop <containerID>
docker run -d -p 8080:80 docker.io/nginx `#对容器的操作时临时的,回退到nginx首页了`
docker cp index.html <containerID>://usr/share/nginx/html #显示html
docker commit -m 'hello' <containerID> `#提交容器临时状态作成一个新的image`
docker commit -m 'hello' <containerID> nginx-hello `命名image为nginx-hello`
docker images `#image列表`
docker rmi <imageID> `#删除image`
docker ps -a `#所有容器`
docker rm <containerID1> <containerID2> <containerID..> `#删除容器`
Dockerfile1
FROM alpine:latest
#Docker特制的很小的Linux环境
MAINTAINER Kelvin
CMD echo 'hello dockerfile'
mkdir d1
cd d1
touch Dockerfile # 将👆的内容写入其中
docker build -t hello_dockerfile . # -t 创建tag为hello_dockerfile的image
docker images hello_dockerfile #5.53 MB
docker run hello_dockerfile #hello Dockerfile #运行image
Dokerfile2
FROM ubuntu
MAINTAINER Kelvin
RUN apt-get update
RUN apt-get install -y nginx
COPY index.html /var/www/html
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
EXPOSE 80
docker build -t kelvin/hello-d2 .
docker images kelvin/hello-d2 #173 MB
docker run -d -p 8080:80 kelvin/hello-d2
curl http://localhost:8080
镜像分层 镜像只读 共享 复用
[root@VM_0_8_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kelvin/hello-d2 latest 9ad56bf4acb3 25 minutes ago 173 MB
hello_dockerfile latest 97fee74b41eb About an hour ago 5.53 MB
nginx-hello latest fa32de7ce85d 2 hours ago 109 MB
docker.io/nginx latest bb776ce48575 2 days ago 109 MB
docker.io/alpine latest cdf98d1859c1 3 days ago 5.53 MB
docker.io/ubuntu latest 94e814e2efa8 4 weeks ago 88.9 MB
Dockerfile cmd
Dockerfile cmd | desc |
---|---|
FROM | base image |
RUN | 执行命令 |
ADD | 添加文件 |
COPY | 拷贝文件 |
CMD | 执行命令 |
EXPOSE | 暴露端口 |
WORKDIR | 指定路径 |
MAINTAINER | 维护者 |
ENV | 设定环境变量 |
ENTRYPOINT | 容器入口 |
USER | 指定用户 |
VOLUME | mount point |
Volume 提供
独立于容器之外的
持久化存储
docker run -d --name nginx -v /usr/share/nginx/html nginx
docker inspect nginx
"Mounts": [
{
"Type": "volume",
"Name": "d35f1b73fb5ab53f038f3c979b066339b38137cc301ba7710d51c8b369e02645",
"Source": "/var/lib/docker/volumes/d35f1b73fb5ab53f038f3c979b066339b38137cc301ba7710d51c8b369e02645/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
cd /var/lib/docker/volumes/ d35f1b73fb5ab53f038f3c979b066339b38137cc301ba7710d51c8b369e02645/_data
ls
50x.html index.html
echo "I'm Kelvin" > index.html
docker exec -it nginx /bin/bash
#进入容器
cat /usr/share/nginx/html/index.html
#结论host's Source = docker's Destination
- 将host的目录挂载到docker容器的目录
-v hostDIR : dockerDIR
docker run -v $PWD/code:/var/www/html nginx
docker run -d -p 8080:80 -v $PWD/html:/usr/share/nginx/html nginx
docker run -volumes-from ...
docker create -v $PWD/data:/var/mydata --name data_container ubuntu
docker run -it --volumes-from data_container ubuntu /bin/bash
#it 进入docker容器
mount
# /var/mydata就是从data_container容器中挂载过来的
/dev/vda1 on /var/mydata type ext3 (rw,noatime,data=ordered)
cd /var/mydata/
ls
touch whatever.txt
ctrl+D
#退出docker容器
cd docker/vol3/data/
ls
# whatever.txt一个文件可被多个容器挂载,实现
数据共享
registry 镜像仓库
docker search whalesay
docker pull docker.io/docker/whalesay
docker run docker.io/docker/whalesay cowsay hello,KelvinFan
docker tag docker.io/docker/whalesay kelvinfan/whalesay
#image格式loginName/xxx
docker push kelvinfan/whalesay
# 需要注册登录 Docker Hub
docker login
docker run docker.io/docker/whalesay cowsay hello,Kelvin
______________
< hello,KelvinFan >
--------------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/
docker-compose 多容器app
curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose
chmod a+x /usr/local/bin/docker-compose
#所有人可执行docker-compose --version
ghost案例 docker-compose
person-->nginx-->ghost app-->mysql
mkdir ghost
cd ghost
mkdir ghost nginx data
cd ghost
touch Dockerfile
#ghost/ghost
vim Dockerfile
# ghost/ghost/Dockerfile
FROM ghost
#COPY ./config.js /var/lib/ghost/config.js
COPY ./config.js /var/lib/ghost/content/config.js #新版本目录变更了
EXPOSE 2368
# CMD ["npm","start","--production"] #新版本已默认提供
vim config.js
#ghost/ghost/config.js
var path = require('path'),
config;
config = {
production: {
url: 'http://kelvin.vip',
mail: {},
database: {
client: 'mysql',
connection: {
host: 'db',
user: 'ghost',
password: 'ghost',
database: 'ghost',
port: '3306',
charset: 'utf8'
},
debug: false
},
paths: {
contentPath: path.join(process.env.GHOST_CONTENT, '/')
},
server: {
host: '0.0.0.0',
port: '2368'
}
}
};
module.exports = config;
cd ../nginx
touch Dockerfile
vim Dockerfile
# ghost/nginx/Dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
vim nginx.conf
# ghost/nginx/nginx.conf
worker_processes 4;
events {worker_connections 1024;}
http {
server {
listen 80;
location / {
proxy_pass http://ghost-app:2368;
}
}
}
cd ..
vim docker-compose.yml
# ghost/docker-compose.yml
version: '2'
networks:
ghost:
services:
ghost-app:
build: ghost
networks:
- ghost
depends_on:
- db
ports:
- "2368:2368"∂
nginx:
build: nginx
networks:
- ghost
depends_on:
- ghost-app
ports:
- "80:80"
db:
image: "mysql:5.7.15"
networks:
- ghost
environment:
MYSQL_ROOT_PASSWORD: mysqlroot
MYSQL_USER: ghost
MYSQL_PASSWORD: ghost
volumes:
- $PWD/data:/var/lib/mysql
ports:
- "3306:3306"
docker-compose up -d
// #配置出错,修改
docker-compose stop
docker-compose rm
docker-compose build
#显示构建image
docker-compose up -d
网友评论