CentOS7下Docker的安裝與使用

前言

簡介

Docker 是一個開源的應用容器引擎,基於 Go 語言,並聽從 Apache2.0 協議開源。php

Docker 可讓開發者打包他們的應用以及依賴包到一個輕量級、可移植的容器中,而後發佈到裝載了任何流行的 Linux 版本系統的機器上,也能夠實現虛擬化。java

容器是徹底使用沙箱機制,相互之間不會有任何接口,更重要的是容器性能開銷極低。mysql

Docker 支持將軟件編譯成一個鏡像,在鏡像中對各類軟件作好配置,而後將鏡像發佈出去,其餘使用者能夠直接使用這個鏡像。運行中的這個鏡像成爲容器,容器的啓動是很是快速的。相似 windows 中的 ghost 操做系統,安裝完成即擁有了不少預裝軟件。linux

核心概念

  • Docker 鏡像(Images):Docker 鏡像是用於建立 Docker 容器的模板。
  • Docker 容器(Container):容器是獨立運行的一個或一組應用。
  • Docker 客戶端(Client):客戶端經過命令行或者其它工具來使用 Docker API(https://docs.docker.com/develop/sdk)來與 Docker 的守護進程通訊。
  • Docker 主機(Host):一個物理或者虛擬的機器用於運行 Docker 守護進程和容器。
  • Docker 倉庫(Registry):Docker 倉庫用來保存鏡像,能夠理解爲代碼版本控制中的代碼倉庫。Docker Hub(https://hub.docker.com)提供了龐大的鏡像集合供使用。

使用 Docker 的步驟:nginx

  1. 安裝 Docker。
  2. 取 Docker 倉庫中找到要使用軟件對應的鏡像。
  3. 使用 Docker 運行這個鏡像,運行中的鏡像就是一個 Docker 容器。
  4. 對容器的啓動與中止就是對軟件的啓動中止。

安裝

檢查內核版本

Docker 要求 CentOS 系統的內核版本高於 3.10,查看當前系統內核版本:web

uname -r

升級軟件包及內核

如若你的 CentOS7 系統的內核版本低於了 3.10,那麼能夠經過如下命令升級:redis

yum update

注意,不適用 CentOS6。spring

安裝Docker

執行下面命令來安裝 Docker:sql

yum install docker

啓動Docker

執行下面命令來啓動 Docker:docker

systemctl start docker

查看 Docker 版本信息以檢查是否安裝成功:

[root@localhost ~]# docker -v
Docker version 1.13.1, build b2f74b2/1.13.1

設置開機啓動

systemctl enable docker

經常使用操做

鏡像操做

查詢鏡像

若是咱們要在 Docker 倉庫中查詢一個鏡像,好比咱們須要一個 MySql 鏡像,可經過以下操做進行查詢:

[root@localhost ~]# docker search mysql
索引名稱    鏡像名稱                                   描述                            關注     是否官方    是否自動構建
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io
/mysql MySQL is a widely used, open-source relati... 7955 [OK] docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 2662 [OK] docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 598 [OK] docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 424 [OK] docker.io docker.io/zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 176 [OK] docker.io docker.io/hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 110 docker.io docker.io/zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 92 [OK] docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 60 [OK] docker.io docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK] docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 49 docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 41 docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 31 docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 25 [OK] docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 25 [OK] docker.io docker.io/zabbix/zabbix-web-apache-mysql Zabbix frontend based on Apache web-server... 24 [OK] docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 20 docker.io docker.io/zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 20 [OK] docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 13 docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 12 docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 8 docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 docker.io docker.io/jelastic/mysql An image of the MySQL database server main... 1 docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 0 [OK] docker.io docker.io/cloudposse/mysql Improved `mysql` service with support for ... 0 [OK] docker.io docker.io/widdpim/mysql-client Dockerized MySQL Client (5.7) including Cu... 0 [OK]

下載鏡像

若是咱們要將 Docker 倉庫中的鏡像下載到本地,則可以使用下面命令:

[root@localhost ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ... 
latest: Pulling from docker.io/library/mysql
27833a3ba0a5: Pull complete 
864c283b3c4b: Pull complete 
cea281b2278b: Pull complete 
8f856c14f5af: Pull complete 
9c4f38c23b6f: Pull complete 
1b810e1751b3: Pull complete 
5479aaef3d30: Pull complete 
ded8fa2e1614: Pull complete 
636033ba4d2e: Pull complete 
902e6010661d: Pull complete 
dbe44d2bf055: Pull complete 
e906385f419d: Pull complete 
Digest: sha256:a7cf659a764732a27963429a87eccc8457e6d4af0ee9d5140a3b56e74986eed7
Status: Downloaded newer image for docker.io/mysql:latest

上述操做默認下載的是最新版本(latest)的 mysql 鏡像,咱們還能夠經過 tag 參數來下載指定版本的鏡像,格式爲 docker pull 鏡像名[:tag] 。好比咱們要下載 mysql 5.6 的鏡像,則可使用使用下面操做:

[root@localhost ~]#  docker pull mysql:5.6
Trying to pull repository docker.io/library/mysql ... 
5.6: Pulling from docker.io/library/mysql
27833a3ba0a5: Already exists 
864c283b3c4b: Already exists 
cea281b2278b: Already exists 
8f856c14f5af: Already exists 
9c4f38c23b6f: Already exists 
688342d0934f: Pull complete 
1f90d0188066: Pull complete 
6d31565f1a00: Pull complete 
12062e921a90: Pull complete 
066ae780ca9a: Pull complete 
f6eece737bcc: Pull complete 
Digest: sha256:de2913a0ec53d98ced6f6bd607f487b7ad8fe8d2a86e2128308ebf4be2f92667
Status: Downloaded newer image for docker.io/mysql:5.6

可以使用的 tag 版本能夠在 Docker Hub 官網上看到。

查看已下載鏡像

若是咱們須要查看本地倉庫中已經下載了哪些鏡像,則可以使用下面命令:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mysql     5.6                 bc788a6a2745        3 hours ago         256 MB
docker.io/mysql     latest              7bb2586065cd        3 hours ago         477 MB

刪除已下載鏡像

若是咱們要刪除一個已下載的鏡像,則可經過 docker rmi <IMAGE ID> 來操做,例如:

[root@localhost ~]# docker rmi 7bb2586065cd
Untagged: docker.io/mysql:latest
Untagged: docker.io/mysql@sha256:a7cf659a764732a27963429a87eccc8457e6d4af0ee9d5140a3b56e74986eed7
Deleted: sha256:7bb2586065cd50457e315a5dab0732a87c45c5fad619c017732f5a13e58b51dd
Deleted: sha256:5362a97701b6ba3d39453e9e8b435b4cc3d7b493fb506780b8e2ec9c3ee20b8e
Deleted: sha256:91ae6166a0c2de34f36c2bb6b66ba3782ec6186337b87362961fad845f5ce236
Deleted: sha256:ae307e2a0c006325d4b96c1f0a40b1ae35cf9a034435ec9925291dafed69e01f
Deleted: sha256:b000e0eccfc8ce916a49183e5168c11b65085d940d76ef9c7c264462aa5f1f5c
Deleted: sha256:97a8302a9556468404f8aeda16ef35d94075cc95ed72249fb2bc2835984e8dd5
Deleted: sha256:4e83d49f100fcd78f57b8f1844faad134d8328c5280be14d85a61ae7c7ffb981
Deleted: sha256:11f5b5e11d25f08da50ca38993f02a3194565beaa516b66970adbbffa58141e0

容器操做

下面是以使用 tomcat 鏡像爲例的全部操做:

搜索鏡像:

[root@localhost ~]# docker search tomcat
INDEX       NAME                                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/tomcat                                     Apache Tomcat is an open source implementa...   2324      [OK]       
docker.io   docker.io/tomee                                      Apache TomEE is an all-Apache Java EE cert...   64        [OK]       
docker.io   docker.io/dordoka/tomcat                             Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   53                   [OK]
docker.io   docker.io/davidcaste/alpine-tomcat                   Apache Tomcat 7/8 using Oracle Java 7/8 wi...   34                   [OK]
docker.io   docker.io/bitnami/tomcat                             Bitnami Tomcat Docker Image                     28                   [OK]
docker.io   docker.io/cloudesire/tomcat                          Tomcat server, 6/7/8                            14                   [OK]
docker.io   docker.io/meirwa/spring-boot-tomcat-mysql-app        a sample spring-boot app using tomcat and ...   12                   [OK]
docker.io   docker.io/aallam/tomcat-mysql                        Debian, Oracle JDK, Tomcat & MySQL              11                   [OK]
docker.io   docker.io/tutum/tomcat                               Base docker image to run a Tomcat applicat...   11                   
docker.io   docker.io/jeanblanchard/tomcat                       Minimal Docker image with Apache Tomcat         8                    
docker.io   docker.io/arm32v7/tomcat                             Apache Tomcat is an open source implementa...   6                    
docker.io   docker.io/maluuba/tomcat7-java8                      Tomcat7 with java8.                             3                    
docker.io   docker.io/rightctrl/tomcat                           CentOS , Oracle Java, tomcat application s...   3                    [OK]
docker.io   docker.io/amd64/tomcat                               Apache Tomcat is an open source implementa...   2                    
docker.io   docker.io/arm64v8/tomcat                             Apache Tomcat is an open source implementa...   2                    
docker.io   docker.io/fabric8/tomcat-8                           Fabric8 Tomcat 8 Image                          2                    [OK]
docker.io   docker.io/99taxis/tomcat7                            Tomcat7                                         1                    [OK]
docker.io   docker.io/camptocamp/tomcat-logback                  Docker image for tomcat with logback integ...   1                    [OK]
docker.io   docker.io/1and1internet/debian-9-java-8-tomcat-8.5   Our tomcat 8.5 image                            0                    [OK]
docker.io   docker.io/cfje/tomcat-resource                       Tomcat Concourse Resource                       0                    
docker.io   docker.io/jelastic/tomcat                            An image of the Tomcat Java application se...   0                    
docker.io   docker.io/oobsri/tomcat8                             Testing CI Jobs with different names.           0                    
docker.io   docker.io/picoded/tomcat7                            tomcat7 with jre8 and MANAGER_USER / MANAG...   0                    [OK]
docker.io   docker.io/s390x/tomcat                               Apache Tomcat is an open source implementa...   0                    
docker.io   docker.io/swisstopo/service-print-tomcat             backend tomcat for service-print "the true...   0  

拉取鏡像:

[root@localhost ~]# docker pull tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ... 
latest: Pulling from docker.io/library/tomcat
22dbe790f715: Pull complete 
0250231711a0: Pull complete 
6fba9447437b: Pull complete 
4afad9c4aba6: Pull complete 
12fce9923c9d: Pull complete 
0dd81083d77e: Pull complete 
4c2afc6ff72d: Pull complete 
a62332a260f5: Pull complete 
96dfb5ad080f: Pull complete 
782f63228ce1: Pull complete 
d610fffe30ad: Pull complete 
Digest: sha256:aacce4e5ca37a3b8241c544deffd65f4cafbfb1a2fb2066f97621253c34f7dc4
Status: Downloaded newer image for docker.io/tomcat:latest

建立並啓動容器

使用鏡像建立並啓動一個容器,命令格式以下:

docker run --name <container-name> [-d] <image-name>[:tag]

其中 container-name 爲將啓動容器的自定義實例名稱, -d 表示後臺運行, image-name 爲鏡像名稱, :tag 爲版本表示。如:

[root@localhost ~]# docker run --name mytomcat -d tomcat:latest
80b379dfb0c8a3038ab7c4a969049331c2dc3e59f1281d35390e1193ad04e349

此時 tomcat 容器就已經運行了。

查看運行中的容器

若是咱們想要查看在 Docker 中正在運行的容器,則可以使用下面命令:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
80b379dfb0c8        tomcat:latest       "catalina.sh run"   About a minute ago   Up About a minute   8080/tcp            mytomcat

中止運行中的容器

若是咱們要中止一個運行中的容器,則可以使用以下命令:

docker stop <container-id|names>

咱們能夠經過運行中容器的 id 或名稱來中止該容器,如:

[root@localhost ~]# docker stop mytomcat
mytomcat

查看全部容器

若是咱們要查看全部容器(包含已中止的容器),則可經過以下命令:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
80b379dfb0c8        tomcat:latest       "catalina.sh run"   13 minutes ago      Exited (143) 39 seconds ago                       mytomcat

啓動容器

此時 Docker 中已經存在了建立好的容器,若是咱們要啓動它,則可以使用下面命令:

docker start <container-id|names>

咱們能夠經過已存在的容器 id 或名稱來啓動該容器,如:

[root@localhost ~]# docker start mytomcat
mytomcat

刪除容器

若是咱們要刪除一個已存在的容器,則可經過下面命令:

docker rm <container-id|names>

咱們能夠經過已存在的容器 id 或名稱來刪除該容器,如:

[root@localhost ~]# docker rm mytomcat
mytomcat

端口映射

上面咱們啓動了 tomcat ,但其實是不能被訪問的,由於它的啓動只是相對在 Docker 提供的沙箱環境中,若是咱們要在外部訪問 Docker 中容器暴露的端口,則須要在建立並啓動容器時經過 -p [本機端口]:[容器端口] 參數來指定端口映射,例如:

[root@localhost ~]# docker run --name mytomcat -d -p 8088:8080 tomcat
57ec817e273be44187a054269cf45a4a31837e85588deb424ec7b92a10e8014d
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
57ec817e273b        tomcat              "catalina.sh run"   15 seconds ago      Up 15 seconds       0.0.0.0:8088->8080/tcp   mytomcat

此時就能夠經過本機 8088 端口訪問到 Docker 容器中的 8080 端口。固然,能成功訪問的前提是要關閉本機的防火牆或配置暴露指定端口。

查看容器日誌

查看容器運行期間生成的日誌可經過以下命令: 

docker logs <container-id|names> 

咱們能夠經過已存在的容器 id 或名稱來查看該容器的日誌,如:

[root@localhost ~]# docker logs mytomcat
27-Mar-2019 04:13:41.439 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.38
27-Mar-2019 04:13:41.453 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Feb 5 2019 11:42:42 UTC
27-Mar-2019 04:13:41.453 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.38.0
27-Mar-2019 04:13:41.453 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
27-Mar-2019 04:13:41.453 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-957.el7.x86_64
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_181-8u181-b13-2~deb9u1-b13
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
27-Mar-2019 04:13:41.454 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
27-Mar-2019 04:13:41.455 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
27-Mar-2019 04:13:41.455 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.21] using APR version [1.5.2].
27-Mar-2019 04:13:41.455 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
27-Mar-2019 04:13:41.455 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
27-Mar-2019 04:13:41.478 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.0j  20 Nov 2018]
27-Mar-2019 04:13:41.612 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
27-Mar-2019 04:13:41.635 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
27-Mar-2019 04:13:41.667 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
27-Mar-2019 04:13:41.668 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
27-Mar-2019 04:13:41.671 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 807 ms
27-Mar-2019 04:13:41.706 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
27-Mar-2019 04:13:41.712 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.38
27-Mar-2019 04:13:41.730 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
27-Mar-2019 04:13:42.246 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [515] ms
27-Mar-2019 04:13:42.246 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
27-Mar-2019 04:13:42.269 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [23] ms
27-Mar-2019 04:13:42.270 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
27-Mar-2019 04:13:42.665 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [396] ms
27-Mar-2019 04:13:42.665 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
27-Mar-2019 04:13:42.703 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [38] ms
27-Mar-2019 04:13:42.703 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
27-Mar-2019 04:13:42.740 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [37] ms
27-Mar-2019 04:13:42.749 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
27-Mar-2019 04:13:42.783 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
27-Mar-2019 04:13:42.786 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1114 ms

更多 Docker 相關命令可查看官方文檔

補充

配置阿里雲鏡像加速

使用 Docker 進行 pull 操做時可能會很慢,可按以下操做配置阿里雲鏡像加速:

[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker

安裝鏈接mysql

一、拉取 mysql5.6 鏡像文件:

[root@localhost ~]# docker pull mysql:5.6
Trying to pull repository docker.io/library/mysql ... 
5.6: Pulling from docker.io/library/mysql
27833a3ba0a5: Already exists 
864c283b3c4b: Already exists 
cea281b2278b: Already exists 
8f856c14f5af: Already exists 
9c4f38c23b6f: Already exists 
688342d0934f: Pull complete 
1f90d0188066: Pull complete 
6d31565f1a00: Pull complete 
12062e921a90: Pull complete 
066ae780ca9a: Pull complete 
f6eece737bcc: Pull complete 
Digest: sha256:de2913a0ec53d98ced6f6bd607f487b7ad8fe8d2a86e2128308ebf4be2f92667
Status: Downloaded newer image for docker.io/mysql:5.6

二、建立並運行一個 mysql5.6 的容器:

[root@localhost ~]# docker run --name mysql56 -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306 mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
3f76e2bcd4e1762f40d9bd9dbd8aa5798c0e4f1035c1d518d05359fdcdcdfd19

三、經過 mysql 容器提供的客戶端功能測試鏈接 mysql 容器:

[root@localhost ~]# docker run -it --link mysql56 --rm mysql sh -c 'exec mysql -hmysql56 -uroot -proot'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

四、遠程鏈接測試:

安裝鏈接Redis

一、拉取 redis 鏡像文件:

[root@localhost ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ... 
latest: Pulling from docker.io/library/redis
27833a3ba0a5: Already exists 
cde8019a4b43: Pull complete 
97a473b37fb2: Pull complete 
c6fe0dfbb7e3: Pull complete 
39c8f5ba1240: Pull complete 
cfbdd870cf75: Pull complete 
Digest: sha256:000339fb57e0ddf2d48d72f3341e47a8ca3b1beae9bdcb25a96323095b72a79b
Status: Downloaded newer image for docker.io/redis:latest

二、建立並運行一個 redis 容器:

[root@localhost ~]# docker run -d -p 6379:6379 --name myredis redis
55b1c8d0b0779177f2d7e80b54534b42c79c0dfd77cd1a407ab3f885c6c99b7e

三、使用 redis 桌面工具(百度網盤下載 | 提取碼:jl1a)鏈接測試:

安裝RabbitMQ

一、拉取 RabbitMQ 鏡像:

[root@localhost ~]# docker pull rabbitmq:3-management
Trying to pull repository docker.io/library/rabbitmq ... 
3-management: Pulling from docker.io/library/rabbitmq
898c46f3b1a1: Pull complete 
63366dfa0a50: Pull complete 
041d4cd74a92: Pull complete 
6e1bee0f8701: Pull complete 
d258c5276992: Pull complete 
89aa8ff049c8: Pull complete 
a5722be5aa59: Pull complete 
6081a2e23d49: Pull complete 
123a6d264e66: Pull complete 
b5ef0b7eee2d: Pull complete 
59a84fe37b49: Pull complete 
cd86d2e1983d: Pull complete 
Digest: sha256:32071e38e721c692e41c4884f9540d65981f725b79ab83fff7551727192b8b79
Status: Downloaded newer image for docker.io/rabbitmq:3-management

tag 中含有 management 的是附帶 web 可視化界面的 RabbitMQ 版本。

二、建立並運行一個容器:

[root@localhost ~]# docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq rabbitmq:3-management
98affa27f91b37376642a8ce32c78439c9a39cf643104d1e1f41ebfd96d95379

這裏有映射兩個端口,5672 映射的是 RabbitMQ 服務端口,而 15672 映射的是 RabbitMQ 提供的 web 可視化管理端口。

三、測試訪問 RabbitMQ 的可視化管理頁:

默承認以經過 guest 用戶登陸,密碼也是 guest。

安裝ElasticSearch

一、拉取 ElasticSearch 鏡像:

[root@localhost ~]# docker pull elasticsearch
Using default tag: latest
Trying to pull repository docker.io/library/elasticsearch ... 
latest: Pulling from docker.io/library/elasticsearch
05d1a5232b46: Pull complete 
5cee356eda6b: Pull complete 
89d3385f0fd3: Pull complete 
65dd87f6620b: Pull complete 
78a183a01190: Pull complete 
1a4499c85f97: Pull complete 
2c9d39b4bfc1: Pull complete 
1b1cec2222c9: Pull complete 
59ff4ce9df68: Pull complete 
1976bc3ee432: Pull complete 
5af49e8af381: Pull complete 
42c8b75ff7af: Pull complete 
7e6902915254: Pull complete 
99853874fa54: Pull complete 
596fbad6fcff: Pull complete 
Digest: sha256:a8081d995ef3443dc6d077093172a5931e02cdb8ffddbf05c67e01d348a9770e
Status: Downloaded newer image for docker.io/elasticsearch:latest

二、建立並運行一個容器:

[root@localhost ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name myES elasticsearch
5e6541d7c18f2467f40a22c58242ac76c6ce5a5311f0de6b6ca2842e26719f24
  • ElasticSearch 服務默認會佔用 2g 的堆內存空間,測試時能夠經過指定 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" 來分別限制 ElasticSearch 初始堆內存大小爲 256m,最大使用內存爲 256m。
  • ElasticSearch 默認通訊使用 9200 端口,而在分佈式狀況下它各個節點的通訊使用 9300 端口。

三、測試鏈接,瀏覽器訪問 9200 端口以下則運行成功:

安裝Zookeeper

一、拉取 Zookeeper 鏡像:

[root@localhost ~]# docker pull zookeeper
Using default tag: latest
Trying to pull repository docker.io/library/zookeeper ... 
latest: Pulling from docker.io/library/zookeeper
8e402f1a9c57: Pull complete 
4866c822999c: Pull complete 
cf419f3f41ff: Pull complete 
88430b15d43f: Pull complete 
d5a9723280f1: Pull complete 
c43110341cd8: Pull complete 
9ec9c92edf13: Pull complete 
cdf8329cd90a: Pull complete 
Digest: sha256:8832a49f7ee6399c608e9a666e294adeaef0e4b1bc540c60ac75b8ec4b41f572
Status: Downloaded newer image for docker.io/zookeeper:latest

二、建立並運行一個容器:

[root@localhost ~]# docker run --name myZookeeper -p 2181:2181 -p 2888:2888 -p 3888:3888 --restart always -d zookeeper
7131a3e7db31900f5f3acd5db51d5e75f23b82ee223c926514679c4b477e4471

Zookeeper 默認暴露 218一、288八、3888 這三個端口,分別爲客戶端端口,集羣端口,選舉端口。

相關文章
相關標籤/搜索