2-Docker CE安装
1、Docker版本
Docker在17.03版本前叫Docker Engine,版本号范围为0.1.0~1.13.1
17.03之后分为Docker CE(社区版)和Docker EE(企业版),并采用基于时间的版本号方案。
Docker CE/EE每个季度发布一个季度版本,也就是说每年会发布 4 个季度版本, 17.03, 17.06, 17.09, 17.12 就是 2017 年的 4 个季度版本的版本号, 同时 Docker CE 每个月还会发布一个 EDGE 版本,比如 17.04, 17.05, 17.07, 17.08, 17.10, 17.11.
2、Docker安装
2.1、设置镜像源
[root@localhost yum.repos.d]# /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 默认docker镜像源为docker.com,更改为国内镜像源
[root@localhost yum.repos.d]# sed -i -e 's#https://download.docker.com#https://mirrors.tuna.tsinghua.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
2.2、安装
[root@localhost yum.repos.d]# yum install docker-ce
2.3、配置文件说明
docker-ce默认配置文件为/etc/docker/daemon.json,此文件默认不存在需创建。
2.3.1、修改Docker镜像源
docker默认镜像源为dockerhub,修改为国内镜像源,主要有以下三家
-
docker cn
-
阿里云镜像站
-
中国科技大学镜像站
# vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
2.4、启动docker服务
[root@localhost yum.repos.d]# systemctl start docker.service
3、docker 命令介绍
- docker命令支持两种模式,一种为新的按照分类管理方式,一种为兼容之前的独立命令方式
3.1、分类管理方式
[root@localhost yum.repos.d]# docker --help
Usage: docker [OPTIONS] COMMAND
Management Commands: # 新的分类管理方式
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
3.2、独立命令模式
Commands: # 老的支持方式
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
3.3、查看docker版本
[root@localhost yum.repos.d]# docker version
Client: Docker Engine - Community
Version: 19.03.1
API version: 1.40
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:21:07 2019
OS/Arch: linux/amd64
Experimental: false
3.4、常用命令
docker search: 从docker hub搜索镜像
docker pull:从镜像仓库下载image镜像
docker images: 列出image镜像
docker create: 创建一个新的容器
docker satrt: 启动一个停止状态的容器
docker run:运行一个新的容器
docker attach:
docker ps: 显示容器列表
docker logs:查看容器日志
docker restart:重启容器
dockerr stop:停止一个容器
docker kill:kill容器
docker rm:删除容器
4、示例
4.1、搜索容器
[root@localhost yum.repos.d]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11931 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1651 [OK]
4.2、下载镜像
[root@localhost yum.repos.d]# docker pull redis:latest
latest: Pulling from library/redis
1ab2bdfe9778: Downloading
966bc436cc8b: Download complete
c1b01f4f76d9: Download complete
8a9a85c968a2: Download complete
8e4f9890211f: Download complete
93e8c2071125: Download complete
latest: Pulling from library/redis
1ab2bdfe9778: Pull complete
966bc436cc8b: Pull complete
c1b01f4f76d9: Pull complete
8a9a85c968a2: Pull complete
8e4f9890211f: Pull complete
93e8c2071125: Pull complete
Digest: sha256:9755880356c4ced4ff7745bafe620f0b63dd17747caedba72504ef7bac882089
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
4.3、查看本地docker镜像
[root@localhost yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx stable-alpine 8587e8f26fc1 2 weeks ago 21.2MB
redis latest f7302e4ab3a8 3 weeks ago 98.2MB
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx stable-alpine 8587e8f26fc1 2 weeks ago 21.2MB
redis latest f7302e4ab3a8 3 weeks ago 98.2MB
4.4、创建容器
# 通过-it创建busybox容器
[root@localhost yum.repos.d]# docker run --name b1 -it busybox:latest
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
7c9d20b9b6cd: Pull complete
Digest: sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e
Status: Downloaded newer image for busybox:latest
# 通过-d创建nginx容器并后台启动
[root@localhost ~]# docker run --name nginxserver1 -d nginx:stable-alpine
b588101f59095e9c57e36e367ac85a67f1597a22321851c4a860a7d083ccc28f
4.4.1、-it和-d的区别
- -it 表示启动容器时启动一个伪终端进程,此进程PID为1,启动后容器不会退出。
- -d 表示后台启动服务,如果容器中没有PID为1的进程启动,则使用-d运行后容器会自动退出。
4.5、查看本地容器列表
# docker ps查看运行状态的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1669d617b6b2 busybox:latest "sh" 4 minutes ago Up 4 minutes b1
# docker ps -a查看所有状态的容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1669d617b6b2 busybox:latest "sh" 5 minutes ago Exited (130) 2 seconds ago b1
8e0295137493 nginx:stable-alpine "nginx -g 'daemon of…" 8 minutes ago Exited (0) 6 minutes ago nginxser1