我的docker全套流程例⼦
本⽂来⾃我的github pages博客galengao.github.io/ 即www.gaohuirong
摘要:
下⽂是⾃⼰从搭建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.0-229.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 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 /pos.po <<EOF
[dockerrepo]
name=Docker Repository
baseurl=/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=/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.7.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:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(Assuming it was not already locally available.)
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
For more examples and ideas, visit:
docs.docker/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 get.docker/ | sh
# This script adds po 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.mysql社区版国内镜像下载
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.1-1.el7 @/docker-engine-1.7.1-1.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@pos.d]# docker search centos:6.6
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
anarh/centos6.6 Docker image for centos6.60 [OK]
eliezio/centos6.6-devtoolset2-gtest Docker image based on Centos 6.6 suitable ... 0 [OK]
chrisgeorge/centos6.6-py2.6 CentOS 6.6 with Python 2.60 [OK]
mystique/hadoopbase Hadoop base image - Centos 6.6 Updated to ... 0 [OK]
incu6us/centos6.6-with-nginx Wav server for FreeCall 0 [OK]
pull镜像,上⾯搜索到很多,我们选择⼀个下载
[root@pos.d]# docker pull anarh/centos6.6
Using default tag: latest
latest: Pulling from anarh/centos6.6
a3ed95caeb02: Pull complete
75fcb2d165e6: Pull complete
Digest: sha256:c2e5b2ff2b471ce747952c3887595f34410b390c9e36ba0823fbbc8b9a54c637
Status: Downloaded newer image for anarh/centos6.6:latest
这时在查看镜像发现就有了
[root@pos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
anarh/centos6.6 latest eeb98e74a7bd 10 months ago 202.6 MB
运⾏该镜像
[root@pos.d]# docker run -t -i anarh/centos6.6 /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.6-mysql5.6
其中,-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.6-mysql5.6
sha256:8fa5dde39e421b9ade3dae485f9276bd9c5b64ef5f38995afcdd3a3fb6df40f7
这是我们再次使⽤docker images命令就会发现此时多出了⼀个我们刚刚创建的镜像,此时我们就可以docker run加上你的参数运⾏改容器了:
[root@galen ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
galen/centos6.6-mysql5.6 latest 8fa5dde39e42 57 seconds ago 3.654 GB
hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
anarh/centos6.6 latest eeb98e74a7bd 10 months ago 202.6 MB
存储镜像
登录docker hub
我们刚刚已经创建了⾃⼰的第⼀个镜像,尽管它很简单,但这已经⾮常棒了,现在,我们希望它能够被更多的⼈使⽤到,此时,我们就需要将这个镜像上传到镜像仓库,Docker的官⽅Docker Hub应该是⽬前最⼤的Docker镜像中⼼,所以,我们就将我们的镜像上传到Docker Hub。
⾸先,我们需要成为Docker Hub的⽤户,前往hub.docker/进⾏注册。需要注意的是,为了⽅便下⾯的操作,你需要将你的⽤户名设为和我刚刚在上⽂提到的⾃定义⽤户名相同,例如我的刚刚将镜像的名字命名为是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 hub.docker to create one.
Username: galen
Password:
Login Succeeded
注意:要把防⽕墙关闭,否则登不上
登录后运⾏命令docker push galen/centos6.6-mysql5.6,把镜像push到docker hub上
[root@galen ~]# docker push galen/centos6.6-mysql5.6
The push refers to a repository [docker.io/galen/centos6.6-mysql5.6]
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 5000:5000 -v /opt/data/registry:/tmp/registry registry
# -p端⼝映射,默认情况下,仓库会被创建在容器的 /tmp/registry 下。可以通过 -v 参数来将镜像⽂件存放在本地的指定路径。例如下⾯的例⼦将上传的镜像放到 /opt/data/registry ⽬录。
把⾃⼰已有的容器tag的⼀个标识,相当于复制重命名⼀个
docker tag galen/centos6.6-mysql5.6127.0.0.1:5000/mysql
# 此处我命名为这个127.0.0.1:5000/mysql,docker images时就会看到⼀个127.0.0.1:5000/mysql镜像
把刚才tag的容器上传
docker push 127.0.0.1:5000/mysql
查看是否有该镜像了
curl 192.168.10.146:5000/v1/search
{"num_results": 1, "query": "", "results": [{"description": "", "name": "library/mysql"}]}
在其他服务器上下载刚才的镜像
docker pull 192.168.10.146:5000/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端⼝映射,这时只能,先提交成镜像,然后重新启动,详见其它步骤
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论