移除旧的版本
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
安装一些必要的系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 缓存
yum makecache fast
安装 Docker-ce
yum -y install docker-ce
启动Docker后台服务
systemctl start docker
测试运行 hello-world
docker run hello-world
运行结果
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe Status: Downloaded newer image for hello-world:latest
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.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Docker运行nginx
拉取镜像
docker pull hub.c.163.com/library/nginx:latest
以后台运行nginx,并指定端口映射。- -name指定容器名称,如果不指定,默认会随机生成一个名称,-d代表后台运行,8080是指主机端口,80为容器端口
docker run --name nginx-test -d -p 8080:80 hub.c.163.com/library/nginx
使用docker ps命令查看运行的容器
CONTAINER ID | IMAGE | … | PORTS | NAMES |
---|---|---|---|---|
4f1523ef4d16 | hub.c.163.com/library/nginx | … | 0.0.0.0:8080->80/tcp | nginx-test |
浏览器输入ip:8080即可访问到nginx页面
常用命令
拉取镜像,TAG是版本号的意思
docker pull [OPTIONS] NAME[:TAG]
查看本机有哪些镜像,[REPOSITORY [:TAG]]指定镜像名称和TAG
docker images [OPTIONS] [REPOSITORY [:TAG]]
删除镜像
docker rmi IMAGEID
强制删除镜像
docker rmi -f IMAGEID
运行docker,COMMAND是指要执行的命令,ARG…是指命令依赖的参数
docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG…]
查看机器上运行的容器
docker ps
查看机器上运行过的容器
docker ps -a
进入运行的容器
docker exec -it CONTAINERID /bin/bash
停止容器
docker stop CONTAINERID
启动容器
docker start CONTAINERID
停用全部运行中的容器
docker stop KaTeX parse error: Expected 'EOF', got '&' at position 8: (docker&̲nbsp;ps -q…(docker ps -aq)
一条命令实现停用并删除容器
docker stop KaTeX parse error: Expected 'EOF', got '&' at position 8: (docker&̲nbsp;ps -q…(docker ps -aq)