官方文档
菜鸟教程
[visitor@bogon ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
[visitor@bogon ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[sudo] password for visitor:
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
设置国内yum源
[visitor@bogon ~]$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
本地缓存
[visitor@bogon ~]$ sudo yum makecache fast
安装docker
[visitor@bogon ~]$ sudo yum -y install docker-ce
Installed:
docker-ce.x86_64 0:18.06.1.ce-3.el7
Dependency Installed:
container-selinux.noarch 2:2.68-1.el7
Dependency Updated:
libsemanage.x86_64 0:2.5-11.el7 libsemanage-python.x86_64 0:2.5-11.el7
policycoreutils.x86_64 0:2.5-22.el7 policycoreutils-python.x86_64 0:2.5-22.el7
selinux-policy.noarch 0:3.13.1-192.el7_5.6 selinux-policy-targeted.noarch 0:3.13.1-192.el7_5.6
setools-libs.x86_64 0:3.3.8-2.el7
Complete!
[visitor@bogon ~]$ docker --version
Docker version 18.06.1-ce, build e68fc7a
[visitor@bogon ~]$ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:03 2018
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[visitor@bogon ~]$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://docs.docker.com
[visitor@bogon ~]$ sudo systemctl start docker
[visitor@bogon ~]$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2018-08-22 17:52:52 CST; 2s ago
Docs: https://docs.docker.com
Main PID: 51339 (dockerd)
Memory: 44.2M
CGroup: /system.slice/docker.service
├─51339 /usr/bin/dockerd
└─51351 docker-containerd --config /var/run/docker/containerd/containerd.toml
配置国内docker镜像源
[visitor@localhost ~]$ sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://9rw3bt31.mirror.aliyuncs.com"]
}
重新加载配置文件,重启docker
[visitor@localhost ~]$ sudo systemctl daemon-reload
[visitor@localhost ~]$ sudo systemctl restart docker
[visitor@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5182e96772bf 3 weeks ago 200MB
[visitor@localhost ~]$ sudo docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal … 638 [OK]
[visitor@localhost ~]$ sudo docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest
[visitor@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5182e96772bf 3 weeks ago 200MB
hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB
[visitor@localhost ~]$ sudo docker inspect hello-world
[
{
"Id": "sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34",
"RepoTags": [
"hello-world:latest"
],
"RepoDigests": [
"hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc"
],
"Parent": "",
"Comment": "",
"Created": "2018-07-11T00:32:08.432822465Z",
"Container": "6b6326f6afc81f7850b74670aad2bf550c7f2f07cd63282160e5eb564876087f",
"ContainerConfig": {
"Hostname": "6b6326f6afc8",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/hello\"]"
],
"ArgsEscaped": true,
"Image": "sha256:6bc48d210ad4c6bbb74e02e6196a9133b57107033c09e92cac12616cad30ebcf",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "17.06.2-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/hello"
],
"ArgsEscaped": true,
"Image": "sha256:6bc48d210ad4c6bbb74e02e6196a9133b57107033c09e92cac12616cad30ebcf",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1848,
"VirtualSize": 1848,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/b20057387017e5498043639186788e554c6d1359ef317254abaa04d23f00f74a/merged",
"UpperDir": "/var/lib/docker/overlay2/b20057387017e5498043639186788e554c6d1359ef317254abaa04d23f00f74a/diff",
"WorkDir": "/var/lib/docker/overlay2/b20057387017e5498043639186788e554c6d1359ef317254abaa04d23f00f74a/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
[visitor@localhost ~]$ sudo docker run --name hello-world-container hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
[visitor@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eabfe2c540e6 hello-world "/hello" 39 seconds ago Exited (0) 39 seconds ago hello-world-container
[visitor@localhost ~]$ sudo docker commit eabfe2c540e6 hello-world-1:latest
sha256:c406848947a484c3cebb7bf7ada30ab2ab9f96af9a7eaa81156d394d5c1b9781
[visitor@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world-1 latest c406848947a4 27 seconds ago 1.85kB
centos latest 5182e96772bf 3 weeks ago 200MB
hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB
[visitor@localhost ~]$ sudo docker save -o hello-world-image.tar hello-world-1:latest && ll
total 20
-rw-------. 1 root root 12800 Aug 29 11:16 hello-world-image.tar
[visitor@localhost ~]$ sudo docker rmi hello-world-1
Untagged: hello-world-1:latest
Deleted: sha256:c406848947a484c3cebb7bf7ada30ab2ab9f96af9a7eaa81156d394d5c1b9781
[visitor@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5182e96772bf 3 weeks ago 200MB
hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB
[visitor@localhost ~]$ sudo chown visitor:visitor hello-world-image.tar && ll
total 20
-rw-------. 1 visitor visitor 12800 Aug 29 11:16 hello-world-image.tar
[visitor@localhost ~]$ sudo docker load < hello-world-image.tar
Loaded image: hello-world-1:latest
[visitor@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world-1 latest c406848947a4 10 minutes ago 1.85kB
centos latest 5182e96772bf 3 weeks ago 200MB
hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB
[visitor@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5182e96772bf 3 weeks ago 200MB
[visitor@localhost ~]$ sudo docker run --name container_centos -itd centos
ffc46f56f0381141482e7e92e88e23c8810e380e23aa2069d8d84b43b0369ba0
--name给容器起个名称,-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。-d让Docker 容器在后台以守护态(Daemonized)形式运行。
[visitor@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ffc46f56f038 centos "bash" 31 seconds ago Up 30 seconds container_centos
[visitor@localhost ~]$ sudo docker exec -it container_centos /bin/bash
[root@ffc46f56f038 /]# pwd
/
[root@ffc46f56f038 /]# exit
exit
[visitor@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ffc46f56f038 centos "bash" 10 minutes ago Up 10 minutes container_centos
[visitor@localhost ~]$ sudo docker restart container_centos
container_centos
[visitor@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ffc46f56f038 centos "bash" 11 minutes ago Up 2 seconds container_centos
[visitor@localhost ~]$ sudo docker stop container_centos
container_centos
[visitor@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ffc46f56f038 centos "bash" 11 minutes ago Exited (137) 2 seconds ago container_centos
[visitor@localhost ~]$ sudo docker top container_centos
UID PID PPID C STIME TTY TIME CMD
root 21652 21636 0 11:49 pts/0 00:00:00 bash
[visitor@localhost ~]$ sudo docker port container_centos
[visitor@localhost ~]$ sudo docker logs container_centos
[root@ffc46f56f038 /]# [root@ffc46f56f038 /]#
[visitor@localhost ~]$ sudo docker cp helloworld.txt container_centos:/root/
[visitor@localhost ~]$ sudo docker exec -it container_centos /bin/bash
[root@ffc46f56f038 /]# ll /root
total 4
-rw-------. 1 root root 3415 Aug 4 22:05 anaconda-ks.cfg
-rw-rw-r--. 1 1000 1000 0 Aug 29 03:53 helloworld.txt
[root@ffc46f56f038 /]# exit
exit
[visitor@localhost ~]$ sudo docker cp container_centos:/root/anaconda-ks.cfg /home/visitor && ll
total 24
-rw-------. 1 root root 3415 Aug 5 06:05 anaconda-ks.cfg
-rw-------. 1 visitor visitor 12800 Aug 29 11:16 hello-world-image.tar
-rw-rw-r--. 1 visitor visitor 0 Aug 29 11:53 helloworld.txt
[visitor@localhost ~]$ sudo docker export -o container_centos_snapshot.tar container_centos && ll
total 203040
-rw-------. 1 root root 3415 Aug 5 06:05 anaconda-ks.cfg
-rw-------. 1 root root 207886848 Aug 29 12:07 container_centos_snapshot.tar
[visitor@localhost ~]$ sudo docker import container_centos_snapshot.tar snapshot:latest
sha256:35167b3b3313dedb94a56e6d37d3b8b60ffb56befd63d6e233f8a0a3e360e138
[visitor@localhost ~]$ sudo docker images snapshot
REPOSITORY TAG IMAGE ID CREATED SIZE
snapshot latest 35167b3b3313 2 seconds ago 200MB
用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。这两者的区别在于容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也要大。此外,从容器快照文件导入时可以重新指定标签等元数据信息。
[visitor@localhost ~]$ sudo docker rm container_centos
container_centos
[visitor@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Dockerfile 创建镜像
[visitor@bogon python3]$ pwd
/home/visitor/Dockerfile/python3
[visitor@bogon python3]$ ll
total 22148
-rw-rw-r--. 1 visitor visitor 544 Aug 23 15:55 Dockerfile
-rw-rw-r--. 1 visitor visitor 22673115 Aug 23 15:39 Python-3.6.3.tgz
[visitor@bogon python3]$ sudo docker build -t python3:latest .
[sudo] password for visitor:
Sending build context to Docker daemon 22.68MB
Step 1/16 : FROM centos
---> 5182e96772bf
Step 2/16 : MAINTAINER liuzhiguo
---> Running in 0ba2e5df130e
Removing intermediate container 0ba2e5df130e
---> 28c4c0bd1f34
Step 3/16 : COPY Python-3.6.3.tgz /root/
---> f500567d601d
Step 4/16 : ADD Python-3.6.3.tgz /usr/local
---> af6e8a02b1a2
Step 5/16 : WORKDIR /usr/local/Python-3.6.3/
---> Running in bf7d353dc833
Removing intermediate container bf7d353dc833
---> 037027b00704
Step 6/16 : RUN yum -y install gcc zlib zlib-devel openssl openssl-devel sqlite sqlite-devel
---> Running in 45b4af43b192
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
Removing intermediate container e08cf491200a
---> e3ef214a680e
Step 10/16 : RUN python -V
---> Running in 5788885cdf94
Python 2.7.5
Removing intermediate container 5788885cdf94
---> a268e901ad2c
Step 11/16 : RUN python2 -V
---> Running in d99a9f8bb808
Python 2.7.5
Removing intermediate container d99a9f8bb808
---> 3b7bd22880ed
Step 12/16 : RUN python3 -V
---> Running in 169afa36624c
Python 3.6.3
Removing intermediate container 169afa36624c
---> c21485c22542
Step 13/16 : RUN pip -V
---> Running in 8e612048df09
/bin/sh: pip: command not found
docker run -itd --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
网速太慢切换镜像源
[visitor@localhost ~]$ sudo mkdir -p /etc/docker
[visitor@localhost ~]$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9rw3bt31.mirror.aliyuncs.com"]
}
EOF
[visitor@localhost ~]$ sudo systemctl daemon-reload
[visitor@localhost ~]$ sudo systemctl restart docker
[visitor@localhost ~]$ sudo docker info
Registry Mirrors:
https://9rw3bt31.mirror.aliyuncs.com/
Live Restore Enabled: false
[visitor@localhost ~]$ bash docker-tags.sh
#!/bin/sh
#
# Simple script that will display docker repository tags.
#
# Usage:
# $ docker-show-repo-tags.sh ubuntu centos
for Repo in $* ; do
curl -s -S "https://registry.hub.docker.com/v2/repositories/library/$Repo/tags/" | \
sed -e 's/,/,\n/g' -e 's/\[/\[\n/g' | \
grep '"name"' | \
awk -F\" '{print $4;}' | \
sort -fu | \
sed -e "s/^/${Repo}:/"
done
[visitor@localhost ~]$ bash docker-tags.sh rabbitmq
rabbitmq:3.7.26
rabbitmq:3.7.26-management
rabbitmq:3.7.27-rc.1
rabbitmq:3.7.27-rc.1-management
rabbitmq:3.8
rabbitmq:3.8-management
rabbitmq:3.8.5
rabbitmq:3.8.5-management
rabbitmq:latest
rabbitmq:management
[visitor@localhost ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
要安装其他版本的 Compose,替换掉1.26.2。
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 638 100 638 0 0 76 0 0:00:08 0:00:08 --:--:-- 190
100 11.6M 100 11.6M 0 0 243k 0 0:00:49 0:00:49 --:--:-- 301k
[visitor@localhost ~]$ sudo chmod +x /usr/local/bin/docker-compose
[visitor@localhost ~]$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[visitor@localhost ~]$ docker-compose --version
docker-compose version 1.26.2, build eefe0d31
其中sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose必须配置,否则运行docker-compose up报错:Couldn't connect to Docker daemon at http+docker://localhost - is it running? 或者运行sudo docker-compose up时找不到命令。