转~Docker极简教程「高级」

2018-12-01  本文已影响0人  Chting

作者:像风一样;

来源:后端技术精选

1.Registry介绍

Registry 是镜像仓库,我们可以从镜像仓库中拉取一些镜像到本地,也可以提交镜像到仓库。

一些术语:

与registry仓库的交互:

查找镜像

docker search whalesay

拖取镜像

docker pull whalesay

推送镜像

docker push myname/whalesay

国内的docker镜像仓库:

daoclou

时速云

aliyun

2.Registry实战

1.查找镜像

STARS可以理解为点赞数,默认是按照这个排序的。

2.拉取镜像

3.查看镜像

可以看到,REPOSITORY是镜像名,TAG是默认的latest,正常情况是版本号,这两个比较重要。

CREATED是创建时间,SIZE是占用空间的大小。

4.运行镜像

5.标记镜像

使用docker tag命令标记本地镜像,将其归入某一仓库。

docker tag docker/whalesay myhaleasy:tag

6.上传镜像

可以使用docker push命令上传本地镜像到仓库,但是需要注册登录。

注册登录地址:https://hub.docker.com

执行登录命令,输入用户名与密码即可。

docker login

上传镜像前还需要在Docker官网个人中心创建该镜像。

3.Compose多容器应用

Compose 是一个用户定义和运行多个容器的 Docker 应用程序。在 Compose 中你可以使用 YAML 文件来配置你的应用服务。然后,只需要一个简单的命令,就可以创建并启动你配置的所有服务。

docker-compose 安装

1.Mac/Windows:

安装docker的时候附带安装了。

2.Linux:

curl https://github.com/docker/compose

Linux安装

执行命令

curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose

该命令会下载文件到(>)/usr/local/bin/docker-compose,在下载地址中使用$(uname -s)是获取 unmae -s 命令的输出加到路径中。

下载完成后,可以设置该目录的权限为777,意为所有人都可以读写该目录,最后使用 docker-compose --version 检查是否安装成功。

4.Compose搭建博客网站

接下来我们使用docker-compose搭建一个含nginx+ghost+db的博客网站。

1.准备

首先创建如下目录结构:

ghost

- ghost

- Dockfile

- config.js

- nginx

- nginx.conf

- Dockfile

- data

- docker-compose.yml

每个文件的具体内容有:

ghost/ghost/Dockfile

过时的配置(不使用)

FROM ghost

COPY ./config.js /var/lib/ghost/config.js

EXPOSE 2368

CMD ["npm","start","--production"]

最新版的官方镜像, 把配置文件改到/var/lib/ghost/content/ ,然后注释掉了 CMD["npm","start","--production"] 即可。

FROM ghost

COPY ./config.js /var/lib/ghost/content/

EXPOSE 2368

ghost/ghost/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;

ghost/nginx/Dockerfile

FROM nginx

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

ghost/nginx/nginx.conf

worker_processes 4;

events {worker_connections 1024;}

http {

server {

listen 80;

location / {

proxy_pass http://ghost-app:2368;

}

}

}

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"

此处数据库相关的配置要和config.js中的配置(user、password)保持一致。

一旦我们为一个服务指定了一个名字(db),它就可以被其它服务所解析。

nginx 中proxy_pass 设置成 http://ghost-app:2368 也是因为在docker-compose.yml 指定了服务是ghost-app。

2.启动

将所有容器启动,并以daemon的方式后台运行。第一次不需要build,因为会自动创建镜像。

docke-compose up -d

3.查看应用

docker-compose ps

因为文件内容输入有误,导致启动未成功,修改文件后还需要停止、删除掉容器再重新构建才行。

4.停止(+)

停止所有容器

docker-compose stop

5.删除(+)

删除时需要输入y手动确认

docker-compose rm

6.构建(+)

非首次运行就需要手动创建镜像了

docker-compose build

最后,在ghost目录下再次启动!

7.测试

访问域名进入搭建的博客网站,可以注册账号发布博客。

关于ghost的使用这里不做过多介绍。

5.docker-compose.yml常用命令

6.Docker资源链接

Docker官方英文资源

docker官网:http://www.docker.com

Docker中文资源

Docker中文网站:https://www.docker-cn.com/Docker安装手册:https://docs.docker-cn.com/engine/installation/

Docker 国内镜像

网易加速器:http://hub-mirror.c.163.com官方中国加速器:https://registry.docker-cn.comustc的镜像:https://docker.mirrors.ustc.edu.cndaocloud:https://www.daocloud.io/mirror#accelerator-doc(注册后使用)

上一篇 下一篇

猜你喜欢

热点阅读