本文来自个人github pages博客http://galengao.github.io/ 即www.gaohuirong.cnmysql
摘要:linux
目前红帽RHEL系统下面安装docker能够有两种方式:一种是使用curl得到docker的安装脚本进行安装,还有一种是使用yum包管理器来安装docker。nginx
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.git
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:github
$ 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.sql
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.docker
Log into your machine as a user with sudo or root privileges.ubuntu
$ yum update
$ tee /etc/yum.repos.d/docker.repo <<EOF [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF
$ sudo yum install docker-engine
$ sudo service docker start
$ 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: http://docs.docker.com/userguide/
Log into your machine as a user with sudo or root privileges.centos
$ sudo yum update
$ curl -fsSL https://get.docker.com/ | sh # This script adds the docker.repo repository and installs Docker.
$ sudo service docker start # Verify docker is installed correctly by running a test image in a container. $ sudo docker run hello-world
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.ruby
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:
groupadd docker
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
$ 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.
You can uninstall the Docker software with yum.
$ yum list installed | grep docker docker-engine.x86_64 1.7.1-1.el7 @/docker-engine-1.7.1-1.el7.x86_64.rpm
$ sudo yum -y remove docker-engine.x86_64
This command does not remove images, containers, volumes, or user-created configuration files on your host.
$ rm -rf /var/lib/docker
Locate and delete any user-created configuration files.
docker images
[root@galen yum.repos.d]# docker search centos:6.6 NAME DESCRIPTION STARS OFFICIAL AUTOMATED anarh/centos6.6 Docker image for centos6.6 0 [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.6 0 [OK] mystique/hadoopbase Hadoop base image - Centos 6.6 Updated to ... 0 [OK] incu6us/centos6.6-with-nginx Wav server for FreeCall 0 [OK]
[root@galen yum.repos.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@galen yum.repos.d]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE anarh/centos6.6 latest eeb98e74a7bd 10 months ago 202.6 MB
[root@galen yum.repos.d]# docker run -t -i anarh/centos6.6 /bin/bash [root@14aa06d651c6 /]#
咱们在里面编译安装完mysql后,咱们就提交生成镜像,之后就能够直接启动引用该镜像了
exit
后经过docker ps -a
找到该容器names对应的容器IDcommit
命令来提交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
[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的官方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.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 pull registry # ps:此处已经不想吐槽,由于GFW的关系,我不知道重试了多少次才下载成功。
docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry # -p端口映射, 默认状况下,仓库会被建立在容器的 /tmp/registry 下。能够经过 -v 参数来将镜像文件存放在本地的指定路径。 例以下面的例子将上传的镜像放到 /opt/data/registry 目录。
docker tag galen/centos6.6-mysql5.6 127.0.0.1:5000/mysql # 此处我命名为这个127.0.0.1:5000/mysql,docker images时就会看到一个127.0.0.1:5000/mysql镜像
docker push 127.0.0.1:5000/mysql
curl http://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。
如何找到你上述那个容器呢?
一、退出来后执行下面命令会把全部容器列出来 docker ps -a
二、若是当初启动的时候没有指定一个可认识的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
三、找到后当即把那个名字重名之后,方便之后好找。原来你没有指定name,它会本身随机一个,如上图的name列
docker rename 旧名字 新名字
docker rename drunk_goldberg mysql
四、若是要从新加新的参数启动该容器,好比加上-p端口映射,这时只能,先提交成镜像,而后从新启动,详见其它步骤