美文网首页
docker容器学习笔记(常用命令记录)

docker容器学习笔记(常用命令记录)

作者: mr_xl | 来源:发表于2018-07-14 20:55 被阅读0次

    在centos7上操作

    一、安装、启动、开机启动、停止docker

    1.Docker 要求 CentOS 系统的内核版本高于 3.10

    查看内核版本
    [root@common-server ~]# uname -r
    3.10.0-693.21.1.el7.x86_64
    注:返回的版本可以安装docker
    
    用yum安装(如果之间安装过,先卸载)
    [root@common-server ~]# yum -y install docker
    
    用wget安装(这里是非root用户)
    [xl@common-server ~]# sudo wget -qO- https://get.docker.com/ | sh
      将将当前用户追加到docker组中(这样执行的时候就不用sudo提权了)
    [xl@common-server ~]# sudo usermod -aG docker xl
    
    

    2.查看是否安装成功

    [root@common-server ~]# docker version
    Client:
     Version:         1.13.1
     API version:     1.26
     Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
     Go version:      go1.9.4
     Git commit:      94f4240/1.13.1
     Built:           Fri May 18 15:44:33 2018
     OS/Arch:         linux/amd64
    

    3.开启docker服务端

    [root@common-server ~]# service docker start
    Redirecting to /bin/systemctl start docker.service
    或者
    [root@common-server ~]# systemctl start docker
    

    4.开启后在查看版本(服务端已经启动了)

    [root@common-server ~]# docker version
    Client:
     Version:         1.13.1
     API version:     1.26
     Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
     Go version:      go1.9.4
     Git commit:      94f4240/1.13.1
     Built:           Fri May 18 15:44:33 2018
     OS/Arch:         linux/amd64
    
    Server:
     Version:         1.13.1
     API version:     1.26 (minimum version 1.12)
     Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
     Go version:      go1.9.4
     Git commit:      94f4240/1.13.1
     Built:           Fri May 18 15:44:33 2018
     OS/Arch:         linux/amd64
     Experimental:    false
    

    5.加入开机启动

    [root@common-server ~]# systemctl enable docker
    

    6.关闭docker服务端

    [root@common-server ~]# service docker stop
    Redirecting to /bin/systemctl stop docker.service
    或者
    [root@common-server ~]# systemctl stop docker
    

    7.网上找的删除docker的方法(没试过,只作为记录)

    方法一:
    yum remove docker docker-common docker-selinux docker-engine -y
    /etc/systemd -name '*docker*' -exec rm -f {} ;
    find /etc/systemd -name '*docker*' -exec rm -f {} \;
    find /lib/systemd -name '*docker*' -exec rm -f {} \;
    
    方法二:
    [root@localhost ~]# yum remove docker
    [root@localhost ~]# yum remove docker-common
    [root@localhost ~]# yum remove container-selinux
      或者
    [root@localhost ~]# yum erase docker
    [root@localhost ~]# yum erase docker-common
    [root@localhost ~]# yum erase container-selinux
    

    二、操作docker

    1.查看镜像(images)

    [root@common-server ~]# docker images
    REPOSITORY                    TAG                 IMAGE ID            CREATED                  SIZE
    docker.io/hello-world         latest              2cb0d9787c4d        Less than a second ago   1.85 kB
    docker.io/jenkins             latest              00b7c903b9e4        2 weeks ago              696 MB
    hub.c.163.com/library/nginx   latest              46102226f2fd        13 months ago            109 MB
    

    2.拉取镜像

    [root@common-server ~]# docker pull hello-world
    Using default tag: latest
    Trying to pull repository docker.io/library/hello-world ...
    latest: Pulling from docker.io/library/hello-world
    9db2ca6ccae0: Pull complete
    Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
    Status: Downloaded newer image for docker.io/hello-world:latest
    
    或者(指定镜像中心地址)
    [root@common-server ~]# docker pull hub.c.163.com/library/nginx:latest
    

    3.运行镜像

    [root@common-server ~]# docker run hello-world
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/engine/userguide/
    

    5.在后台运行镜像,并指定映射端口

    [root@common-server ~]# docker run -d -p 8080:80 hub.c.163.com/library/nginx
    356b6966c727f422dfc39222a306271eeef2d76f421ad34aeab740c9c9b89d7f
    
    使用命令查看监听的端口(8080已经在监听容器中的80端口了)
    [root@common-server ~]# netstat -na|grep 8080
    tcp6       0      0 :::8080                 :::*                    LISTEN
    

    6.查看正在运行的容器

    [root@common-server ~]# docker ps
    CONTAINER ID        IMAGE                         COMMAND                  CREATED              STATUS              PORTS                  NAMES
    356b6966c727        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   dazzling_goldwasser
    

    7.进入容器内部(容器内部类似一个linux系统,可以使用(部分)linux命令)

    [root@common-server ~]# docker exec -it 8a375439629e bash
    root@8a375439629e:/#
    
    在容器内查看正在运行的任务
    root@8a375439629e:/# ps -ef
    
    退出容器
    root@8a375439629e:/# exit
    exit
    

    8.停止容器运行(只需要输入能辨别容器的容器id号码即可,不需要将整个容器id输入完)

    [root@common-server ~]# docker stop 356
    356
    

    9.查询容器

    [root@common-server ~]# docker ps -a
    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                        PORTS               NAMES
    28b62dc4bd54        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   3 hours ago         Exited (0) 20 minutes ago                         tender_noether
    66126ce89b60        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   3 hours ago         Exited (0) 3 hours ago                            pedantic_meninsky
    316f23a0bf35        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   3 hours ago         Exited (0) 3 hours ago                            blissful_lamarr
    36e7f241e390        hello-world                   "/hello"                 4 hours ago         Exited (0) 4 hours ago                            naughty_jones
    dff23de99bd4        docker.io/jenkins             "/bin/tini -- /usr..."   9 hours ago         Exited (143) 20 minutes ago                       jenkins
    

    10.重启容器

    [root@common-server ~]# docker restart 8a375439629e
    8a375439629e
    

    11.删除容器

    [root@common-server ~]# docker rm 214b86ffe5b9
    214b86ffe5b9
    

    12.删除镜像(要先删除用这个镜像的容器)

    [root@common-server ~]# docker rmi docker.io/hello-world
    Untagged: docker.io/hello-world:latest
    Untagged: docker.io/hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
    Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
    Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5
    

    三、制作镜像

    1.创建一个Dockerfile文件,将要发布的应用和这个文件放在一个目录

    文件中的内容
    # 以tomcat为基础
    from hub.c.163.com/library/tomcat:latest
    # 作者信息
    MAINTAINER xxx@163.com
    # 将jpress.war应用放到容器中的tomcat中的/usr/local/tomcat/webapps目录下
    COPY jpress.war /usr/local/tomcat/webapps
    

    1.1 编写Dockerfile(在项目的src同级目录下新建Dockerfile文件,将下面内容填入)

    # 从docker仓库中拉取一个能运行jar程序的环境(jdk)
    FROM java:8-alpine
    
    # 将自己的程序(app.jar)添加到容器中的指定目录中(/usr/local/src目录下)
    ADD target/app.jar /usr/local/src/app.jar
    
    # 指定端口(外部访问时的端口)
    EXPOSE 8761
    
    # 执行命令启动程序
    ENTRYPOINT ["java", "-jar", "/usr/local/src/app.jar"]
    
    

    2.构建镜像(指定镜像名)

    在有Dockerfile的目录下执行docker build命令(-t后面是镜像名和标签,最后有个点表示当前目录)
    
    [root@common-server ~]# docker build -t jpress:latest .
    Sending build context to Docker daemon   286 MB
    Step 1/3 : FROM hub.c.163.com/library/tomcat:latest
     ---> 72d2be374029
    Step 2/3 : MAINTAINER xxx@163.com
     ---> Running in 00ac0fb7ae2c
     ---> 1a0858c478de
    Removing intermediate container 00ac0fb7ae2c
    Step 3/3 : COPY jpress.war /usr/local/tomcat/webapps
     ---> d52a8f8158f0
    Removing intermediate container 494cdef62c83
    Successfully built d52a8f8158f0
    
    使用docker images查看,已经有刚构建的镜像了
    [root@common-server ~]# docker images
    REPOSITORY                     TAG                 IMAGE ID            CREATED                  SIZE
    jpress                         latest              d52a8f8158f0        About a minute ago       313 MB
    
    四.Mysql

    启动mysql容器并设置root密码和创建数据库

    [root@common-server ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=jpress hub.c.163.com/library/mysql
    e5c1d8c219a7de7ab0eb7e59430cc939b07f8d1ca0b93563e1952d03beeadb30
    

    相关文章

      网友评论

          本文标题:docker容器学习笔记(常用命令记录)

          本文链接:https://www.haomeiwen.com/subject/kstupftx.html