Docker 学习笔记

来源:互联网 发布:怎么投诉淘宝客服 编辑:程序博客网 时间:2024/06/03 21:15

() 内是必填项

[] 内是选填项

一.安装和配置

1.安装

系统: Centos 7.x

yum install -y docker

在Windows中安装可以参考

二.镜像

1. 获取镜像

docker pull (镜像名称)

在国内pull获取镜像可能会很慢,可以使用 阿里云 加速器

2.查看镜像信息

docker images
[root@localhost ~]# docker imagesREPOSITORY                                                    TAG                 IMAGE ID            CREATED             SIZEdocker.io/ubuntu                                              latest              c73a085dc378        2 weeks ago         127 MBregistry.cn-hangzhou.aliyuncs.com/michael-nijiaqi/my-ubuntu   latest              c73a085dc378        2 weeks ago         127 MBubuntu                                                        latest              c73a085dc378        2 weeks ago         127 MBdocker.io/registry                                            latest              541a6732eadb        2 weeks ago         33.27 MB

3.给镜像创建一个TAG

docker (镜像名[:TAG名] 生成的TAG名称)

4.查看镜像详情

docker inspect (镜像ID)
[root@localhost ~]# docker inspect c73a085dc378[    {        "Id": "sha256:c73a085dc3782b3fd4c032971c76d6afb45fa3728a048175c8c77d7403de5f21",        "RepoTags": [            "docker.io/ubuntu:latest",            "registry.cn-hangzhou.aliyuncs.com/michael-nijiaqi/my-ubuntu:latest",            "ubuntu:latest"        ],        ....]

5.搜索镜像

docker search (搜索内容)
[root@localhost ~]# docker search gccINDEX       NAME                                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDdocker.io   docker.io/gcc                            The GNU Compiler Collection is a compiling...   151       [OK]docker.io   docker.io/frolvlad/alpine-gcc            The smallest Docker image with gcc and g++...   5                    [OK]docker.io   docker.io/sojournlabs/gcc                Smaller alternative to the "official" gcc ...   2                    [OK]docker.io   docker.io/cmbant/docker-gcc-build        gfortran built from gcc6 git source (trunk)     1                    [OK]docker.io   docker.io/dockercore/gcc                 GCC image                                       1                    [OK]docker.io   docker.io/firemiles/arm-linux-gcc        Provide image with arm-linux-gcc.               1                    [OK]docker.io   docker.io/heliostech/jenkins-slave-gcc   Jenkins slave configured to compile C and ...   1                    [OK]docker.io   docker.io/hugome/gcc-multilib            GCC With multilib for i386 compilation          1                    [OK]docker.io   docker.io/msaraiva/elixir-gcc            Minimal image to compile Elixir applicatio...   1                    [OK]docker.io   docker.io/3dcube/3dcube-gcc              3dcube-gcc                                      0                    [OK]docker.io   docker.io/aliem/gcc                      A container to build stuff inside an Alpin...   0                    [OK]docker.io   docker.io/domeos/gcc                     GCC docker image                                0                    [OK]docker.io   docker.io/glportal/whale-gcc             gcc build environment with glportal depend...   0                    [OK]docker.io   docker.io/gorzechowski/gcc               gcc, g++, make                                  0                    [OK]docker.io   docker.io/kdeenanauth/gcc                A minimal docker image to compile and run ...   0                    [OK]docker.io   docker.io/ksimple/linaro-multilib-gcc    linaro-multilib-gcc                             0                    [OK]docker.io   docker.io/manuchen/ubt-gcc               gcc based on ubuntu                             0                    [OK]docker.io   docker.io/pataquets/gcc                  gcc                                             0                    [OK]docker.io   docker.io/pschiffe/docker101-gcc         GCC example Docker image built on Fedora.       0                    [OK]docker.io   docker.io/rferraro/cxx-gcc               Base image for GCC CI workflows                 0                    [OK]docker.io   docker.io/rubdos/fedora-gcc              Fedora 23 with gcc, cmake, git                  0                    [OK]docker.io   docker.io/sjackman/linuxbrew-gcc                                                         0                    [OK]docker.io   docker.io/sjackman/linuxbrew-gcc-deps                                                    0                    [OK]docker.io   docker.io/tibux/slackware-gcc                                                            0                    [OK]docker.io   docker.io/walberla/buildenv-ubuntu-gcc   waLBerla build environment using GCC            0                    [OK]

6.删除镜像

docker rmi (IMAGE名称或者IAMGE ID)

7.创建镜像

1) 通过容器创建

docker commit -m ("提交信息") -a ("作者信息") (容器ID) (生成后的IAMGE名称)

2)通过模板导入

cat exampleimage.tgz | sudo docker import- exampleimagelocal:new

8.存入和载入镜像

1)存入

docker save -o (tar文件名) (IMAGE名称)

2)取出

docker load --input (文件名)

三.容器

1.创建容器

1)新建容器

docker create -it (镜像名[:TAG])

创建一个指定名字的BASH容器

[root@localhost ~]# docker run --name my_container -i -t ubuntu /bin/bashroot@7ff7e1cb1d4f:/# exit
[root@localhost ~]# docker create -it ubuntu:latest08db97602bbc0126dbf9259a4e20af22a92c5ceae300965eaa21dec3aa24be74

2)新建并启动容器

docker run ubuntu [选项] (镜像名) [COMMAND] [ARG...]

3)守护态运行

使用 -d 选项

守护态启动一个容器:

[root@localhost ~]# docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"72e98ee536a031693d29a3f7ec0bc35b3b44531b945fa1151d8a7d72ad13fd7c

查看容器ID和容器的输出信息:

[root@localhost ~]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES72e98ee536a0        ubuntu              "/bin/sh -c 'while tr"   6 seconds ago       Up 5 seconds                            desperate_wing[root@localhost ~]# docker logs 72e98ee536a0hello worldhello worldhello worldhello worldhello worldhello worldhello world...

2.终止容器

查看所有的容器:

docker ps -a

查看正在运行的容器:

docker ps

查看未运行的容器:

docker ps -a -q

终止一个运行中的容器:

docker stop (IMAGE名称)

它会首先向容器发送SIGTERM信号,等待一段时间后(默认10秒),再发送SIGKILL信号终止容器。

启动一个已经终止的容器

docker start (IMAGE名称)

重启一个容器

docker restart (IMAGE名称)

3.进入容器

docker exec [选项] (容器名称) (执行的命令) [ARG..]
[root@localhost ~]# docker run -idt ubuntu4f70663ef12aa33a1ddd8db660f5d3211360247e39b9b843fed7897db225293d[root@localhost ~]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES4f70663ef12a        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            furious_snyder[root@localhost ~]# docker exec -ti 4f70663ef12aa33a1ddd8db660f5d3211360247e39b9b843fed7897db225293d  /bin/bashroot@4f70663ef12a:/#root@4f70663ef12a:/# exit

4.删除容器

docker rm [选项] (容器名)

-f, –force=false 强行终止并删除一个运行中的容器

-l, –link=false 删除容器的连接,但保留容器
-v, –volumes=false 删除

杀死所有正在运行的容器

docker kill $(docker ps -a -q)

删除所有已经停止的容器

docker rm $(docker ps -a -q)

删除所有未打 dangling 标签的镜像

docker rmi $(docker images -q -f dangling=true)

删除所有镜像

docker rmi $(docker images -q)

5)导入和导出容器

1)导出容器

docker export (容器名)
docker export ce5 > test_for_run.tar

2)导入容器

cat test_for_run.tar | docker import - test/ubuntu:v1.0

四.数据管理

0 0
原创粉丝点击