Docker入门

2018-10-17  本文已影响24人  Hey_Shaw

Linux安装Docker

安装docker本身

$ sudo wget -qO- https://get.docker.com/ | sh
-q:让wget输出不要过多
-O-:把wget的输出直接输出到标准输出,而不是输出到文件,管道技术交给sh执行

运行非root用户运行docker

$ sudo usermod -aG docker imooc
把用户加到docker用户组中,使指定用户获取执行docker权限

启动docker服务

$ systemctl start docker.service

设置开机启动

$ sudo systemctl enable docker

docker架构介绍

实战

$ docker run ubuntu echo hello docker

运行nginx

$ docker run -p 8080:80 -d nginx        #将docker容器的80端口映射到宿主机8080端口

访问:宿主机ip:8080

定制化自己的nginx页面

$ cat index.html
<html>
  <h1>Docker is fun!</h1>
</html>

$ docker ps  # 查看运行镜像,获取镜像ID

$ docker cp index.html 653c6d248e5a://usr/share/nginx/html

$ docker stop 653c6d248e5a  # 停止运行的镜像

$ docker run -p 8080:80 -d nginx  # 原来设定的网页被还原

$ docker cp index.html 58cf1b4ef86b://usr/share/nginx/html

$ docker commit -m 'fun' 58cf1b4ef86b nginx-fun   # 保存自定义,生成新的镜像nginx-fun

$ docker rmi 1696742493b7  # 删除镜像

$ docker ps -a # 显示所有运行过的镜像

$ docker rm 5e23511ac629 653c6d248e5a   # 删除运行过的镜像历史

命令小结

命令 用途
docker pull 获取image
docker build 创建image
docker images 列出image
docker run 运行container
docker ps 列出container
docker rm 删除container
docker rmi 删除image
docker cp 在host和container之间拷贝文件
docker commit 保存改动为新的image

Dockerfile

touch Dockerfile # 新建Dockerfile

FROM alpine:latest
MAINTAINER jack
CMD echo "Hello Docker!"

构建新的image

$ docker build -t hello_docker .

Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM alpine:latest
latest: Pulling from library/alpine
4fe2ade4980c: Pull complete 
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:latest
 ---> 196d12cf6ab1
Step 2/3 : MAINTAINER jack
 ---> Running in 0bf9be49775b
Removing intermediate container 0bf9be49775b
 ---> 2752ef8ca6a2
Step 3/3 : CMD echo "Hello Docker!"
 ---> Running in 5aa61e2dcc24
Removing intermediate container 5aa61e2dcc24
 ---> 9feb05314694
Successfully built 9feb05314694
Successfully tagged hello_docker:latest

运行镜像

$ docker run hello_docker
Hello Docker!

Dockerfile实战

FROM ubuntu
MAINTAINER jack
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list  # 替换archive.ubuntu.com为mirrors.ustc.edu.cn,加速
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

自定义新建index.html
执行下列命令

docker build -t jack/hello_nginx .     # 构建镜像
docker run -d -p 80:80 jack/hello_nginx   # 启动镜像
docker stop jack/hello_nginx  # 停止镜像

Dockerfile语法

命令 用途
FROM base image
RUN 执行命令
ADD 添加文件
COPY 拷贝文件
CMD 执行命令
EXPOSE 暴露端口
WORKDIR 指定路径
MAINTAINER 维护者
ENV 设定环境变量
ENTRYPOINT 容器入口
USER 指定用户
VOLUME mount point

注:Dockerfile中的每一行都产生一个新层

Volume:提供独立于容器之外的持久化存储

$ docker run -d --name nginx -v /usr/share/nginx/html nginx
$ docker inspect nginx   # 容器所有信息

...
"Mounts": [   # 数据卷
            {
                "Type": "volume",
                "Name": "c6670ae27f24945d3f19267db45e730def339ba36eae1028fa3a94e116eab20e",
                "Source": "/var/lib/docker/volumes/c6670ae27f24945d3f19267db45e730def339ba36eae1028fa3a94e116eab20e/_data",
                "Destination": "/usr/share/nginx/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
...

$ docker exec -it nginx /bin/bash   # 进入容器内部
$ cd /usr/share/nginx/html/

第二种数据卷:docker run -v $PWD/code:/var/www/html nginx

$ docker run -p 80:80 -d -v $PWD/html:/usr/share/nginx/html nginx
$ vim index.html   # 修改即可生效

第三种Volume: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

Registry(镜像仓库)

术语

English 中文
host 宿主机
image 镜像
container 容器
registry 仓库
daemon 守护程序
client 客户端
$ docker search whalesay
$ docker pull whalesay
$ docker push myname/whalesay

compose介绍(多容器app)

Linux下安装docker-compose

$ 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

github中提示安装方式的写法为:

$ curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose

docker-compose实战

docker-compose.yaml

ghost-app:
  build: ghost
  depends_on:
    - db
  ports:
    - "2368:2368"
nginx:
  build: nginx
  ports:
    - "80:80"
  depends_on:
    - ghost-app
db:
  image: "mysql:5.7.15"
实战部分

目录:$PWD/ghost/下:

$ mkdir ghost
$ mkdir data
$ mkdir nginx

$ cd ghost
$ vim Dockerfile

FROM ghost
COPY ./config.js /var/lib/ghost/config.js
EXPOSE 2368
CMD ["npm","start","--production"]

$ vim config.js

var path = require('path'),
config;

config = {
    production: {
        url: 'http://mytestblog.com',
        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

$ vim Dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

$ vim nginx.conf

worker processes 4;
events {worker connections 1024;}
http {
    server {
        listen 80;
    location / {
        proxy pass http://ghost-app:2368;
    }
    }
}

cd ./ghost

$ vim 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/libmysql
    ports: 
      - "3306:3306"

进入 $PWD/ghost/ 执行

$ docker-compose up -d        # 构建服务
$ docker-compose stop          # 停止服务
上一篇下一篇

猜你喜欢

热点阅读