Docker命令学习整理

目录

第一个命令 Hello world

查看docker命令

查看版本 docker --v

查看信息$ docker info

查看帮助docker command --help

Docker 容器

查看容器正在运行的容器 docker ps

查看所有容器 docker ps -a

启动容器 docker run

动态查看容器日志 docker logs -f con_name

重启容器 docker restart <容器ID>

进入容器 docker exec 或 docker attac

停止容器 docker stop <容器ID>

删除容器 docker rm -f <容器ID>

查看容器状统计信息$ docker status

Docker 镜像

查询镜像 docker images

删除镜像 docker rmi image_id

查询镜像 docker search xxx

拉取镜像 docker pull image_name

重命名标签 docker tag

更新容器docker commit


第一个命令 Hello world

$ 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/get-started/

查看docker命令

$ docker

Usage: docker COMMAND

A self-sufficient runtime for containers

Options:

--config string Location of client config files (default

"C:\\Users\\User\\.docker")

-D, --debug Enable debug mode

-H, --host list Daemon socket(s) to connect to

-l, --log-level string Set the logging level

("debug"|"info"|"warn"|"error"|"fatal")

(default "info")
....

查看版本 docker --v

同 dockder -version

$ docker -v

Docker version 18.03.0-ce, build 0520e24302

查看信息$ docker info

$ docker info

Containers: 4

Running: 0

Paused: 0

Stopped: 4

Images: 4

Server Version: 19.03.12

Storage Driver: overlay2

Backing Filesystem: extfs

Supports d_type: true

Native Overlay Diff: true

Logging Driver: json-file

Cgroup Driver: cgroupfs

Plugins:

查看帮助docker command --help

docker command --help

Docker 容器

查看容器正在运行的容器 docker ps

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

查看所有容器 docker ps -a

$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

cbef5a6d099e nginx "/docker-entrypoint." 46 hours ago Exited (255) 26 minutes ago 0.0.0.0:80->80/tcp webserver

8fd78cd3f288 ubuntu "bash" 47 hours ago Exited (127) 47 hours ago beautiful_murdock

133fb7be6b30 hello-world "/hello" 47 hours ago Exited (0) 45 hours ago hopeful_burnell

7537dc6cc044 ubuntu:15.10 "/bin/bash" 2 days ago Exited (255) 2 days ago

启动容器 docker run

-i 表示交互操作

-t 表示终端

ubuntu 镜像 ,冒号后面15.10表示版本

/bin/bash 镜像名称后的命令,表示交互方式为shell

这样就使用-i -t 使容器可以对话可以执行 exit ,或者Ctrl +D 退出

$ docker run -i -t ubuntu:15.10 /bin/bash

root@5bf777a45bb3:/# pwd

/

root@5bf777a45bb3:/# ls -l

total 64

drwxr-xr-x 2 root root 4096 Jul 6 2016 bin

drwxr-xr-x 2 root root 4096 Oct 19 2015 boot

drwxr-xr-x 5 root root 360 Nov 25 06:35 dev

drwxr-xr-x 1 root root 4096 Nov 25 06:34 etc

drwxr-xr-x 2 root root 4096 Oct 19 2015 home

drwxr-xr-x 8 root root 4096 Sep 13 2015 lib

drwxr-xr-x 2 root root 4096 Jul 6 2016 lib64

drwxr-xr-x 2 root root 4096 Jul 6 2016 media

drwxr-xr-x 2 root root 4096 Oct 19 2015 mnt

drwxr-xr-x 2 root root 4096 Jul 6 2016 opt

dr-xr-xr-x 135 root root 0 Nov 25 06:35 proc

drwx------ 2 root root 4096 Jul 6 2016 root

drwxr-xr-x 5 root root 4096 Jul 6 2016 run

drwxr-xr-x 1 root root 4096 Jul 22 2016 sbin

drwxr-xr-x 2 root root 4096 Jul 6 2016 srv

dr-xr-xr-x 13 root root 0 Nov 25 06:35 sys

drwxrwxrwt 2 root root 4096 Jul 6 2016 tmp

drwxr-xr-x 1 root root 4096 Jul 22 2016 usr

drwxr-xr-x 1 root root 4096 Jul 22 2016 var

root@5bf777a45bb3:/# exit

exit

动态查看容器日志 docker logs -f con_name

               docker logs -f con_name

重启容器 docker restart <容器ID>

$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

2a70dd029ef1 hello-world "/hello" 23 hours ago Exited (0) 23 hours ago interesting_panini

ed6f3717d842 ubuntu:15.10 "/bin/bash" 23 hours ago Exited (0) 23 hours ago pensive_varahamihira

5bf777a45bb3 ubuntu:15.10 "/bin/bash" 27 hours ago Exited (0) 27 hours ago lucid_shtern

cbef5a6d099e nginx "/docker-entrypoint." 3 days ago Exited (255) 27 hours ago 0.0.0.0:80->80/tcp webserver

8fd78cd3f288 ubuntu "bash" 3 days ago Exited (127) 3 days ago beautiful_murdock

7537dc6cc044 ubuntu:15.10 "/bin/bash" 3 days ago Exited (255) 3 days ago boring_hoover



$ docker restart 7537dc6cc044

7537dc6cc044

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7537dc6cc044 ubuntu:15.10 "/bin/bash" 3 days ago Up 6 seconds boring_hoover

进入容器 docker exec 或 docker attac

其中

docker attac 使用 exit退出后容器会自动关闭

docker exec 使用 exit退出后容器不会关闭还会运行

$ docker exec -it 7537dc6cc044 /bin/bash

root@7537dc6cc044:/# ls

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@7537dc6cc044:/# exits

bash: exits: command not found

root@7537dc6cc044:/# exit

exit

停止容器 docker stop <容器ID>

$ docker stop 7537dc6cc044

7537dc6cc044

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

删除容器 docker rm -f <容器ID>

$ docker rm -f 2a70dd029ef1

2a70dd029ef1

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ed6f3717d842 ubuntu:15.10 "/bin/bash" 23 hours ago Exited (0) 23 hours ago pensive_varahamihira

5bf777a45bb3 ubuntu:15.10 "/bin/bash" 27 hours ago Exited (0) 27 hours ago lucid_shtern

cbef5a6d099e nginx "/docker-entrypoint." 3 days ago Exited (255) 27 hours ago 0.0.0.0:80->80/tcp webserver

8fd78cd3f288 ubuntu "bash" 3 days ago Exited (127) 3 days ago beautiful_murdock

7537dc6cc044 ubuntu:15.10 "/bin/bash" 3 days ago Exited (0) About a minute ago boring_hoover

查看容器状统计信息$ docker status

用于显示一个或者多个容器的统计信息,例如 CUP,内存,网络IO的新能和指标

$ docker status



CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS

36d6a8480986 demo 0.12% 1.098MiB / 985.4MiB 0.11% 1.24kB / 0B 4.06MB / 0B 2

Docker 镜像

Docker镜像就是一个只读的模板

例如:一个镜像可以包含一个完整的ubuntu操作系统环境,里面仅安装了Apache或用户需要的其他应用程序

Docker 运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker 会从镜像仓库下载(默认是 Docker Hub 公共注册服务器中的仓库)

本地镜像都保存在 Docker 宿主机的 /var/lib/docker目录下,所有容器/var/lib/docker/containers

查询镜像 docker images

$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

helloworld v1 aa8412949a91 2 hours ago 13.3kB

  bafbee25d7e3 2 hours ago 0B

nginx latest daee903b4e43 7 days ago 133MB

ubuntu latest d70eaf7277ea 4 weeks ago 72.9MB

hello-world latest bf756fb1ae65 10 months ago 13.3kB

ubuntu 15.10 9b9cb95443b5 4 years ago 137MB

REPOSITORY 所在仓库

Tag ,同一个仓库中的不同镜像,通过仓库名后面加冒号和标签来指定仓库的某个镜像,

例如ubuntu 镜像存在多个版本 ubuntu:15.10 则是指定15.10 版本,

删除镜像 docker rmi image_id

$ docker rmi aa8412949a91

Untagged: helloworld:v1

Deleted: sha256:aa8412949a918eb5541e3c63ef4e3f1e553da19c7f0808eebf2b0b39f2ac0462

Deleted: sha256:078f722de66a550e6f81d7af400024619efd504b00706bbfdc1efe7426abc13a

查询镜像 docker search xxx

可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/

$ docker search ubuntu

NAME DESCRIPTION STARS OFFICIAL AUTOMATED

ubuntu Ubuntu is a Debian-based Linux operating sys 11552 [OK]

dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface 476 [OK]

rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi 250 [OK]

consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session 228 [OK]

ubuntu-upstart Upstart is an event-based replacement for th 109 [OK]

neurodebian NeuroDebian provides neuroscience research s 76 [OK]

1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]

ubuntu-debootstrap debootstrap --variant=minbase --components=m 44 [OK]

nuagebec/ubuntu Simple always updated Ubuntu docker images w 24 [OK]

i386/ubuntu Ubuntu is a Debian-based Linux operating sys 24

1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK]

1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]

ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys 13

1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK]

1and1internet/ubuntu-16-nginx-php-5.6 ubuntu-16-nginx-php-5.6 8 [OK]

1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK]

darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK]

1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK]

pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc 4

pivotaldata/ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2

pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1

1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]

smartentry/ubuntu ubuntu with smartentry 1 [OK]

1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK]

pivotaldata/ubuntu16.04-test Ubuntu 16.04 image for GPDB testing 0

可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: Docker Hub

拉取镜像 docker pull image_name

如果镜像不在本地,Docker会先从Docker Hub下载镜像

如果没有指定标签,Docker会直接下载latest的标签镜像

$ docker pull ubuntu:14.10

14.10: Pulling from library/ubuntu

Image docker.io/library/ubuntu:14.10 uses outdated schema1 manifest format. Please upgra

b0efe5c05b4c: Pull complete

0a1f1b169319: Pull complete

1ceb0a3c7c48: Pull complete

a3ed95caeb02: Pull complete

Digest: sha256:6341c688b4b0b82ec735389b3c97df8cf2831b8cb8bd1856779130a86574ac5c

Status: Downloaded newer image for ubuntu:14.10

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

再次查询时候ubuntu 会多一个14.10版本的镜像

$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

  bafbee25d7e3 2 hours ago 0B

nginx latest daee903b4e43 7 days ago 133MB

ubuntu latest d70eaf7277ea 4 weeks ago 72.9MB

hello-world latest bf756fb1ae65 10 months ago 13.3kB

ubuntu 15.10 9b9cb95443b5 4 years ago 137MB

ubuntu 14.10 a8a2ba3ce1a3 5 years ago 194MB

repository:镜像仓库源

tag: 镜像标签,同一个仓库员可以有多个镜像,表示多个版本

imageid: 镜像 ID

重命名标签 docker tag

docker tag 镜像Id 镜像源:新标签

合法的容器名称是 :小写字母a-z ,大写字母A-Z,数字0-9 ,下划线,圆点,横线

例如:

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

nginx latest daee903b4e43 2 weeks ago 133MB

ubuntu latest d70eaf7277ea 5 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

ubuntu 15.10 9b9cb95443b5 4 years ago 137MB

ubuntu 14.10 a8a2ba3ce1a3 5 years ago 194MB



User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker tag a8a2ba3ce1a3 ubuntu:14.10.1



User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

nginx latest daee903b4e43 2 weeks ago 133MB

ubuntu latest d70eaf7277ea 5 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

ubuntu 15.10 9b9cb95443b5 4 years ago 137MB

ubuntu 14.10 a8a2ba3ce1a3 5 years ago 194MB

ubuntu 14.10.1 a8a2ba3ce1a3 5 years ago 194MB

更新容器docker commit

构建镜像有2中方法,这里说的构建是指基于一个已经有的基础镜像构建新镜像

1)使用docker commit 命令 不推荐

2)使用docker build 命令和Dockerfile 文件,更灵活更强大

$ docker run -i -t ubuntu:15.10 /bin/bash

root@dce665789bea:/# ls

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@dce665789bea:/# cd ./home

root@dce665789bea:/home# mkdir ./boss

root@dce665789bea:/home# exit

exit

容器里创建一个文件夹 /home/boss

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker container ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

dce665789bea ubuntu:15.10 "/bin/bash" 4 minutes ago Exited (0) 10 seconds ago boring_lehmann

189a55df250f ubuntu:15.10 "/bin/bash" 7 minutes ago Exited (127) 6 minutes ago wizardly_poitras

boring_hoover

产品刚才运行的容器dce665789bea

User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker commit -m "Add boss dir" -a "Docker boss" dce665789bea boss:v1

sha256:7e7584afbc180c1f9b26ce89236bf8d5a1d13d52a200e0a7dfcb60bd1d2076ec

创建一个新的镜像



User@User-PC MINGW64 /c/Program Files/Docker Toolbox

$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

boss v1 7e7584afbc18 9 seconds ago 137MB

nginx latest daee903b4e43 2 weeks ago 133MB

ubuntu latest d70eaf7277ea 5 weeks ago 72.9MB

hello-world latest bf756fb1ae65 11 months ago 13.3kB

ubuntu 15.10 9b9cb95443b5 4 years ago 137MB

ubuntu 14.10 a8a2ba3ce1a3 5 years ago 194MB

ubuntu 14.10.1 a8a2ba3ce1a3 5 years ago 194MB

boss则是我们新创建的镜像, 运行后可以看到创建的boss文件夹

你可能感兴趣的:(CICD,docker,容器,运维)