docker 1.2 之docker基本用法

虚幻大学 xuhss 235℃ 0评论

? 优质资源分享 ?

学习路线指引(点击解锁) 知识定位 人群定位
? Python实战微信订餐小程序 ? 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
?Python量化交易实战? 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

1.docker的基本用法

镜像相关操作:dockerhub查找镜像,例如查找centos的镜像

[root@elk ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 7142 [OK] 
centos/systemd                               systemd enabled base container.                 108 [OK]
centos/mysql-57-centos7                      MySQL 5.7 SQL database server                   94 
centos/postgresql-96-centos7                 PostgreSQL is an advanced Object-Relational …   45 
kasmweb/centos-7-desktop                     CentOS 7 desktop for Kasm Workspaces            19 
kasmweb/core-centos-7                        CentOS 7 base image for Kasm Workspaces         3 
continuumio/centos5\_gcc5\_base                                                                3 
couchbase/centos7-systemd                    centos7-systemd images with additional debug…   0 [OK]
datadog/centos-i386                                                                          0 
couchbase/centos-72-jenkins-core                                                             0 
couchbase/centos-72-java-sdk                                                                 0 
fnndsc/centos-python3                        Source for a slim Centos-based Python3 image…   0 [OK]
bitnami/centos-extras-base                                                                   0 
couchbase/centos-69-sdk-build                                                                0 
couchbase/centos-70-sdk-build                                                                0 
spack/centos6                                CentOS 6 with Spack preinstalled                0 
spack/centos-stream                                                                          0 
starlingx/stx-centos                         StarlingX centos                                0 
ibmcom/fhe-toolkit-centos-amd64              The IBM Fully Homomorphic Encryption (FHE) T…   0 
ibmcom/fhe-toolkit-centos                    The IBM Fully Homomorphic Encryption (FHE) T…   0 
apache/couchdbci-centos                      Apache CouchDB CI CentOS                        0 
silintl/openldap                             OpenLDAP base image on Centos 6                 0 [OK]
bitnami/centos-base-buildpack                Centos base compilation image                   0 [OK]
spack/centos7                                CentOS 7 with Spack preinstalled                0 
couchbase/centos-69-sdk-nodevtoolset-build                                                   0                    

2.下载镜像,下载centos的

[root@elk ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

3.查看本地镜像:

[root@elk ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 8 months ago   231MB

4.把镜像做成离线压缩包,格式:docker save -o 压缩包格式 压缩包名

[root@elk ~]# docker save -o centos.tar.gz centos

5.解压离线镜像包

[root@elk ~]# docker load -i centos.tar.gz
Loaded image: centos:latest

6.删除镜像

[root@elk ~]# docker rmi -f centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff8

7.启动docker

(1)以交互式方式启动并进去容器

[root@elk ~]# docker run --name=hello centos /bin/bash

输入exit,退出容器,退出之后容器也会停止,不会再前台运行

docker run运行并创建容器

--name 容器的名字
-i 交互式
-d 后台运行docker
-t 分配伪终端
centos: 启动docker需要的镜像
/bin/bash说明你的shell类型为bash

(2)以守护进程方式启动容器

[root@elk ~]# docker run --name=zhao -td centos  /bin/bash
4c74154517e15ed57c9066789bbde0b01c9f67fd72366e1e84dfb7e40114b443

8.查看容器是否启动

[root@elk ~]# docker ps |grep zhao
4c74154517e1 centos "/bin/bash"   47 seconds ago   Up 46 seconds

9.登录到容器中

[root@elk ~]# docker exec -it zhao /bin/bash
[root@4c74154517e1 /]#          显示前面主机名更换了说明就是登录到容器中了 exit                            **#退出容器**
[root@elk ~]# docker ps **#查看正在运行的容器**
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c74154517e1 centos "/bin/bash" 3 minutes ago Up 3 minutes 

[root@elk ~]# docker ps -a #查看所有容器,包含正在运行和不在运行的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4c74154517e1 centos "/bin/bash" 4 minutes ago Up 4 minutes zhao19b4cafa3d13 centos "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago hello

[root@elk ~]# docker stop zhao #停止容器

[root@elk ~]# docker start zhao #启动已经停止的容器

[root@elk ~]# docker rm -f hello #删除容器

转载请注明:xuhss » docker 1.2 之docker基本用法

喜欢 (0)

您必须 登录 才能发表评论!