前言
简介
Docker 是一个开源的应用容器引擎,基于 Go 语言,并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到装载了任何流行的 Linux 版本系统的机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低。
Docker 支持将软件编译成一个镜像,在镜像中对各种软件做好配置,然后将镜像发布出去,其他使用者可以直接使用这个镜像。运行中的这个镜像成为容器,容器的启动是非常快速的。类似 windows 中的 ghost 操作系统,安装完成即拥有了很多预装软件。
核心概念
- 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 的步骤:
- 安装 Docker。
- 取 Docker 仓库中找到要使用软件对应的镜像。
- 使用 Docker 运行这个镜像,运行中的镜像就是一个 Docker 容器。
- 对容器的启动与停止就是对软件的启动停止。
安装
检查内核版本
Docker 要求 CentOS 系统的内核版本高于 3.10,查看当前系统内核版本:
uname -r
升级软件包及内核
如若你的 CentOS7 系统的内核版本低于了 3.10,那么可以通过以下命令升级:
yum update
注意,不适用 CentOS6。
安装Docker
执行下面命令来安装 Docker:
yum install 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
1、拉取 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
2、创建并运行一个 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
3、通过 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>
4、远程连接测试:
安装连接Redis
1、拉取 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
2、创建并运行一个 redis 容器:
[root@localhost ~]# docker run -d -p 6379:6379 --name myredis redis 55b1c8d0b0779177f2d7e80b54534b42c79c0dfd77cd1a407ab3f885c6c99b7e
3、使用 redis 桌面工具(百度网盘下载 | 提取码:jl1a)连接测试:
安装RabbitMQ
1、拉取 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 版本。
2、创建并运行一个容器:
[root@localhost ~]# docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq rabbitmq:3-management 98affa27f91b37376642a8ce32c78439c9a39cf643104d1e1f41ebfd96d95379
这里有映射两个端口,5672 映射的是 RabbitMQ 服务端口,而 15672 映射的是 RabbitMQ 提供的 web 可视化管理端口。
3、测试访问 RabbitMQ 的可视化管理页:
默认可以通过 guest 用户登录,密码也是 guest。
安装ElasticSearch
1、拉取 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
2、创建并运行一个容器:
[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 端口。
3、测试连接,浏览器访问 9200 端口如下则运行成功:
安装Zookeeper
1、拉取 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
2、创建并运行一个容器:
[root@localhost ~]# docker run --name myZookeeper -p 2181:2181 -p 2888:2888 -p 3888:3888 --restart always -d zookeeper 7131a3e7db31900f5f3acd5db51d5e75f23b82ee223c926514679c4b477e4471
Zookeeper 默认暴露 2181、2888、3888 这三个端口,分别为客户端端口,集群端口,选举端口。