1.Docker
1.1卸载旧版本
旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本:
$ sudo apt-get remove docker \
docker-engine \
docker.io
1.2使用脚本安装
$ curl -fsSL get.docker.com -o get-docker.sh
# 可能会出现 404 错误,请移步下面的特别说明
$ sudo sh get-docker.sh --mirror AzureChinaCloud
1.3启动Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
1.5建立 docker 用户组
默认情况下,docker
命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root
用户和 docker
组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root
用户。因此,更好地做法是将需要使用 docker
的用户加入 docker
用户组。
建立 docker
组:
$ sudo groupadd docker
将当前用户加入 docker 组:
$ sudo usermod -aG docker $USER
1.6测试 Docker 是否安装正确
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
1.7# Docker 镜像加速器( 视频)
以 Docker 官方加速器为例。Docker 官方提供的中国 registry mirror
#Ubuntu 16.04+、Debian 8+、CentOS 7
对于使用 systemd 的系统,请在 /etc/docker/daemon.json
中写入如下内容(如果文件不存在请新建该文件)
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。
之后重新启动服务。
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
1.8检查加速器是否生效
配置加速器之后,如果拉取镜像仍然十分缓慢,请手动检查加速器配置是否生效,在命令行执行 docker info,如果从结果中看到了如下内容,说明配置成功。
Registry Mirrors:
https://registry.docker-cn.com/
1.9 常用命令
查看 Docker 版本
docker version
从 Docker 文件构建 Docker 映像
docker build -t image-name docker-file-location
运行 Docker 映像
docker run -d image-name
查看可用的 Docker 映像
docker images
查看最近的运行容器
docker ps -l
查看所有正在运行的容器
docker ps -a
停止运行容器
docker stop container_id
删除一个镜像
docker rmi image-name
删除所有镜像
docker rmi $(docker images -q)
强制删除所有镜像
docker rmi -r $(docker images -q)
删除所有虚悬镜像
docker rmi $(docker images -q -f dangling=true)
删除所有容器
docker rm $(docker ps -a -q)
进入 Docker 容器
docker exec -it container-id /bin/bash
查看所有数据卷
docker volume ls
删除指定数据卷
docker volume rm [volume_name]
删除所有未关联的数据卷
docker volume rm $(docker volume ls -qf dangling=true)
从主机复制文件到容器
sudo docker cp host_path containerID:container_path
从容器复制文件到主机
sudo docker cp containerID:container_path host_path