安装及使用docker
//安装docker源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
//安装阿里源
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost yum.repos.d]# yum -y install docker-ce
docker常用操作
参数 | 用途 | 语法 |
---|---|---|
search | 在docker hub中搜索镜像 | docker search 镜像名称 |
pull | 在docker hub中下载镜像到本地 | docker pull 镜像名 |
push | 推送指定镜像到docker镜像服务器 | docker push 本地镜像 |
images | 查看本地所有docker镜像 | docker images |
create | 创建容器但不启动容器 | docker create 参数 镜像名称 |
start | 启动容器 | docker start 容器ID或容器名称 |
stop | 停止容器 | docker stop 容器ID或容器名称 |
restart | 重新启动容器 | docker restart 容器ID或名称 |
run | 创建容器,并运行 | docker run 参数 镜像名 |
history | 查看镜像形成过程 | docker history 本地镜像名 |
build | 通过dockerfile制作镜像 | docker build 参数 镜像名 |
attach | 当前shell连接运行容器 | docker attach 容器名或ID |
commit | 保存当前容器为镜像/快照 | docker commit 容器ID或容器名 新镜像名 |
diff | 查看容器改动 | docker diff 容器ID或容器名称 |
exec | 在容器中执行命令 | docker exec 参数 容器ID或名称 命令 |
logs | 输出当前容器的日志信息 | docker logs 容器ID或名称 |
port | 查看容器的端口映射情况 | docker port 容器ID或名称 |
ps | 列出容器列表 | docker ps 参数 |
rm | 删除容器 | docker rm 参数 容器ID或名称 |
rmi | 删除本地镜像 | docker rmi 镜像名 |
kill | 杀死正在运行的容器 | docker kill 参数 容器ID或名称 |
info | 查看docker系统信息 | docker info |
inspect | 查看容器详细信息 | docker inspect 容器ID或名称 |
tag | 镜像打标签 | docker tag 镜像名:tag 新镜像名:tag |
//常用参数
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
命令使用实例
搜索镜像
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13662 [OK] //官方的
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1866 [OK] //个人的
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM
下载镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bf5952930446: Pull complete
cb9a6de05e5a: Pull complete
9513ea0afb93: Pull complete
b49ea07d2e93: Pull complete
a5e4a503d449: Pull complete
Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4bb46517cac3 12 days ago 133MB
创建容器但不启动容器
[root@localhost ~]# docker create 4bb46517cac3
WARNING: IPv4 forwarding is disabled. Networking will not work.
a15189bd5d85dee7785bd8dba3817a3fdaf1c226e4f2d49dc9a7cc451947042e
查看容器
[root@localhost ~]# docker ps //查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a //查看所有的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a15189bd5d85 4bb46517cac3 "/docker-entrypoint.…" 19 seconds ago Created peaceful_ardinghelli
启动容器
[root@localhost ~]# docker start a15189bd5d85
a15189bd5d85
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a15189bd5d85 4bb46517cac3 "/docker-entrypoint.…" 4 minutes ago Up 3 seconds 80/tcp peaceful_ardinghelli
停止容器
[root@localhost ~]# docker stop a15189bd5d85
a15189bd5d85
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM
重启容器
[root@localhost ~]# docker restart a15189bd5d85
a15189bd5d85
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a15189bd5d85 4bb46517cac3 "/docker-entrypoint.…" 6 minutes ago Up 4 seconds 80/tcp peaceful_ardinghelli
创建容器,并运行
//运行
[root@localhost ~]# docker run nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
//创建容器,并运行
[root@localhost ~]# docker run httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
bf5952930446: Already exists
3d3fecf6569b: Pull complete
b5fc3125d912: Pull complete
679d69c01e90: Pull complete
76291586768e: Pull complete
Digest: sha256:3cbdff4bc16681541885ccf1524a532afa28d2a6578ab7c2d5154a7abc182379
Status: Downloaded newer image for httpd:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.5. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.5. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 27 00:17:09.332465 2020] [mpm_event:notice] [pid 1:tid 140257087710336] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Thu Aug 27 00:17:09.332640 2020] [core:notice] [pid 1:tid 140257087710336] AH00094: Command line: 'httpd -D FOREGROUND'
进入容器
退出会自动关闭容器
[root@localhost ~]# docker attach 33bb53582d4d
^C[Thu Aug 27 00:31:08.433729 2020] [mpm_event:notice] [pid 1:tid 139690621809792] AH00491: caught SIGTERM, shutting down
在容器中执行命令
[root@localhost ~]# docker exec -it 33bb53582d4d /bin/bash
root@33bb53582d4d:/usr/local/apache2# ls
bin cgi-bin error icons logs
build conf htdocs include modules
root@33bb53582d4d:/usr/local/apache2# exit
exit
[root@localhost ~]#
查看日志
[root@localhost ~]# docker logs 33bb53582d4d
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.5. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.5. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 27 00:17:09.332465 2020] [mpm_event:notice] [pid 1:tid 140257087710336] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Thu Aug 27 00:17:09.332640 2020] [core:notice] [pid 1:tid 140257087710336] AH00094: Command line: 'httpd -D FOREGROUND'
[Thu Aug 27 00:18:48.703474 2020] [mpm_event:notice] [pid 1:tid 140257087710336] AH00491: caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 27 00:22:42.818763 2020] [mpm_event:notice] [pid 1:tid 140414541927552] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Thu Aug 27 00:22:42.819649 2020] [core:notice] [pid 1:tid 140414541927552] AH00094: Command line: 'httpd -D FOREGROUND'
[Thu Aug 27 00:22:51.206232 2020] [mpm_event:notice] [pid 1:tid 140414541927552] AH00491: caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 27 00:30:25.264564 2020] [mpm_event:notice] [pid 1:tid 139690621809792] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Thu Aug 27 00:30:25.279184 2020] [core:notice] [pid 1:tid 139690621809792] AH00094: Command line: 'httpd -D FOREGROUND'
[Thu Aug 27 00:31:08.433729 2020] [mpm_event:notice] [pid 1:tid 139690621809792] AH00491: caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 27 00:32:06.170493 2020] [mpm_event:notice] [pid 1:tid 140333091947648] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Thu Aug 27 00:32:06.170595 2020] [core:notice] [pid 1:tid 140333091947648] AH00094: Command line: 'httpd -D FOREGROUND'
杀死正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33bb53582d4d httpd "httpd-foreground" 18 minutes ago Up 3 minutes 80/tcp quizzical_robinson
[root@localhost ~]# docker kill 33bb53582d4d
33bb53582d4d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
删除容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33bb53582d4d httpd "httpd-foreground" 20 minutes ago Exited (137) 2 minutes ago quizzical_robinson
8449d4b1401c nginx "/docker-entrypoint.…" 23 minutes ago Up 18 seconds 80/tcp reverent_hellman
8b1de4ee38d2 nginx "/docker-entrypoint.…" 24 minutes ago Exited (0) 16 minutes ago silly_robinson
18bb3f719886 nginx "/docker-entrypoint.…" 25 minutes ago Exited (0) 25 minutes ago unruffled_albattani
a15189bd5d85 4bb46517cac3 "/docker-entrypoint.…" 34 minutes ago Exited (0) 15 minutes ago peaceful_ardinghelli
[root@localhost ~]# docker rm 8b1de4ee38d2
8b1de4ee38d2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8449d4b1401c nginx "/docker-entrypoint.…" 24 minutes ago Up 43 seconds 80/tcp reverent_hellman
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33bb53582d4d httpd "httpd-foreground" 21 minutes ago Exited (137) 2 minutes ago quizzical_robinson
8449d4b1401c nginx "/docker-entrypoint.…" 24 minutes ago Up 48 seconds 80/tcp reverent_hellman
18bb3f719886 nginx "/docker-entrypoint.…" 26 minutes ago Exited (0) 26 minutes ago unruffled_albattani
a15189bd5d85 4bb46517cac3 "/docker-entrypoint.…" 35 minutes ago Exited (0) 16 minutes ago peaceful_ardinghelli
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8449d4b1401c nginx "/docker-entrypoint.…" 24 minutes ago Up 57 seconds 80/tcp reverent_hellman
[root@localhost ~]# docker rm 8449d4b1401c
Error response from daemon: You cannot remove a running container 8449d4b1401cded184247ac06979ed5e03f5baed046a2da609153e65d98989f5. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm -f 8449d4b1401c //强制删除
8449d4b1401c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33bb53582d4d httpd "httpd-foreground" 22 minutes ago Exited (137) 3 minutes ago quizzical_robinson
18bb3f719886 nginx "/docker-entrypoint.…" 26 minutes ago Exited (0) 26 minutes ago unruffled_albattani
a15189bd5d85 4bb46517cac3 "/docker-entrypoint.…" 35 minutes ago Exited (0) 17 minutes ago peaceful_ardinghelli
查看容器的详细信息
[root@localhost ~]# docker inspect 33bb53582d4d
[
{
"Id": "33bb53582d4d698e426f392666bb35a13d88fe9270b17db8f662c09282da3547",
"Created": "2020-08-27T00:17:08.470593045Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2020-08-27T00:32:06.164564317Z",
"FinishedAt": "2020-08-27T00:35:52.937487624Z"
},
······
查看容器改动
[root@localhost ~]# docker diff 33bb53582d4d
C /usr
C /usr/local
C /usr/local/apache2
C /usr/local/apache2/logs
A /usr/local/apache2/logs/httpd.pid
C /root
A /root/.bash_history
3.基于容器制作镜像
[root@localhost ~]# docker run -it busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
61c5ed1cbdf8: Pull complete
Digest: sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977
Status: Downloaded newer image for busybox:latest
/ #
/ #
/ # ls
bin dev etc home proc root sys tmp usr var
/ # mkdir /data
/ # cd /data
/data # echo 'web test' > index.html
/data # exit
修改镜像名字必须是hub docker的账号加仓库名
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> fa3d84733dbb 10 seconds ago 1.22MB
nginx latest 4bb46517cac3 13 days ago 133MB
httpd latest a6ea92c35c43 3 weeks ago 166MB
busybox latest 018c9d7b792b 4 weeks ago 1.22MB
[root@localhost ~]# docker tag fa3d84733dbb zheng1028/httpd:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zheng1028/httpd v0.1 fa3d84733dbb About an hour ago 1.22MB
nginx latest 4bb46517cac3 13 days ago 133MB
httpd latest a6ea92c35c43 3 weeks ago 166MB
busybox latest 018c9d7b792b 4 weeks ago 1.22MB
仓库名叫httpd,所以我们要在Docker Hub上创建一个名为httpd的仓库,然后再将我们做好的镜像push上去
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: zheng1028
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@localhost ~]# docker push zheng1028/httpd:v0.1
The push refers to repository [docker.io/zheng1028/httpd]
536ce50bfa78: Pushed
514c3a3e64d4: Mounted from library/busybox
v0.1: digest: sha256:404037d80d29bbb7780f68d336350296e5e3d30b9e33d04bb1d1915283a9df79 size: 734
使用新生成的镜像创建容器
[root@localhost ~]# docker run --name zzl -it fa3d84733dbb
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # cat /data/index.html
web test
/ #
重新生成镜像并上传
[root@localhost ~]# docker commit -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p zzl zheng1028/httpd:v0.2
sha256:adb55d6e31eab7e4c30d38fda39ab7c57cbb50fec0d60ce9cdd6fdee35914500
[root@localhost ~]# docker push zheng1028/httpd
The push refers to repository [docker.io/zheng1028/httpd]
536ce50bfa78: Layer already exists
514c3a3e64d4: Layer already exists
v0.1: digest: sha256:404037d80d29bbb7780f68d336350296e5e3d30b9e33d04bb1d1915283a9df79 size: 734
dddc139b6bc8: Pushed
536ce50bfa78: Layer already exists
514c3a3e64d4: Layer already exists
v0.2: digest: sha256:07acc651f8a1d25b373c50eb088a597ea2f5331351e25329c8a7d972f49d1d2d size: 941
使用新生成的镜像创建容器
[root@localhost ~]# docker run --name test -d zheng1028/httpd:v0.2
a0a5eb78cf1bbbeccfcaf470940db9e8daa405c20d36c0ece44432f8269d5931
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0a5eb78cf1b zheng1028/httpd:v0.2 "/bin/httpd -f -h /d…" 4 minutes ago Up 4 minutes test
0725ed04d0cb busybox "sh" 2 hours ago Up 2 hours cranky_shannon
3d6933387e0f nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 80/tcp unruffled_noether
6a693d84f7d2 a6ea92c35c43 "httpd-foreground" 3 hours ago Up 2 hours 80/tcp serene_lewin
[root@localhost ~]# curl 172.17.0.5
web test
镜像的导入与导出
在已生成镜像的主机上执行docker save导出镜像
[root@localhost ~]# docker save -o zzl.gz zheng1028/httpd
[root@localhost ~]# ls
anaconda-ks.cfg zzl.gz
在另一台没有镜像的主机上执行docker load导入镜像
[root@localhost ~]# docker load -i zzl.gz