Docker学习使用01

安装

官网地址:https://docs.docker.com/engine/install/centos/

1.卸载旧版本

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2.需要的安装包

yum install -y yum-utils

3.设置镜像的仓库

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
## 上面这个是外网的地址,可能会很慢,建议用国内的镜像
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
## 建议用下面阿里的
yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4.更新yum软件包索引

yum makecache fast

5.安装docker

yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

6.启动docker

systemctl start docker

7.查看docker version

docker version

7.hello-world

docker run hello-world
## 没有hello-world镜像回去拉取

8.查看hello-world镜像

docker images

卸载

yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

Docker 命令

https://docs.docker.com/engine/reference/run/

帮助命令

docker version
docker info
docker 命令 --help 

镜像命令

查看镜像 docker images

docker images [OPTIONS] [REPOSITORY[:TAG]]
#参数 -a 显示所有
#参数 -q 只显示id

docker search mysql --filter=stars=5000
# 查询关键字mysql,过滤条件为Stars不区分大小写,5000以上的

下载镜像 docker pull

docker pull mysql
Using default tag: latest #默认标签使用
latest: Pulling from library/mysql
0ed027b72ddc: Pull complete #分层下载
0296159747f1: Pull complete 
3d2f9b664bd3: Pull complete 
df6519f81c26: Pull complete 
36bb5e56d458: Pull complete 
054e8fde88d0: Pull complete 
f2b494c50c7f: Pull complete 
132bc0d471b8: Pull complete 
135ec7033a05: Pull complete 
5961f0272472: Pull complete 
75b5f7a3d3a4: Pull complete 
Digest: sha256:3d7ae561cf6095f6aca8eb7830e1d14734227b1fb4748092f2be2cfbccf7d614 #签名
Status: Downloaded newer image for mysql:latest 
docker.io/library/mysql:latest #实际地址

########################################################
docker pull mysql
#等价于
docker pull docker.io/library/mysql:latest

删除镜像 docker rmi -f [imagesid]

docker rmi -f $(docker images -aq)

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习

docker pull centos

新建容器并启动

docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 
-d            后台运行方式
-it           使用交互方式运行,进入容器查看内容
-p            指定容器的端口 -p 主机端口:容器端口
-P            随机指定端口

启动并进入容器

[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker run -it centos /bin/bash 
[root@7e491d32442c /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

退出容器,停止运行

exit

退出容器,不停止运行

Ctrl+P+Q

列出所有容器

docker ps
docker ps -a # 列出所有容器,包括已停止的

删除容器

docker rm 容器id 
docker rm -f $(docker ps -aq)   #-f为强制删除
docker ps -a -q|xargs docker rm #删除所有容器

启动和停止容器

docker start 容器id
docker stop 容器id
docker restart 容器id
docker kill 容器id

其他命令

查看日志

docker logs -f -t --tail 10 容器id

查看进程信息

docker top 容器id

查看镜像元数据

docker inspect 容器id

进入当前正在运行的容器

docker exec -it 容器id bashShell
docker exec -it 7e491d32442c /bin/bash

docker attach 容器id
docker attach 7e491d32442c

拷贝容器内文件

注意,容器不运行也没有关系

docker cp 容器id:容器path   

[root@7e491d32442c /]# cd home 
[root@7e491d32442c home]# touch test.java
[root@7e491d32442c home]# ls
test.java
[root@7e491d32442c home]# lsread escape sequence
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker cp 7e491d32442c:/home/test.java /home
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# ls
1.txt  test1  tool
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# cd /home
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# ls
jdk-8u202-linux-x64.rpm  temp  test.java  website
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# 

Commit

docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像:[Tag]
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS        PORTS                                        NAMES
cbc4b01db557   portainer/portainer   "/portainer"             27 hours ago   Up 27 hours   8000/tcp, 9443/tcp, 0.0.0.0:8088->9000/tcp   funny_ritchie
bb5e2e32b8c7   tomcat:9.0            "catalina.sh run"        28 hours ago   Up 28 hours   0.0.0.0:8081->8080/tcp                       tomcat01
c6e16eb5fde2   nginx                 "/docker-entrypoint.…"   28 hours ago   Up 28 hours   0.0.0.0:8080->80/tcp                         nginx01
0b0c75786a4c   centos                "/bin/bash"              30 hours ago   Up 30 hours                                                silly_kirch
7e491d32442c   centos                "/bin/bash"              31 hours ago   Up 30 hours                                                elastic_torvalds
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker exec -it ^C
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker exec -it 24849d4d07c1 /bin/bash
Error: No such container: 24849d4d07c1
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker exec -it bb5e2e32b8c7 /bin/bash
root@bb5e2e32b8c7:/usr/local/tomcat# cd webapps
root@bb5e2e32b8c7:/usr/local/tomcat/webapps# ls
root@bb5e2e32b8c7:/usr/local/tomcat/webapps# cd..
bash: cd..: command not found
root@bb5e2e32b8c7:/usr/local/tomcat/webapps# cd ..
root@bb5e2e32b8c7:/usr/local/tomcat# ls
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  native-jni-lib  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  webapps  webapps.dist  work
root@bb5e2e32b8c7:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@bb5e2e32b8c7:/usr/local/tomcat# cd webapps
root@bb5e2e32b8c7:/usr/local/tomcat/webapps# ls
docs  examples  host-manager  manager  ROOT
root@bb5e2e32b8c7:/usr/local/tomcat/webapps# exit
exit
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS        PORTS                                        NAMES
cbc4b01db557   portainer/portainer   "/portainer"             27 hours ago   Up 27 hours   8000/tcp, 9443/tcp, 0.0.0.0:8088->9000/tcp   funny_ritchie
bb5e2e32b8c7   tomcat:9.0            "catalina.sh run"        28 hours ago   Up 28 hours   0.0.0.0:8081->8080/tcp                       tomcat01
c6e16eb5fde2   nginx                 "/docker-entrypoint.…"   28 hours ago   Up 28 hours   0.0.0.0:8080->80/tcp                         nginx01
0b0c75786a4c   centos                "/bin/bash"              31 hours ago   Up 31 hours                                                silly_kirch
7e491d32442c   centos                "/bin/bash"              31 hours ago   Up 31 hours                                                elastic_torvalds
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker commit -a="zzy" -m="add webapps" bb5e2e32b8c7 tomcat@2
invalid reference format
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker commit -a="zzy" -m="add webapps" bb5e2e32b8c7 tomcat@2:1.0
invalid reference format
[root@iZ8vb9gbgzbq4vx5w96rj7Z ~]# docker commit -a="zzy" -m="add webapps" bb5e2e32b8c7 tomcat2:1.0
sha256:09f76771f2fb94e8cc16c4c06be9103864dcd4bd24ed14bb13b4dc76c1bee181

实例

安装nginx

[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        17913     [OK]       
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   181                  
bitnami/nginx                                     Bitnami nginx Docker Image                      149                  [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   73                   
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   23                   [OK]
rancher/nginx-ingress-controller                                                                  11                   
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   4                    
ibmcom/nginx-ingress-controller                   Docker Image for IBM Cloud Private-CE (Commu…   4                    
bitnami/nginx-exporter                                                                            3                    
bitnami/nginx-ldap-auth-daemon                                                                    3                    
rancher/nginx                                                                                     2                    
circleci/nginx                                    This image is for internal use                  2                    
rancher/nginx-ingress-controller-defaultbackend                                                   2                    
rapidfort/nginx                                   RapidFort optimized, hardened image for NGINX   2                    
vmware/nginx                                                                                      2                    
vmware/nginx-photon                                                                               1                    
bitnami/nginx-intel                                                                               1                    
wallarm/nginx-ingress-controller                  Kubernetes Ingress Controller with Wallarm e…   1                    
rapidfort/nginx-ib                                RapidFort optimized, hardened image for NGIN…   0                    
rancher/nginx-conf                                                                                0                    
rapidfort/nginx-official                          RapidFort optimized, hardened image for NGIN…   0                    
ibmcom/nginx-ppc64le                              Docker image for nginx-ppc64le                  0                    
rancher/nginx-ssl                                                                                 0                    
ibmcom/nginx-ingress-controller-ppc64le           Docker Image for IBM Cloud Private-CE (Commu…   0                    
continuumio/nginx-ingress-ws                                                                      0                    
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
3f4ca61aafcd: Pull complete 
50c68654b16f: Pull complete 
3ed295c083ec: Pull complete 
40b838968eea: Pull complete 
88d3ab68332d: Pull complete 
5f63362a3fa3: Pull complete 
Digest: sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker run -d --name nginx01 -p 8080:80 nginx
c6e16eb5fde2a687d8c4b41cc2bb5d2bb8f425ac142f1f687b15cb4f9794e292
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# 

安装tomcat

[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker pull tomcat:9.0
9.0: Pulling from library/tomcat
6e3729cf69e0: Pull complete 
4d8d923227d8: Pull complete 
eda8241fd25f: Pull complete 
35dccabde73d: Pull complete 
978c906bcdda: Pull complete 
542de2e3d783: Pull complete 
3294a3c6321f: Pull complete 
Digest: sha256:39cb3ef7ca90051f969c58137b9439eb2e81fcfca38f1d09f9cb5ca3d1b7bc93
Status: Downloaded newer image for tomcat:9.0
docker.io/library/tomcat:9.0
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    1403e55ab369   2 weeks ago     142MB
tomcat        9.0       24849d4d07c1   4 weeks ago     476MB
mysql         latest    7484689f290f   4 weeks ago     538MB
hello-world   latest    feb5d9fea6a5   15 months ago   13.3kB
centos        latest    5d0da3dc9764   15 months ago   231MB
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker run -d -p 8081:8080 --name tomcat01 tomcat:9.0
bb5e2e32b8c76ee0e47fecb5532413a5e103b62645878947c455f534b4723c9f
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# curl localhost:8081
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.70</h3></body></html>[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# 

安装ES

# 注意需要加内存限制,不然内存太小可能直接崩掉
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker run -d --name elasticsearch3  -p 9200:9200 -p9300:9300 -e"discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m"  elasticsearch:7.6.2
59c49905ad53dff5ceaf82385a03383988e7cd121271d2f488c7fe2bf2828e54
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                            NAMES
59c49905ad53   elasticsearch:7.6.2   "/usr/local/bin/dock…"   11 seconds ago   Up 10 seconds   0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   elasticsearch3
bb5e2e32b8c7   tomcat:9.0            "catalina.sh run"        29 minutes ago   Up 29 minutes   0.0.0.0:8081->8080/tcp                           tomcat01
c6e16eb5fde2   nginx                 "/docker-entrypoint.…"   44 minutes ago   Up 44 minutes   0.0.0.0:8080->80/tcp                             nginx01
0b0c75786a4c   centos                "/bin/bash"              3 hours ago      Up 3 hours                                                       silly_kirch
7e491d32442c   centos                "/bin/bash"              4 hours ago      Up 3 hours                                                       elastic_torvalds
[root@iZ8vb9gbgzbq4vx5w96rj7Z home]# curl localhost:9200
{
  "name" : "59c49905ad53",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "GNQzypIZTs6GOGkfSw2EKw",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

可视化

Portainer

docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

Rancher (CI/CD再用)

posted @ 2023-01-07 21:27  花茶冰糖  阅读(50)  评论(0编辑  收藏  举报