服务计算 | 应用容器化

Docker安装

Docker官网

  • 基于官方的 centos7 镜像搭建 golang 开发环境(可以利用 MAINTAINER 指定作者)
FROM centos:7
  • 安装开发环境:gcc 和 golang
RUN yum install -y gcc
RUN rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN yum install golang -y
  • 配置环境变量
vim /etc/systemd/system/docker.service.d/override.conf

[Service]
ExecStart=
#ExecStart=/usr/bin/docker daemon  -H fd:// --insecure-registry hub.03in.com:5002
ExecStart=/usr/bin/dockerd -s overlay2  --insecure-registry hub.03in.com:5002

  mkfs.ext4 /path/to/your/device # ext4 方式
  
  mkfs.xfs -n ftype=1 /path/to/your/device #注意这里带了 ftype=1
  
  # 例如我的机器
  mkfs.xfs -n ftype=1 /dev/vdc
  # 挂载磁盘到目录(临时 挂机后可能会消失)
  mount /dev/vdc /data
  # 开启挂载 写入 /etc/fstab
  vim /etc/fstab
  添加 
  /dev/vdc  /data                  xfs    defaults        1   2
  • 安装项目运行所需第三方库
RUN go get github.com/boltdb/bolt
RUN go get github.com/graphql-go/graphql
RUN go get -u github.com/go-sql-driver/mysql
  • 启动
# Start docker service
systemctl enable docker
systemctl start docker


# 重新加载设置  重启引擎
systemctl daemon-reload
systemctl restart docker
  • 拷贝项目代码进入容器
COPY . /root/gopath/src/BringMeFlyServer/
  • 编译代码
WORKDIR /root/gopath/src/BringMeFlyServer
RUN go build -o server.bin main.go
  • 指定容器入口
CMD /root/gopath/src/BringMeFlyServer/server.bin

Docker指令介绍

  • 查看版本
$ docker version
  • 运行镜像
~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
~]# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
32802c0cfa4d: Pull complete
da1315cffa03: Pull complete
fa83472a3562: Pull complete
f85999a86bef: Pull complete
Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49
Status: Downloaded newer image for ubuntu:latest
root@e2442527555f:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@e2442527555f:/# exit
exit
[root@centos-docker-ex ~]#
  • 显示本地镜像酷内容
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 93fd78260bd1 2 weeks ago 86.2 MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84 kB
  • 获得帮助
~]# docker --help
  • 显示运行中容器
~]# docker ps
CONTAINER ID IMAGE COMMAND …
  • 显示所有容器
~]# docker ps -a
CONTAINER ID IMAGE COMMAND STATUS NAMES
e2442527555f ubuntu "bash" Exited (0) frosty_goodall
627aa20c5232 hello-world "/hello" Exited (0) heuristic_franklin
  • 停止一个容器
$ docker stop bfd094233f96   
  • 删除一个容器
$ docker rm bfd094233f96   
  • 进入一个容器
$ docker attach d20f3dc6cd92  
  • 重启Docker服务
sudo service docker restart
docker pull 本机ip:5000/hello-world:1.10

应用容器化

  1. 拉取MySQL 镜像
  2. 远程仓库官方仓库: https://hub.docker.com/_/mysql/
     格式:https://registry/user/repo
     仓库中保存若干版本镜像,每个镜像对应一个tag,默认的tag 是lastest
    每个镜像有github 上Dockfile 生成
  3. MySQL容器构建文件Dockerfile
    https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile
  4. Docker compose 与多容器应用自动化部署
    下载:docker-compose(容器编排的原型)
    编写:stack.yml
    启动服务
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值