docker入门----简单部署及一些常用命令

docker入门----简单部署及一些常用命令

1.docker的简单部署

  • 防火墙及核心功能
[root@localhost ~]# sed -i '7s/enforcing/disabled/' /etc/sysconfig/selinux 
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  • 安装依赖包及安装
[root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum -y install docker-ce
  • 启动docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
  • 镜像加速
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors":["从阿里云的镜像加速获取到的地址"]
}
EOF
  • 重启docker
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
  • 网络优化
[root@localhost ~]# /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl restart docker

2.docker的常用命令

  • 查看版本
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
  • 搜索镜像
[root@localhost ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        12946               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1767                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   764                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   102                                     
bitnami/nginx                      Bitnami nginx Docker Image                      81                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   67                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   50                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co…   47                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        37                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         28                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   23                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  13                                      
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13                                      
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       10                                      
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            6                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   4                                       [OK]
pebbletech/nginx-proxy             nginx-proxy sets up a container running ngin…   2                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
wodby/nginx                        Generic nginx                                   0                                       [OK]
centos/nginx-110-centos7           Platform for running nginx 1.10 or building …   0          
#查找的服务会列出来 stars表示下载量 jwilder/nginx-proxy 表示jwilder写的nginx-proxy
  • 下载镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete 
74cda408e262: Pull complete 
ffadbd415ab7: Pull complete 
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
  • 查看下载的镜像
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
  • 显示下载镜像的详细数据
[root@localhost ~]# docker inspect ed21b7a8aee9
[
    {
        "Id": "sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-03-31T03:19:30.487069362Z",
        "Container": "5c86b143cf5caec0aed5c331922c243f00600152ec0e9ecbe8531771562e72b8",
        "ContainerConfig": {
            "Hostname": "5c86b143cf5c",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.9",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9be1fc3b00d81a66f4de11f5f5bf176e0748434be056cf3152386cc917307e7f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.9",
                "NJS_VERSION=0.3.9",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9be1fc3b00d81a66f4de11f5f5bf176e0748434be056cf3152386cc917307e7f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 126769107,
        "VirtualSize": 126769107,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/6b27023d27e96fe7630bb5483ed4fd3fc475621d5ec86be9ec9475942127e90e/diff:/var/lib/docker/overlay2/79824a896c42c9e5c678d5231d4e320f788d3ec51bb2e83da8f12b426bc9b3c7/diff",
                "MergedDir": "/var/lib/docker/overlay2/06364ab4845a145f702c271df18e2f48b64d0a621e10ea1a911af423015673dc/merged",
                "UpperDir": "/var/lib/docker/overlay2/06364ab4845a145f702c271df18e2f48b64d0a621e10ea1a911af423015673dc/diff",
                "WorkDir": "/var/lib/docker/overlay2/06364ab4845a145f702c271df18e2f48b64d0a621e10ea1a911af423015673dc/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45",
                "sha256:99134ec7f247e5a211c7205fec587bf72a6d4aac339b21858b892e9c04f78920",
                "sha256:d37eecb5b7691ec21bd19989e37f8bb4d20b340a775591d0f3db5897d606b0e4"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
  • 建立相同的镜像
[root@localhost ~]# docker tag nginx:latest nginx:test
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
nginx               test                ed21b7a8aee9        8 days ago          127MB
  • 保存镜像
[root@localhost ~]# docker save -o /opt/nginx nginx:latest
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
containerd  nginx  rh
  • 导入镜像
[root@localhost opt]# docker load < nginx
Loaded image: nginx:latest
  • 查看正在运行的容器
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 删除镜像
[root@localhost home]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
nginx               test                ed21b7a8aee9        8 days ago          127MB
[root@localhost home]# docker rmi nginx:test 
Untagged: nginx:test
[root@localhost home]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
  • 启动容器
[root@localhost home]# docker run -it nginx:latest /bin/bash
root@1a5319a4fa45:/# exit
exit
[root@localhost home]# docker ps -qa
1a5319a4fa45
[root@localhost home]# docker start 1a5319a4fa45
1a5319a4fa45
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1a5319a4fa45        nginx:latest        "/bin/bash"         2 minutes ago       Up 11 seconds       80/tcp              hardcore_meninsky
  • 关闭容器
[root@localhost home]# docker stop 1a5319a4fa45
1a5319a4fa45
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
1a5319a4fa45        nginx:latest        "/bin/bash"         4 minutes ago       Exited (0) 5 seconds ago                       hardcore_meninsky
  • docker的几种状态
created #镜像pull下来,没有加载
up  #容器在运行中
exited  #0表示正常退出 其他数表示异常退出
  • 进入到容器里面
[root@localhost home]# docker run -it nginx:latest /bin/bash
root@1d634837b6c7:/# ls  
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@1d634837b6c7:/# exit
exit
[root@localhost home]# docker start 1a5319a4fa45
1a5319a4fa45
[root@localhost home]# docker exec -it 1a5319a4fa45 /bin/bash
root@1a5319a4fa45:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@1a5319a4fa45:/# exit
exit
  • 获取centos7的镜像,并进行简单操作
[root@localhost home]# docker pull centos:7
7: Pulling from library/centos
ab5ef0e58194: Pull complete 
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@localhost home]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
centos              7                   5e35e350aded        4 months ago        203MB
[root@localhost home]# docker create -it centos:7 /bin/bash
ca9cdcbe215c01788607b37a19844e46fd9b268d88cb6bea68f37cb1c867ef09
[root@localhost home]# docker start ca9cdcbe215c01788607b37a19844e46fd9b268d88cb6bea68f37cb1c867ef09
ca9cdcbe215c01788607b37a19844e46fd9b268d88cb6bea68f37cb1c867ef09
[root@localhost home]# docker ps -q
[root@localhost home]# docker run centos:7 /usr/bin/bash -c ls /
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
88a55e5aa881        centos:7            "/usr/bin/bash -c ls…"   11 seconds ago      Exited (0) 10 seconds ago                       beautiful_clarke
ca9cdcbe215c        centos:7            "/bin/bash"              2 minutes ago       Up 2 minutes                                    sad_satoshi
195abeb364a1        nginx:latest        "/bin/bash"              14 minutes ago      Exited (0) 13 minutes ago                       modest_mclaren
1d634837b6c7        nginx:latest        "/bin/bash"              18 minutes ago      Exited (0) 18 minutes ago                       vigilant_darwin
1a5319a4fa45        nginx:latest        "/bin/bash"              26 minutes ago      Exited (0) 15 minutes ago                       hardcore_meninsky
[root@localhost home]# docker stop ca9cdcbe215c
ca9cdcbe215c
  • 导出和导入容器
[root@localhost home]# docker export ca9cdcbe215c > nginx_c
[root@localhost home]# cat nginx_c | docker import - centos:7
sha256:931c0d8db28ef1b2611d357542a92b26374eef60f962bc409bc95eec035149c8
[root@localhost home]# ls
nginx_c  py
  • 删除容器
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
88a55e5aa881        5e35e350aded        "/usr/bin/bash -c ls…"   5 minutes ago       Exited (0) 5 minutes ago                         beautiful_clarke
ca9cdcbe215c        5e35e350aded        "/bin/bash"              8 minutes ago       Exited (137) 4 minutes ago                       sad_satoshi
195abeb364a1        nginx:latest        "/bin/bash"              20 minutes ago      Exited (0) 19 minutes ago                        modest_mclaren
1d634837b6c7        nginx:latest        "/bin/bash"              24 minutes ago      Exited (0) 23 minutes ago                        vigilant_darwin
1a5319a4fa45        nginx:latest        "/bin/bash"              32 minutes ago      Exited (0) 21 minutes ago                        hardcore_meninsky
[root@localhost home]# docker rm 1a5319a4fa45 
1a5319a4fa45
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
88a55e5aa881        5e35e350aded        "/usr/bin/bash -c ls…"   6 minutes ago       Exited (0) 6 minutes ago                         beautiful_clarke
ca9cdcbe215c        5e35e350aded        "/bin/bash"              8 minutes ago       Exited (137) 4 minutes ago                       sad_satoshi
195abeb364a1        nginx:latest        "/bin/bash"              20 minutes ago      Exited (0) 19 minutes ago                        modest_mclaren
1d634837b6c7        nginx:latest        "/bin/bash"              24 minutes ago      Exited (0) 24 minutes ago                        vigilant_darwin
[root@localhost home]# docker ps -a | awk '{print "docker rm "$1}' | bash
88a55e5aa881
ca9cdcbe215c
195abeb364a1
1d634837b6c7
# docker rm `docker ps -qa`
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
发布了129 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/double_happy111/article/details/105397544
今日推荐