建议在linux环境下安装Docker
,window
环境搭建比较复杂且容易出错,使用·
来安装Docker环境很方便(这里不推荐使用centos6.x版本)。
Docker 软件包已经包括在默认的 CentOS-Extras 软件源里。因此想要安装 docker,只需要运行下面的 yum 命令:
yum install docker
安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:
systemctl start docker.service
systemctl enable docker.service
查看docker
版本
docker version
搜索并运行某个镜像,以hello-world
为例:
[root@Master docker]# docker search hello-world
[root@Master docker]# docker pull hello-world #下拉一个hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Image is up to date for hello-world:latest
docker run hello-world
[root@Master docker]# docker run hello-world
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.
(amd64)
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.
[root@Master docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
hello-world <none> fce289e99eb9 18 months ago 1.84kB
以安装mysql为例
去hub找需要的mysql版本:https://registry.hub.docker.com/_/mysql?tab=tags
[root@Master docker]# docker pull mysql:5.7
5.7: Pulling from library/mysql
8559a31e96f4: Pull complete
d51ce1c2e575: Pull complete
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
d85174a87144: Pull complete
a4ad33703fa8: Pull complete
f7a5433ce20d: Pull complete
3dcd2a278b4a: Pull complete
Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
[root@Master docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 9cfcce23593a 2 weeks ago 448MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
hello-world <none> fce289e99eb9 18 months ago 1.84kB
[root@Master docker]# docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 3306:3306 --restart=always mysql:5.7 #运行mysql
851296f1b5d8802e145982b019475d52c7bd5f3b53ec1f7be71f505db46d96ff
[root@Master docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
851296f1b5d8 mysql:5.7 "docker-entrypoint.s…" 53 seconds ago Up 52 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
进入到容器
[root@Master docker]# docker exec -ti mysql bash
root@851296f1b5d8:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 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>
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
再来了解一下Docker
的三个主要概念:
Docker image:镜像是只读的,镜像中包含有需要运行的文件。镜像用来创建container,一个镜像可以运行多个container;镜像可以通过Dockerfile创建,也可以从Docker hub/registry上下载。
Docker container:容器是Docker的运行组件,启动一个镜像就是一个容器,容器是一个隔离环境,多个容器之间不会相互影响,保证容器中的程序运行在一个相对安全的环境中。
Docker hub/registry: 共享和管理Docker镜像,用户可以上传或者下载上面的镜像,官方地址为https://registry.hub.docker.com/,也可以搭建自己私有的Docker registry。