centos7 docker的常规使用

提问:docker的容器是什么?
答:容器就是再隔离环境中运行的一个进程,进程停止,容器就会销毁,隔离的环境拥有自己的系统文件ip,主机名等
进程:正在运行的程序

镜像操作

拉取镜像centos7.6版本

docker pull centos:7.6
监听当前运行的容器
[root@VM_0_2_centos ~]# docker info 
Containers: 3
 Running: 1
 Paused: 0
 Stopped: 2
Images: 2
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: e45dd70447fb72ee4e1f6989173aa6c5dd492d87 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
Kernel Version: 3.10.0-957.21.3.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 2
Total Memory: 3.701 GiB
Name: VM_0_2_centos
ID: 35F3:HXPB:I57V:4XVM:2TO2:7PXU:FXAF:NTI5:J5FE:DUVR:HBDG:PCMT
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
搜索需要使用的镜像
[root@VM_0_2_centos ~]# docker search nginx 
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                              Official build of Nginx.                        12609     [OK]       
docker.io   docker.io/jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1736                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   754                  [OK]
docker.io   docker.io/linuxserver/nginx                  An Nginx container, brought to you by Linu...   91                   
docker.io   docker.io/bitnami/nginx                      Bitnami nginx Docker Image                      75                   [OK]
docker.io   docker.io/tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rt...   62                   [OK]
docker.io   docker.io/jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ...   39                   
docker.io   docker.io/nginxdemos/hello                   NGINX webserver that serves a simple page ...   36                   [OK]
docker.io   docker.io/jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        33                   [OK]
docker.io   docker.io/nginx/unit                         NGINX Unit is a dynamic web and applicatio...   33                   
docker.io   docker.io/nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         23                   
docker.io   docker.io/privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & ...   20                   [OK]
docker.io   docker.io/schmunk42/nginx-redirect           A very simple container to redirect HTTP t...   18                   [OK]
docker.io   docker.io/blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                   [OK]
docker.io   docker.io/centos/nginx-112-centos7           Platform for running nginx 1.12 or buildin...   12                   
docker.io   docker.io/centos/nginx-18-centos7            Platform for running nginx 1.8 or building...   12                   
docker.io   docker.io/nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  12                   
docker.io   docker.io/nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       9                    
docker.io   docker.io/sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats...   6                    [OK]
docker.io   docker.io/mailu/nginx                        Mailu nginx frontend                            5                    [OK]
docker.io   docker.io/bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Con...   4                    [OK]
docker.io   docker.io/pebbletech/nginx-proxy             nginx-proxy sets up a container running ng...   2                    [OK]
docker.io   docker.io/ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                    [OK]
docker.io   docker.io/centos/nginx-110-centos7           Platform for running nginx 1.10 or buildin...   0                    
docker.io   docker.io/wodby/nginx                        Generic nginx                                   0                    [OK]

查看镜像列表

[root@VM_0_2_centos ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              2073e0bcb60e        4 days ago          127 MB
docker.io/ubuntu    latest              ccc6e87d482b        3 weeks ago         64.2 MB
移除nginx镜像
[root@VM_0_2_centos ~]# docker rmi nginx 
Untagged: docker.io/nginx:latest
Deleted: sha256:2073e0bcb60ee98548d313ead5eacbfe16d9054f8800a32bedd859922a99a6e1
Deleted: sha256:a3136fbf38691346715cac8360bcdfca0fff812cede416469653670f04e2cab0
Deleted: sha256:99360ffcb2da18fd9ede194efaf5d4b90e7aee99f45737e918113e6833dcf278
Deleted: sha256:488dfecc21b1bc607e09368d2791cb784cf8c4ec5c05d2952b045b3e0f8cc01e
[root@VM_0_2_centos ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              ccc6e87d482b        3 weeks ago         64.2 MB
镜像导出

导出镜像 -o 导出后的文件名 docker.io/nginx 通过docker images 获取

docker save -o nginx.tar docker.io/nginx 

导入镜像

docker load -i nginx.tar

容器:

允许nginx镜像 内部端口隐射外部端口
docker run -d -p 80:80 镜像名称 //-d 后台运行 -p 内外端口映射
/bin/bash 创建容器成功后执行的语句 必须
下面是我真实使用记录

创建容器

[root@VM_0_2_centos ~]# docker run -d --name hl -it -p 8888:8888 -p 888:888 -p 443:443 docker.io/centos:7 /bin/bash
[root@VM_0_2_centos ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                  NAMES
7e9dd36a45e4        docker.io/ubuntu    "/bin/bash"         About a minute ago   Up 3 seconds        0.0.0.0:8080->80/tcp   ubuntu

列出所有容器

docker container ls -a 

命令等价于docker container ls -a

docker ps -a 

停止容器的运行

docker stop nginx 

运行容器

docker start nginx 

杀死容器

[root@VM_0_2_centos ~]# docker container kill ubuntu
ubuntu

删除nginx容器

docker container rm nginx 

进入容器

尾部必须要加上进入后的初始命令 如果你已经部署过环境和程序那可以初始命令写你程序的运行命令

[root@VM_0_2_centos ~]# docker exec -it ubuntu /bin/bash
root@4287c3a60a23:/# 

静默输出只输出容器id

[root@VM_0_2_centos ~]# docker ps -a -q
a753509ba209
4287c3a60a23
64f4845f5ebe

运行10秒然后死掉

docker run -d centos:6.9 sleep 10 

给容器更换yum源

查看系统版本

[root@VM_0_2_centos ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

下载wget

yum install wget -y

进入yum文件路径

cd /etc/yum.repos.d

删除原始文件不用备份

rm -rf CentOS-Bash.repo

下载阿里云yum源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

然后执行下面两句

yum clean all
yum makecache

你可能感兴趣的:(docker)