目录
- 一、Docker安装与启动
- 1、安装Docker (安装环境为CentOS7)
- 2、启动Docker
- 3、Docker的基本使用
- 4、Docker容器的操作
- 二、Docker私有仓库
- 1、创建仓库库
- 2、上传镜像
- 三、Dockerfile
- 1、常用命令
- 2、使用Dockerfile创建镜像
一、Docker安装与启动
1、安装Docker (安装环境为CentOS7)
(1)安装需要的软件包, yum-utils 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的:
yum install -y yum-utils device-mapper-persistent-data lvm2
(2)设置yum源为阿里云
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(3)安装docker
yum install docker-ce
(4)设置镜像地址
vi /ect/docker/daemon.json;
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
2、启动Docker
启动docker
systemctl start docker #启动docker
停止docker
systemctl stop docker #停止docker
重启docker
systemctl restart docker #重启docker
查看docker帮助文档
docker --help
3、Docker的基本使用
(1)查看本地镜像:
docker images
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gogs/gogs latest f5089a318904 3 days ago 98.7MB
registry latest 708bc6af7e5e 7 weeks ago 25.8MB
(2)搜索镜像
docker search mysql
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9265 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3305 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 681 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 71
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 66
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
bitnami/mysql Bitnami MySQL Docker Image 36 [OK]
tutum/mysql Base docker image to run a MySQL database se… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
prom/mysqld-exporter 27 [OK]
(3)pull拉取镜像
docker pull mysql:latest #mysql:latest 为 镜像名:版本,版本可以省略,默认最新版本
#根据镜像的NAME或者ID拉取
[root@localhost docker]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
68ced04f60ab: Pull complete
f9748e016a5c: Pull complete
da54b038fed1: Pull complete
6895ec5eb2c0: Pull complete
111ba0647b87: Pull complete
c1dce60f2f1a: Pull complete
702ec598d0af: Pull complete
4aba2fcbe869: Pull complete
b26bbbd533e6: Pull complete
7bd100a66c55: Pull complete
74149336419a: Pull complete
145ea1f01648: Pull complete
Digest: sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
(4)删除镜像
docker rmi mysql
4、Docker容器的操作
(1)创建容器
#-di为守护式,不进入容器
#--name=为容器起的名字,随意
#-p端口映射 宿主机端口:容器端口
#-e指容器内的环境变量,初始化root密码
#mysql为镜像名或者ID
[root@localhost docker]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123321 mysql
c62db957fef49574e1cf0198567eaa622dfcdcd38a46447230ea294d676cc206 #创建成功
(2)查看运行的容器
docker ps
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c62db957fef4 mysql "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
c9e1f3d7b361 registry "/entrypoint.sh /etc…" 57 minutes ago Up 57 minutes 0.0.0.0:5000->5000/tcp registry
(2)查看所有的容器
docker ps -a
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c62db957fef4 mysql "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
ad25d28d7d0f gogs/gogs "/app/gogs/docker/st…" 54 minutes ago Exited (0) 36 seconds ago gogs
c9e1f3d7b361 registry "/entrypoint.sh /etc…" 58 minutes ago Up 58 minutes 0.0.0.0:5000->5000/tcp registry
(3)停止容器
docker stop mysql
二、Docker私有仓库
1、创建仓库库
(1)下载registry镜像
docker pull registry
(2) 创建容器
docker run -di --name=registry -p 5000:5000 registry
(3)页面访问url:http://宿主机ip:5000/v2/_catalog
创建成功,一个空的repositories仓库
(4)在daemon.json中添加信任,否则无法访问
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"insecure-registries":["192.168.0.110:5000"] #添加私有仓库地址
}
(5)重启docker
systemctl daemon-reload #重新加载修改的配置文件
systemctl restart docker #重启docker
2、上传镜像
(1)标记镜像为私有仓库镜像
#tag 为标记
#mysq 为要标记的镜像
#192.168.0.110:5000/mysql 是创建的私有仓库地址/和上传后的命名
docker tag mysql 192.168.0.110:5000/mysql
(2)查看是否创建成功
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.0.110:5000/mysql latest 9b51d9275906 2 weeks ago 547MB
mysql latest 9b51d9275906 2 weeks ago 547MB
registry latest 708bc6af7e5e 8 weeks ago 25.8MB
标记成功 192.168.0.110:5000/mysql
(3)上传镜像
(4)查看私有仓库
上传成功
(5)下载
docker pull 192.168.0.110:5000/mysql #这样就可以从私服下载镜像
三、Dockerfile
Dockerfile是一个包含用于组合映像的命令的文本文档。可以使用在命令行中调用任何命令。 Docker通过读取Dockerfile中的指令自动生成镜像
1、常用命令
| | 作用
| ---------------------------------- | ------------------------------------------------------------
| FROM image_name:tag | 定义了使用哪个基础镜像启动构建流程
| MAINTAINER user_name | 声明镜像的创建者
| ENV key value | 设置环境变量 (可以写多条)
| RUN command | 是Dockerfile的核心部分(可以写多条)
| ADD source_dir/file dest_dir/file | 将宿主机的文件复制到容器内,如果是一个压缩文件,将会在复制后自动解压
| COPY source_dir/file dest_dir/file | 和ADD相似,但是如果有压缩文件并不能解压
| WORKDIR path_dir | 设置工作目录
2、使用Dockerfile创建镜像
(1)创建目录
mkdir /usr/local/dockerjdk1.8
(2)将 jdk-8u162-linux-x64.tar.gz上传到 /usr/local/dockerjdk1.8目录下
(3)在当前目录下创建Dockerfile
[root@localhost dockerjdk1.8]# vi Dockerfile
#定义基础依赖 centos7
FROM centos:7
#指定镜像创建者信息
MAINTAINER Q
#在容器中创建目录
RUN mkdir /usr/local/jdk1.8
#将当前目录下的jdk,复制到容器指定目录下
ADD jdk-8u162-linux-x64.tar.gz /usr/local/jdk1.8/
#配置jdk环境变量
ENV JAVA_HOME /usr/local/jdk1.8/jdk1.8.0_162
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
(4)执行命令创建镜像
[root@localhost dockerjdk1.8]# docker build -t='jdk1.8' .
Sending build context to Docker daemon 189.8MB
Step 1/8 : FROM centos:7
7: Pulling from library/centos
ab5ef0e58194: Pull complete
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
---> 5e35e350aded
Step 2/8 : MAINTAINER Q
---> Running in 55f40d1f988f
Removing intermediate container 55f40d1f988f
---> 6199583f0daf
Step 3/8 : RUN mkdir /usr/local/jdk1.8
---> Running in 169d99418814
Removing intermediate container 169d99418814
---> 02ab7a01a2aa
Step 4/8 : ADD jdk-8u162-linux-x64.tar.gz /usr/local/jdk1.8/
---> cddb688c899b
Step 5/8 : ENV JAVA_HOME /usr/local/jdk1.8/jdk1.8.0_162
---> Running in f0a086c45ad4
Removing intermediate container f0a086c45ad4
---> 656ed8b09e6a
Step 6/8 : ENV JRE_HOME $JAVA_HOME/jre
---> Running in 108f11e0ef6b
Removing intermediate container 108f11e0ef6b
---> 3011393dcf5f
Step 7/8 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
---> Running in 0dc37caee617
Removing intermediate container 0dc37caee617
---> 5fe0931e04a3
Step 8/8 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 4532d12933eb
Removing intermediate container 4532d12933eb
---> 5f169b347ee5
Successfully built 5f169b347ee5
Successfully tagged jdk1.8:latest
执行成功,Dockerfile中8句代码,一次执行成功
(5)查看镜像
[root@localhost dockerjdk1.8]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jdk1.8 latest 5f169b347ee5 2 minutes ago 588MB
查看镜像,我们看到Dockerfile创建的镜像jdk1.8