我的docker全套流程例子
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn
摘要:
- 下文是自己从搭建docker到docker里安装mysql到push的一遍大概过程
- docker的安装直接引用官方文档,英文比较简单,所以没有多加翻译
目前红帽RHEL系统下面安装docker可以有两种方式:一种是使用curl获得docker的安装脚本进行安装,还有一种是使用yum包管理器来安装docker。
Install on CentOS
Docker runs on CentOS 7.X. An installation on other binary compatible EL7 distributions such as Scientific Linux might succeed, but Docker does not test or support Docker on these distributions.
This page instructs you to install using Docker-managed release packages and installation mechanisms. Using these packages ensures you get the latest release of Docker. If you wish to install using CentOS-managed packages, consult your CentOS documentation.
Prerequisites
Docker requires a 64-bit installation regardless of your CentOS version. Also, your kernel must be 3.10 at minimum, which CentOS 7 runs.
To check your current kernel version, open a terminal and use uname -r to display your kernel version:
$ uname -r
3.10.-.el7.x86_64
Finally, it is recommended that you fully update your system. Please keep in mind that your system should be fully patched to fix any potential kernel bugs. Any reported kernel bugs may have already been fixed on the latest kernel packages.
Install
There are two ways to install Docker Engine. You can install using the yum package manager. Or you can use curl with the get.docker.com site. This second method runs an installation script which also installs via the yum package manager.
Install with yum
Log into your machine as a user with sudo or root privileges.
- Make sure your existing yum packages are up-to-date.
$ yum update
- Add the yum repo.
$ tee /etc/yum.repos.d/docker.repo <<EOF
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=
gpgcheck=
gpgkey=https://yum.dockerproject.org/gpg
EOF
- Install the Docker package.
$ sudo yum install docker-engine
- Start the Docker daemon.
$ sudo service docker start
- Verify docker is installed correctly by running a test image in a container.
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1..1cf5daeb82aab55838d
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:
. The Docker client contacted the Docker daemon.
. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(Assuming it was not already locally available.)
. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
. 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 For more examples and ideas, visit:
http://docs.docker.com/userguide/
Install with the script
Log into your machine as a user with sudo or root privileges.
- Make sure your existing yum packages are up-to-date.
$ sudo yum update
- Run the Docker installation script.
$ curl -fsSL https://get.docker.com/ | sh
# This script adds the docker.repo repository and installs Docker.
- Start the Docker daemon.
$ sudo service docker start
# Verify docker is installed correctly by running a test image in a container.
$ sudo docker run hello-world
Create a docker group
The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user.
To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
Warning: The docker group is equivalent to the root user; For details on how this impacts security in your system, see Docker Daemon Attack Surface for details.
To create the docker group and add your user:
- Log into Centos as a user with sudo privileges.
- Create the docker group.
groupadd docker
- Add your user to docker group.
usermod -aG docker your_username
Log out and log back in.
This ensures your user is running with the correct permissions.Verify your work by running docker without sudo.
$ docker run hello-world
Start the docker daemon at boot
- To ensure Docker starts when you boot your system, do the following:
$ sudo chkconfig docker on
If you need to add an HTTP Proxy, set a different directory or partition for the Docker runtime files, or make other customizations, read our Systemd article to learn how tocustomize your Systemd Docker daemon options.
Uninstall
You can uninstall the Docker software with yum.
- List the package you have installed.
$ yum list installed | grep docker
docker-engine.x86_64 1.7.-.el7 @/docker-engine-1.7.-.el7.x86_64.rpm
- Remove the package.
$ sudo yum -y remove docker-engine.x86_64
This command does not remove images, containers, volumes, or user-created configuration files on your host.
- To delete all images, containers, and volumes, run the following command:
$ rm -rf /var/lib/docker
Locate and delete any user-created configuration files.
Docker的使用
下载启动镜像安装应用
- 查看已有的镜像
docker images
- 搜索镜像,假如我们要下载centos6.6,这里只列出部分搜索结果来
[root@galen yum.repos.d]# docker search centos:6.6
NAME DESCRIPTION STARS OFFICIAL AUTOMATED anarh/centos6. Docker image for centos6. [OK]
eliezio/centos6.-devtoolset2-gtest Docker image based on Centos 6.6 suitable ... [OK]
chrisgeorge/centos6.-py2. CentOS 6.6 with Python 2.6 [OK]
mystique/hadoopbase Hadoop base image - Centos 6.6 Updated to ... [OK]
incu6us/centos6.-with-nginx Wav server for FreeCall [OK]
- pull镜像,上面搜索到很多,我们选择一个下载
[root@galen yum.repos.d]# docker pull anarh/centos6.
Using default tag: latest
latest: Pulling from anarh/centos6.
a3ed95caeb02: Pull complete
75fcb2d165e6: Pull complete
Digest: sha256:c2e5b2ff2b471ce747952c3887595f34410b390c9e36ba0823fbbc8b9a54c637
Status: Downloaded newer image for anarh/centos6.:latest
- 这时在查看镜像发现就有了
[root@galen yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
anarh/centos6. latest eeb98e74a7bd months ago 202.6 MB
- 运行该镜像
[root@galen yum.repos.d]# docker run -t -i anarh/centos6. /bin/bash
[root@14aa06d651c6 /]#
- 然后就可以在里面像正常linux一样安装自己的软件了,这里我安装mysql,步骤省略
提交容器
我们在里面编译安装完mysql后,我们就提交生成镜像,以后就可以直接启动引用该镜像了
exit
后通过docker ps -a
找到该容器names对应的容器ID- 运行
commit
命令来提交
docker commit -m "install mysql5.6.28 from centos6.6" -a "galen" 14aa06d651c6 galen/centos6.-mysql5.
其中,-m参数用来来指定提交的说明信息;-a可以指定用户信息的;14aa06d651c6代表你的容器的id;galen/centos6.6-mysql5.6指定目标镜像的用户名、仓库名和 tag 信息。创建成功后会返回这个镜像的 ID 信息。注意的是,你一定要将galen改为你自己的用户名。因为下文还会用到此用户名!
[root@galen ~]# docker commit -m "install mysql5.6.28 from centos6.6" -a "galen" 14aa06d651c6 galen/centos6.-mysql5.
sha256:8fa5dde39e421b9ade3dae485f9276bd9c5b64ef5f38995afcdd3a3fb6df40f7
- 这是我们再次使用docker images命令就会发现此时多出了一个我们刚刚创建的镜像,此时我们就可以docker run加上你的参数运行改容器了:
[root@galen ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
galen/centos6.-mysql5. latest 8fa5dde39e42 seconds ago 3.654 GB
hello-world latest c54a2cc56cbb days ago 1.848 kB
anarh/centos6. latest eeb98e74a7bd months ago 202.6 MB
存储镜像
- 登录docker hub
我们刚刚已经创建了自己的第一个镜像,尽管它很简单,但这已经非常棒了,现在,我们希望它能够被更多的人使用到,此时,我们就需要将这个镜像上传到镜像仓库,Docker的官方Docker Hub应该是目前最大的Docker镜像中心,所以,我们就将我们的镜像上传到Docker Hub。
首先,我们需要成为Docker Hub的用户,前往https://hub.docker.com/进行注册。需要注意的是,为了方便下面的操作,你需要将你的用户名设为和我刚刚在上文提到的自定义用户名相同,例如我的刚刚将镜像的名字命名为是galen/centos6.6-mysql5.6,所以我的用户名为galen、注册完成后记住用户名、密码、邮箱。
login默认是用来登陆Docker Hub的,因此,输入如下命令来尝试登陆Docker Hub:docker login
此时,就会输出交互,让我们输入Username、Password、Email,成功输入我们刚才注册的信息后就会返回Login Success提示:
[root@galen ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: galen
Password:
Login Succeeded
注意:要把防火墙关闭,否则登不上
- 登录后运行命令
docker push galen/centos6.6-mysql5.6
,把镜像push到docker hub上
[root@galen ~]# docker push galen/centos6.-mysql5.
The push refers to a repository [docker.io/galen/centos6.-mysql5.]
57b8f026c3f7: Waiting
5f70bf18a086: Image already pushed, skipping
2a45600af30a: Pushing [====================> ] 83.93 MB/202.6 MB
- 上传完后你登录docker hub你的账号后,在里面就能看到你上传的东西了
创建私有仓库
- 下载一个registry
docker pull registry
# ps:此处已经不想吐槽,因为GFW的关系,我不知道重试了多少次才下载成功。
- 启动registry容器
docker run -d -p : -v /opt/data/registry:/tmp/registry registry
# -p端口映射, 默认情况下,仓库会被创建在容器的 /tmp/registry 下。可以通过 -v 参数来将镜像文件存放在本地的指定路径。 例如下面的例子将上传的镜像放到 /opt/data/registry 目录。
- 把自己已有的容器tag的一个标识,相当于复制重命名一个
docker tag galen/centos6.-mysql5. 127.0.0.1:/mysql
# 此处我命名为这个127.0.0.:/mysql,docker images时就会看到一个127.0.0.:/mysql镜像
- 把刚才tag的容器上传
docker push 127.0.0.1:/mysql
- 查看是否有该镜像了
curl http://192.168.10.146:5000/v1/search
{"num_results": , "query": "", "results": [{"description": "", "name": "library/mysql"}]}
- 在其他服务器上下载刚才的镜像
docker pull 192.168.10.146:/mysql
# 此处又是一个坑,当你pull的时候会默认用https,所以无法现在。
# 网上很多说法是在/etc/default/docker下加上这句DOCKER_OPTS="$DOCKER_OPTS --insecure-registry=192.168.10.146:5000"但我不成功
# 我是在/lib/systemd/system/docker.service文件里的ExecStart这个参数后面加上--insecure-registry 192.168.10.146:5000如下
# ExecStart=/usr/bin/docker daemon -H fd:// --insecure-registry 192.168.10.146:5000
注意:如果退出去了当前容器,比如
exit
,在重新执行上上述命令docker run -t -i anarh/centos6.6 /bin/bash
是不行的,重新执行上述命令后实际是新启动一个容器,这时是没有你原来的操作过程的的,比如你上面安装的mysql。
如何找到你上述那个容器呢?
1、退出来后执行下面命令会把所有容器列出来 docker ps -a
2、如果当初启动的时候没有指定一个可认识的name,如docker run -t -i –name xxx anarh/centos6.6 /bin/bash 只有一个一个的查看日志,看你的安装步骤了
docker logs 容器id
[root@galen ~]# docker logs 6409d2c066b4
[root@6409d2c066b4 /]#
[root@6409d2c066b4 /]# netstat | grep 22
[root@6409d2c066b4 /]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
[root@6409d2c066b4 /]# yum install wget
3、找到后立即把那个名字重名以后,方便以后好找。原来你没有指定name,它会自己随机一个,如上图的name列
docker rename 旧名字 新名字
docker rename drunk_goldberg mysql
4、如果要重新加新的参数启动该容器,比如加上-p端口映射,这时只能,先提交成镜像,然后重新启动,详见其它步骤
我的docker全套流程例子的更多相关文章
- Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合例子(附完整的请假流程例子,jbpm基础,常见问题解决)
Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子). 1.jbpm4.4 测试环境搭建 2.Jbpm4.4+hibernat ...
- CentOS Linux搭建独立SVN Server全套流程(修改svn仓库地址、服务启动等)
CentOS Linux搭建独立SVN Server全套流程(修改svn仓库地址.服务启动等) 原 一事能狂便少年 发布于 2016/12/27 11:16 字数 1113 阅读 1.3K 收藏 0 ...
- Dockerfile与Docker构建流程解读
摘要 本文主要讨论了对docker build的源码流程进行了梳理和解读,并分享了在制作Dockerfile过程中的一些实践经验,包括如何调试.优化和build中的一些要点.另外,还针对现有Docke ...
- docker工作流程
Docker提供一种方法在容器中运行安全隔离的应用程序,应用程序与所有依赖项和库一起打包在容器中.因为你的应用程序总是可以使用它在构建镜像中期望的环境运行,测试和部署比以往任何时候都更简单,因为你的构 ...
- 运行docker大致流程
平时部署测试环境使用jenkins将代码打包成docker镜像部署在rancher中,闲下来研究了一下docker的大致流程,自己画了一个流程图
- Docker 容器备份例子
# 在 node1 执行 nginx 程序,挂载本地的目录 docker pull nginx:stable-alpine mkdir /data/html echo "hello worl ...
- Linux Shell流程例子
#!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...
- 自动化测试全套流程(一)-搭建Jenkins环境
前提 既然要做自动化测试,那我们就做得彻底一些,将整套系统部署在Linux服务器上,在搭建Jenkins环境之前,我已经通过VirtualBox安装了一个CentOS的服务器,搭建Jenkins是基于 ...
- CentOS Linux搭建独立SVN Server全套流程(转)
环境为centos6.3 1.首先 看看机器上安装了svn了没有 rpm -qa |grep svn 2.如果没有安装 执行 yum -y install subversion 3.安装好了之后 新建 ...
随机推荐
- 织梦dedeCMS留言薄
dedeCMS留言薄模塊名爲guestbook, 留言薄模板:/templets/plus/guestbook.htm; 留言回覆模板: 管理員回覆調用/templets/plus/guestbook ...
- JavaScript之BST
自己尝试用js实现了数据结构的二叉查找树. // node function Node(data) { this.data = data; this.lc = null; this.rc = null ...
- 数据库复习总结(2)-SQLServer的管理
1.需要安装SQLServer2008或者SQLServer2012,若要使用SQLServer管理工具进行开发还要安装SQL Server Management Studio,还可以使用Visual ...
- C#如何释放已经加载的图片
使用Image.FromFile取磁盘上的图片时,这个方法会锁定图片文件,而且会导致内存占用增大, 有几种方法解决:一:将Image类转换成Bitmap类System.Drawing.Image im ...
- 汇编编译器(masm.exe)对jmp的相关处理
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- [转]如何使用PHP实现javascript的escape和unescape函数
前端开发工程师都知道javascript有编码函数escape()和对应的解码函数unescape(),而php中只有个urlencode和urldecode,这个编码和解码函数对encodeURI和 ...
- 输入和输出--javase中的路径
就目前为止, javase中经常用到路径来读取一个资源文件的所有情况都已经整理在博客里面了,这里做一个统一的整理: 1,IO流来读取一个文件,比如说new FileInputStream(" ...
- MYSQL 5.7 修改密码、登录问题
mysql5.7 关于密码问题 报错: ERROR 1862 (HY000): Your password has expired. To log in you must change it usin ...
- js中常用的方法(数组篇)
1.replace(),根据释义,即为代替,用法为: stringObject.replace(regexp/substr,replacement)括号内前者是待匹配字符串,并用后者代替这个字符串.例 ...
- yum 安装zabbix2.4 /3.2.4
yum 安装zabbix2.4 首先zabbix需要的环境是web环境,默认的是lamp或者lnmp,讲道理,要是使用tomcat也是可以的,不过实验没有成功 系统:centos6.8_x64 附yu ...