你还不会在CentOS7上安装Docker嘛?

Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

通过 uname -r 命令查看你当前的内核版本

[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Docker 软件包和依赖包已经包含在默认的 CentOS-Extras 软件源里,安装命令如下:

[root@localhost ~]# yum -y install docker

查看docker版本

[root@localhost ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-162.git64e9980.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      64e9980/1.13.1
 Built:           Wed Jul  1 14:56:42 2020
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-162.git64e9980.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      64e9980/1.13.1
 Built:           Wed Jul  1 14:56:42 2020
 OS/Arch:         linux/amd64
 Experimental:    false
[root@localhost ~]# 

启动docker

[root@localhost ~]# systemctl start docker


开机启动docker

[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.


停止docker
 

systemctl stop docker

容器操作

1、搜索镜像
[root@localhost ~]# docker search tomcat
2、拉取镜像
[root@localhost ~]# docker pull tomcat
3、根据镜像启动容器
docker run --name mytomcat -d tomcat:latest
4、docker ps  
查看运行中的容器
5、 停止运行中的容器
docker stop  容器的id
6、查看所有的容器
docker ps -a
7、启动容器
docker start 容器id
8、删除一个容器
 docker rm 容器id
9、启动一个做了端口映射的tomcat
[root@localhost ~]# docker run -d -p 8888:8080 tomcat
-d:后台运行
-p: 将主机的端口映射到容器的一个端口    主机端口:容器内部的端口

10、为了演示简单关闭了linux的防火墙
service firewalld status ;查看防火墙状态
service firewalld stop:关闭防火墙
11、查看容器的日志
docker logs container-name/container-id

更多命令参看
https://docs.docker.com/engine/reference/commandline/docker/
可以参考每一个镜像的文档

查看镜像、容器、数据卷所占用的空间

[root@localhost ~]# docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              3                   3                   776.6 MB            0 B (0%)
Containers          4                   1                   34.66 kB            1.893 kB (5%)
Local Volumes       9                   4                   895.6 MB            491 MB (54%)
[root@localhost ~]#

容器退出再次进入报错

[root@localhost ~]# docker run -it -v /home/docker/build:/root/build --privileged -h hadoop1 --name hadoop1 centos /bin/bash
/usr/bin/docker-current: Error response from daemon: Conflict. The container name "/hadoop1" is already in use by container a094bdef9e1cac62a17022e568fe9b1eb021e13adf8ed2624a71be5a2e42c618. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.
[root@localhost ~]#
docker ps: 查看当前运行的容器
docker ps -a:查看所有容器,包括停止的。

标题含义:

  • CONTAINER ID:容器的唯一表示ID。
  • IMAGE:创建容器时使用的镜像。
  • COMMAND:容器最后运行的命令。
  • CREATED:创建容器的时间。
  • STATUS:容器状态。
  • PORTS:对外开放的端口。
  • NAMES:容器名。可以和容器ID一样唯一标识容器,同一台宿主机上不允许有同名容器存在,否则会冲突。
[root@localhost ~]# docker kill ee2b1b2311af
Error response from daemon: Cannot kill container ee2b1b2311af: Container ee2b1b2311af214026af0169bc3b5b578a85a1367edfa30f5e5b2b8ed4736917 is not running
[root@localhost ~]# docker kill 5acf0e8da90b
Error response from daemon: Cannot kill container 5acf0e8da90b: No such container: 5acf0e8da90b
[root@localhost ~]# docker rm ee2b1b2311af
ee2b1b2311af
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
docker.io/rabbitmq        3.8-management      64a1f920fb0d        2 days ago          187 MB
docker.io/redis           latest              1319b1eaa0b7        3 days ago          104 MB
docker.io/elasticsearch   latest              5acf0e8da90b        22 months ago       486 MB
[root@localhost ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 5acf0e8da90b
d43db002b4c39f3d7bfb8e295df0a0163f5d8ea6134ac9454e1803211d2eec53

参数解释:

  • -v 表示基于容器的centos系统的/root/build目录与本地/home/hadoop/build共享;这可以很方便将本地文件上传到Docker内部的centos系统;
  • -h 指定主机名为hadoop1
  • –-name  指定容器名
  • /bin/bash  使用bash命令

安装MySQL实例 

docker pull mysql

错误的启动

[root@localhost ~]# docker run --name mysql01 -d mysql
42f09819908bb72dd99ae19e792e0a5d03c48638421fa64cce5f8ba0f40f5846

mysql退出了
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
42f09819908b        mysql               "docker-entrypoint.sh"   34 seconds ago      Exited (1) 33 seconds ago                            mysql01
538bde63e500        tomcat              "catalina.sh run"        About an hour ago   Exited (143) About an hour ago                       compassionate_
goldstine
c4f1ac60b3fc        tomcat              "catalina.sh run"        About an hour ago   Exited (143) About an hour ago                       lonely_fermi
81ec743a5271        tomcat              "catalina.sh run"        About an hour ago   Exited (143) About an hour ago                       sick_ramanujan


//错误日志
[root@localhost ~]# docker logs 42f09819908b
error: database is uninitialized and password option is not specified 
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD;这个三个参数必须指定一个

正确的启动

[root@localhost ~]# docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
b874c56bec49fb43024b3805ab51e9097da779f2f572c22c695305dedd684c5f
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b874c56bec49        mysql               "docker-entrypoint.sh"   4 seconds ago       Up 3 seconds        3306/tcp            mysql01

做了端口映射

[root@localhost ~]# docker run -p 3306:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
ad10e4bc5c6a0f61cbad43898de71d366117d120e39db651844c0e73863b9434
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ad10e4bc5c6a        mysql               "docker-entrypoint.sh"   4 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp   mysql02

总结:

详细步骤请见https://docs.docker.com/engine/install/centos/

 #1、卸载旧版本
 yum remove docker \
         docker-client \
         docker-client-latest \
         docker-common \
         docker-latest \
         docker-latest-logrotate \
         docker-logrotate \
         docker-engin
 #2、下载需要的安装包
 yum install -y yum-utils
 #3、设置镜像仓库(推荐使用aliyun,下载速度较快)
  yum-config-manager --add-repo      
     http://mirror   s.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 #4、更新yum软件包索引
 yum makecache fast
 #4、安装(docker-ce社区版  docker-ee企业版)
  yum install docker-ce docker-ce-cli containerd.io
 #5、启动
 systemctl start docker
 #6、查看安装版本
 docker version
 #7、设置开机自动启动
 systemctl enable docker.service
 #8、关闭开机自动启动
 systemctl disable docker.service
 #9、查看是否设置开机启动
 systemctl list-unit-files | grep enable         

 

 #1、卸载依赖
 yum remove docker-ce docker-ce-cli containerd.io
 #2、删除资源
 sudo rm -rf /var/lib/docker
 #3、默认工作路径
 /var/lib/docker

你可能感兴趣的:(Linux,Docker)