個人docker全套流程例子

本文來自個人github pages博客http://galengao.github.io/ 即www.gaohuirong.cnmysql

摘要:linux

  • 下文是本身從搭建docker到docker裏安裝mysql到push的一遍大概過程
  • docker的安裝直接引用官方文檔,英文比較簡單,因此沒有多加翻譯

目前紅帽RHEL系統下面安裝docker能夠有兩種方式:一種是使用curl得到docker的安裝腳本進行安裝,還有一種是使用yum包管理器來安裝docker。nginx

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.git

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: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

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.docker

Install with yum

Log into your machine as a user with sudo or root privileges.ubuntu

  • 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=1
gpgcheck=1
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.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/
 

Install with the script

Log into your machine as a user with sudo or root privileges.centos

  • 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.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:

  • 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@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]
 
  • pull鏡像,上面搜索到不少,咱們選擇一個下載
[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 /]# 
 
  • 而後就能夠在裏面像正常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的用戶,前往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 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.6 127.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 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端口映射,這時只能,先提交成鏡像,而後從新啓動,詳見其它步驟

相關文章
相關標籤/搜索