【docker安装以及基本命令】
一、docker:基于容器的虚拟化(将应用托管到应用容器上),实现了一种应用程序级别的资源隔离及配额。【什么是容器?虚拟化和容器的区别?】容器:轻量级的虚拟化,容器是由容器镜像来运行 。优点:容器密度高,启动快,没有太多额外的开销;缺点:只能在linux操作系统两者都讲究的是隔离,虚拟化是物理机上可以安装虚拟机安装多种操作系统(常用的vmwore、openstack、kvm都是使用的虚拟化技术);
一、docker:基于容器的虚拟化(将应用托管到应用容器上),实现了一种应用程序级别的资源隔离及配额。
什么是容器?
容器:轻量级的虚拟化(共享同一系统内核),容器是由容器镜像来运行 。
优点:容器密度高,启动快,没有太多额外的开销;缺点:只能在linux操作系统
虚拟化和容器的区别?
不同点:两者都讲究的是隔离,虚拟化是物理机上安装虚拟机然后安装多种操作系统(常用的vmwore、openstack、kvm都是使用的虚拟化技术);容器是直接将容器层安装在linux操作系统之上实现各种板块隔离
1.docker安装
方法一:使用官方安装脚本自动安装
实际上就是下载一个安装脚本,再执行安装(不推荐,因为不能选择版本安装)
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
方法二:使用yum进行安装,不推荐
yum install docker
这样直接从光盘或yum仓库里安装,版本较低
方法三:使用yum添加源进行安装指定版本,推荐
(1)添加docker-ce 源信息
yum-config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(2)更新yum源:是把软件包信息提前把本地缓存一份,为了提高安装搜索软件的速度
yum makecache fast
(3)安装docker-ce
yum -y install docker-ce #默认下载的是最新版本
① 查看都有哪些docker版本
yum list docker-ce.x86_64 --showduplicates | sort -r
② 下载指定版本,这里下载的稳定版本
yum -y install docker-ce-17.03.2.ce
测试:docker version / docker -v
[root@manager yum.repos.d]# docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:46:54 2020
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
#说明docker服务没有开启
[root@manager yum.repos.d]# docker -v
Docker version 1.13.1, build 64e9980/1.13.1s
2.开启服务:systemctl enable --now docker
注1:安装过程中可能出现的问题:
原因是可能之前已经安装过旧版本的docker,在安装的时候报错如下
Transaction check error: file /usr/bin/docker from install of docker-ce-cli-1:19.03.12-3.el7.x86_64 conflicts with file from package docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64 file /usr/bin/dockerd from install of docker-ce-3:19.03.12-3.el7.x86_64 conflicts with file from package docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64
解决:卸载旧版本的包
yum remove docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64
再次安装docker就会成功安装!
注2::[root@manager ~]# docker --help 帮助命令
二、docker三大核心:镜像、容器、仓库
镜像:类似虚拟机镜像
容器:类似linux系统环境,运行和隔离应用。容器从镜像启动的时候,docker会在镜像的最上一层创建一个可写层,镜像本身是只读的,保持不变
仓库:每个仓库存放某一类镜像。分为两类:docker hub共有仓库、docker-registry私有仓库。(图形化构建docker仓库)
三、docker操作
1.仓库相关命令
docker search 镜像名 | 从仓库搜索镜像 |
---|---|
docker pull 镜像名 | 从仓库拉出镜像,默认拉取的最新版 |
docker push 镜像名 | 向仓库推进镜像 【hub.docker.com必须在网页上注册一个账号(docker hub注册账号),然后在命令行使用docker login登录,进行镜像打标签,才能把镜像推到仓库】 |
怎么把镜像push进仓库?
step1——找到本地镜像的ID:docker images ls
step2——登陆Hub:docker login --username=username --password=password --email=email
step3——镜像打标签tag:docker tag <imageID> <namespace>/<image name>:<version tag eg latest>
step4——push镜像:docker push <namespace>/<image name>
eg1.从仓库搜索镜像centos
[root@manager ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6061 [OK]
ansible/centos7-ansible Ansible on Centos7 130 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 116 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 114
...................
eg2: 从仓库拉取镜像busybox
[root@manager ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
76df9210b28c: Pull complete
Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
eg3:向仓库推进镜像
hub.docker.com必须在网页上注册一个账号(docker hub注册账号),才能把镜像推到仓库
docker push 镜像名
docker login
2.镜像相关命令
docker search 镜像名 | 搜索镜像 |
---|---|
docker pull 镜像名 | 拉出镜像,默认拉取的最新版 |
docker image save 镜像名 > name.tar.gz | 导出镜像 |
docker image rm 镜像名 <=>docker rmi 镜像名 | 删除镜像(若要删除正在运行的镜像加-f) |
docker image load -i 文件名 | 导入镜像 |
docker image ls | 查看当前镜像列表 |
docker image inspect 镜像名 | 查看镜像详细信息 |
docker image tag 镜像名:latest 仓库名:tag名 | 给镜像打标记,一般用在把镜像push仓库 |
eg1:查看当前镜像列表,查看镜像详细信息,查看busybox镜像历史记录
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 3 weeks ago 1.22MB
[root@manager ~]# docker image inspect busybox
[root@manager ~]# docker image history busybox
IMAGE CREATED CREATED BY SIZE COMMENT
1c35c4412082 3 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:a84c53d2fe5207d17… 1.22MB
eg2:导出镜像:
方法1:docker image save 镜像名 > 路径位置信息(name.tar.gz)
[root@manager ~]# docker image save busybox > busybox1.tar.gz
方法2:docker image save -o 路径位置信息(name.tar.gz) 镜像名:latest
[root@manager ~]# docker image save -o busybox2.tar.gz busybox:latest
eg3:删除镜像:docker image rm 镜像名
[root@manager ~]# docker image rm busybox
Untagged: busybox:latest
Untagged: busybox@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Deleted: sha256:1c35c441208254cb7c3844ba95a96485388cef9ccc0646d562c7fc026e04c807
Deleted: sha256:1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 b5b4d78bc90c 7 weeks ago 203MB
eg4:导入镜像:docker image load -i 路径位置信息
[root@manager ~]# docker image load -i busybox1.tar.gz
1be74353c3d0: Loading layer 1.437MB/1.437MB
Loaded image: busybox:latest
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 3 weeks ago 1.22MB
centos 7 b5b4d78bc90c 7 weeks ago 203MB
或者
[root@manager ~]# docker image import busybox1.tar.gz busybox:v1
sha256:70fe82f93ddc2f501380d3749e743be3e9afd3d9dfc106ae2e22c92d830f91df
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v1 70fe82f93ddc 6 seconds ago 1.44MB
centos 7 b5b4d78bc90c 7 weeks ago 203MB
eg5:给镜像打标记:tag 一般用在把镜像push进仓库
docker image tag 镜像名:latest 仓库名:tag名
[root@manager ~]# docker image tag busybox:latest busybox:v1.1
[root@manager ~]# docker image tag busybox:latest busybox:v1.2
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 3 weeks ago 1.22MB
busybox v1.1 1c35c4412082 3 weeks ago 1.22MB
busybox v1.2 1c35c4412082 3 weeks ago 1.22MB
centos 7 b5b4d78bc90c 7 weeks ago 203MB
[root@manager ~]# docker image rm busybox:v1.1 busybox:v1.2
Untagged: busybox:v1.1
Untagged: busybox:v1.2
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 3 weeks ago 1.22MB
centos 7 b5b4d78bc90c 7 weeks ago 203MB
docker镜像帮助命令
[root@manager ~]# docker image --help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.
3.容器相关命令:
docker run 镜像名 [-i/-d/-t/–name] | 启动容器 |
---|---|
docker stop 容器名字/容器ID | 停止容器 |
docker kill 容器名字/容器ID | 杀死运行中的容器 |
docker rm 容器名/容器ID | 删除容器(加-f 表示删除运行中的) |
docker start 容器名字/容器ID | 激活关闭中的容器 |
docker ps | 查看正在运行的容器的信息(状态) |
docker ps -a | 查看所有容器的信息(状态) |
docker ps -q | 查看正在运行的容器的ID号 |
docker ps -aq | 查看所有容器的ID号 |
docker inspect 容器名字/容器ID | 查看某个容器的详细信息 |
docker exec -it 容器名 指定的shell | 进入容器 |
docker logs 容器名字/容器ID | 查询容器内部日志 |
退出容器: | 1.要是直接输入exit退出的话,容器的状态就变为Exited,而不是up 2.要时Ctrl+p Ctrl+q退出的话,容器的状态还是up |
docker commit 参数 仓库名 镜像名:tag | 容器内的操作配置提交成一个镜像 |
创建Dockerfile文件去构建镜像 |
eg1:启动容器
方法1(不推荐):
先创建一个容器:docker create 镜像名
再启动容器:docker start 容器名
[root@manager ~]# docker create busybox:latest
47f2de8f995999651498e6f0b58764214817e182ad8eff32b2b0b231f5fd3628
[root@manager ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47f2de8f9959 busybox:latest "sh" 16 seconds ago Created recursing_galileo
[root@manager ~]# docker start recursing_galileo
recursing_galileo
[root@manager ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@manager ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47f2de8f9959 busybox:latest "sh" 58 seconds ago Exited (0) 9 seconds ago recursing_galileo
方法2:docker run 镜像名 [-d/-i/-t/–name],这种启动的话进去又退出的话状态就变成Exited状态
[root@manager ~]# docker run -it busybox:latest
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
[root@manager ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@manager ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fcc320d6f11 busybox:latest "sh" 23 seconds ago Exited (0) 10 seconds ago gracious_hopper
47f2de8f9959 busybox:latest "sh" 3 minutes ago Exited (0) 2 minutes ago recursing_galileo
而docker run 镜像名 [-d/-i/-t/–name],进去按CTRL+p ,CTRL+q的话,状态是UP
[root@manager ~]# docker run -it busybox:latest
/ # [root@manager ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
328a8dbff50f busybox:latest "sh" 9 seconds ago Up 8 seconds vigorous_easley
或者用后台(-d)执行的方式,使状态成为UP【推荐】
[root@manager ~]# docker run -idt --name c1 busybox:latest
baafbd0b1a3dfe19692929cac68e29096652c277140ccdc48f2ec8bd331631ac
[root@manager ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
baafbd0b1a3d busybox:latest "sh" 10 seconds ago Up 9 seconds c1
eg2:停止运行的容器:
[root@manager ~]# docker stop vigorous_easley
vigorous_easley
[root@manager ~]# docker stop baafbd0b1a3d
baafbd0b1a3d
[root@manager ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eg3: 杀死运行的容器
[root@manager ~]# docker kill competent_robinson
competent_robinson
eg4:激活关闭的容器:
[root@manager ~]# docker start competent_robinson
competent_robinson
eg5:查看容器的详细信息:
[root@manager ~]# docker inspect 9f91c7213a7d
[
{
"Id": "9f91c7213a7dda367f8fe152403eb2b6612c2849bb9a06299e360378db3c9f8e",
"Created": "2020-06-25T09:34:09.421984223Z",
"Path": "/bin/sh",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
查看正在运行的容器的ID号
[root@manager ~]# docker ps -q
f658dc1f9c9f
查看所有的容器的ID号
[root@manager ~]# docker ps -aq
f658dc1f9c9f
328a8dbff50f
baafbd0b1a3d
1fcc320d6f11
47f2de8f9959
eg6:删除容器:docker rm 容器名或容器ID
删除所有的容器:查询所有容器 IDdocker ps -aq,加-f表示删除运行中的
[root@manager ~]# docker rm -f `docker ps -aq`
f658dc1f9c9f
328a8dbff50f
baafbd0b1a3d
1fcc320d6f11
47f2de8f9959
[root@manager ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eg7:进入容器:
[root@manager ~]# docker exec -it centos7 /bin/bash
[root@73e57bae16bf /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@dc58900031a2 /]# ip a #有的命令是没有的
bash: ip: command not found
[root@dc58900031a2 /]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=127 time=132 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 1 received, 50% packet loss, time 1004ms
rtt min/avg/max/mdev = 132.518/132.518/132.518/0.000 ms
[root@73e57bae16bf /]# exit 要是直接输入exit退出的话,容器的状态就变为Exited,而不是up
退出容器:1.要是直接输入exit退出的话,容器的状态就变为Exited,而不是up 2.要时Ctrl+p Ctrl+q退出的话,容器的状态还是up
eg8.查询容器内部日志:docker logs 容器名
镜像的制作方式:
eg9:在容器里做的配置或者操作可以将它提交成一个镜像:
1.docker commit 参数 仓库名 镜像名:tag
[root@manager ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith
<hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created
image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@manager ~]# docker commit -a "pyy <1459847670@qq.com>" -m "install htt" centos7 pyy/centos7-httpd:v1.1
sha256:b0aaa32df421d7d4864e635ab7679ce283b5eff342e3daf3a3747a0268793e90
[root@manager ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
pyy/centos7-httpd v1.1 b0aaa32df421 14 seconds ago 203MB
busybox v1 70fe82f93ddc 4 days ago 1.44MB
mattrayner/lamp latest b29dc990e66c 2 weeks ago 898MB
mysql 5.6 8de95e6026c3 2 weeks ago 302MB
busybox latest 1c35c4412082 3 weeks ago 1.22MB
centos 7 b5b4d78bc90c 7 weeks ago
2.使用Dockerfile构建镜像
1.创建目录,在此目录创建一个Dockerfile文件,并进行命令编写
[root@manager ~]# mkdir centos7
[root@manager ~]# cd centos7/
[root@manager centos7]# ls
[root@manager centos7]# vim Dockerfile #文件名字必须这样写
[root@manager centos7]# cat Dockerfile
FROM centos:7
MAINTAINER pyy <1459847670@qq.com
#install wget
RUN yum install -y wget
#download sl[蒸汽火车]
RUN wget http://rpmfind.net/linux/epel/7/x86_64/Packages/s/sl-5.02-1.el7.x86_64.rpm -P /opt/
#install sl
RUN yum install -y /opt/sl-5.02-1.el7.x86_64.rpm
2.开始构建镜像:docker build -t 镜像名 路径
[root@manager centos7]# docker build -t centos-sl .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM centos:7
---> b5b4d78bc90c
Step 2/5 : MAINTAINER pyy <1459847670@qq.com
---> Running in cf8d9e31daeb
Removing intermediate container cf8d9e31daeb
---> affaf920c949
Step 3/5 : RUN yum install -y wget
---> Running in 68119504ded2
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
wget x86_64 1.14-18.el7_6.1 base 547 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/wget-1.14-18.el7_6.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for wget-1.14-18.el7_6.1.x86_64.rpm is not installed
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-8.2003.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : wget-1.14-18.el7_6.1.x86_64 1/1
install-info: No such file or directory for /usr/share/info/wget.info.gz
Verifying : wget-1.14-18.el7_6.1.x86_64 1/1
Installed:
wget.x86_64 0:1.14-18.el7_6.1
Complete!
Removing intermediate container 68119504ded2
---> 8c2c03c0cd2c
Step 4/5 : RUN wget http://rpmfind.net/linux/epel/7/x86_64/Packages/s/sl-5.02-1.el7.x86_64.rpm -P /opt/
---> Running in d7a9e1c8cf52
--2020-06-29 10:14:15-- http://rpmfind.net/linux/epel/7/x86_64/Packages/s/sl-5.02-1.el7.x86_64.rpm
Resolving rpmfind.net (rpmfind.net)... 195.220.108.108
Connecting to rpmfind.net (rpmfind.net)|195.220.108.108|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14244 (14K) [application/x-rpm]
Saving to: '/opt/sl-5.02-1.el7.x86_64.rpm'
0K .......... ... 100% 5.68K=2.5s
2020-06-29 10:14:20 (5.68 KB/s) - '/opt/sl-5.02-1.el7.x86_64.rpm' saved [14244/14244]
Removing intermediate container d7a9e1c8cf52
---> 5e327a21901f
Step 5/5 : RUN yum install -y /opt/sl-5.02-1.el7.x86_64.rpm
---> Running in cb52718c4d83
Loaded plugins: fastestmirror, ovl
Examining /opt/sl-5.02-1.el7.x86_64.rpm: sl-5.02-1.el7.x86_64
Marking /opt/sl-5.02-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package sl.x86_64 0:5.02-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
sl x86_64 5.02-1.el7 /sl-5.02-1.el7.x86_64 17 k
Transaction Summary
================================================================================
Install 1 Package
Total size: 17 k
Installed size: 17 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : sl-5.02-1.el7.x86_64 1/1
Verifying : sl-5.02-1.el7.x86_64 1/1
Installed:
sl.x86_64 0:5.02-1.el7
Complete!
Removing intermediate container cb52718c4d83
---> 8afabae50ed0
Successfully built 8afabae50ed0
Successfully tagged centos-sl:latest
#测试
[root@manager centos7]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-sl latest 8afabae50ed0 7 seconds ago 309MB
pyy/centos7-httpd v1.1 b0aaa32df421 23 minutes ago 203MB
busybox v1 70fe82f93ddc 4 days ago 1.44MB
mattrayner/lamp latest b29dc990e66c 2 weeks ago 898MB
mysql 5.6 8de95e6026c3 2 weeks ago 302MB
busybox latest 1c35c4412082 3 weeks ago 1.22MB
centos 7 b5b4d78bc90c 7 weeks ago 203MB
#启动镜像
[root@manager centos7]# docker run -it centos-sl /bin/bash
[root@1bb4779b1cf1 /]# sl
四、外部访问docker容器:docker run -p(-p表示端口映射);也就是将应用托管到应用容器上
1.从仓库拉出Mysql镜像
[root@manager ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
7d2977b12acb: Pull complete
5fb8400e7f07: Pull complete
234877fbb165: Pull complete
6fe1021f12f3: Pull complete
7e36fe6b53f0: Pull complete
996ec709c11b: Pull complete
5198b7523387: Pull complete
cc9bdad4dcc0: Pull complete
380cd37ad979: Downloading 6.446MB/64.2MB
d64465acf034: Download complete
d4ee6606b3ab: Download complete
380cd37ad979: Downloading 18.26MB/64.2MB
380cd37ad979: Downloading 29.01MB/64.2MB
380cd37ad979: Pull complete
d64465acf034: Pull complete
d4ee6606b3ab: Pull complete
Digest: sha256:2bf1a0a05a6ad437dcac6689e48a9c33774ac92c6213fce2c4196343210592f3
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
2.启动容器mysql(并设置root密码),并且设置外部端口映射
[root@manager ~]# docker run --name mysql -itd -p 3305:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
97be89fd63737771d36882d512f86cf484c5518de9a56848d68ca8ed31edc42c
3.外部安装mysql5.6,并且查看端口3305是否监听
yum whatprovides mysql
yum install mariadb -y
[root@manager ~]# netstat -ltunp | grep 3305
tcp6 0 0 :::3305 :::* LISTEN 6379/docker-proxy
4.从外部访问mysql容器
方法一:docker exec -it mysql mysql -uroot -p123456
[root@manager ~]# docker exec -it mysql mysql -uroot -p123456
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.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> select version();
+-----------+
| version() |
+-----------+
| 5.6.48 |
+-----------+
1 row in set (0.00 sec)
mysql> exit
Bye
方法2:mysql -uroot -p -h172.17.0.2
[root@manager ~]# mysql -uroot -p -h172.17.0.2
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> exit
Bye
注:外部是直接不能进入mysql的,因为没有权限
更多推荐
所有评论(0)