docker基本命令

Posted 学习不停止

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker基本命令相关的知识,希望对你有一定的参考价值。

 

 

运行交互式的容器:

[[email protected] ~]# docker run -i -t ubuntu:15.10 /bin/bash #

[email protected]:/# ls

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

  • -t:在新容器内指定一个伪终端或终端。
  • -i:允许你对容器内的标准输入 (STDIN) 进行交互。

 

启动容器(后台模式):

[[email protected] ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"

5a2a7609bb50410323b30532988422412a0bd76f7ca36a7b79fc7dfd58348402

[[email protected] ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

5a2a7609bb50        ubuntu:15.10        "/bin/sh -c ‘while tr"   5 seconds ago       Up 3 seconds                            tiny_shaw

[[email protected] ~]# docker logs 5a2a7609bb50

hello world

hello world

hello world

hello world

hello world

hello world

hello world

hello world

hello world

hello world

hello world

hello world

 

停止容器:

[[email protected] ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES

5a2a7609bb50        ubuntu:15.10        "/bin/sh -c ‘while tr"   4 minutes ago       Exited (137) 6 seconds ago                       tiny_shaw

 

网络端口映射:

[[email protected] sysconfig]# docker run -d -P training/webapp python app.py

6ab91fee56ff81c3ee71f89dfad0ed6439aaf698c563b4b90d9355f5231a8240

You have mail in /var/spool/mail/root

[[email protected] sysconfig]# docker ps -l

CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                     NAMES

6ab91fee56ff        training/webapp:latest   "python app.py"     6 seconds ago       Up 6 seconds        0.0.0.0:32778->5000/tcp   furious_rosalind

  • -d:让容器在后台运行。
  • -P:将容器内部使用的网络端口映射到我们使用的主机上。
  • -p:指定需要绑定的端口号

 

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

-l,--latest=false:查看最后创建的容器(包含没有启动的容器)

[[email protected] sysconfig]# docker run -d -p 5000:5000 training/webapp python app.py

2bb2ed1f9867438b287b837151c88527cd21770eaec700323879815b5d1feea4

[[email protected] sysconfig]# docker ps -l

CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                    NAMES

2bb2ed1f9867        training/webapp:latest   "python app.py"     7 seconds ago       Up 6 seconds        0.0.0.0:5000->5000/tcp   high_mcclintock

 

查看网络端口映射:

[[email protected] sysconfig]# docker ps -l

CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                    NAMES

2bb2ed1f9867        training/webapp:latest   "python app.py"     9 minutes ago       Up 9 minutes        0.0.0.0:5000->5000/tcp   high_mcclintock

[[email protected]elk01 sysconfig]# docker port 2bb2ed1f9867  #可以使用容器ID或者名字

5000/tcp -> 0.0.0.0:5000

[[email protected] sysconfig]# docker port high_mcclintock

5000/tcp -> 0.0.0.0:5000

 

查看容器的应用日志:

和tail -f一样

[[email protected] sysconfig]# docker logs -f 2bb2ed1f9867

 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

10.11.102.12 - - [03/Nov/2017 06:52:49] "GET / HTTP/1.1" 200 -

10.11.102.12 - - [03/Nov/2017 06:52:49] "GET /favicon.ico HTTP/1.1" 404 -

 

检查WEB应用程序:

使用 docker inspect 来查看Docker的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息。

[[email protected] sysconfig]# docker inspect 2bb2ed1f9867

[{

    "AppArmorProfile": "",

    "Args": [

        "app.py"

    ],

    "Config": {

        "AttachStderr": false,

        "AttachStdin": false,

        "AttachStdout": false,

        "Cmd": [

            "python",

            "app.py"

        ],

 

删除容器:

容器必须先停止,才能删除

[[email protected] sysconfig]# docker rm 2bb2ed1f9867

Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f

FATA[0000] Error: failed to remove one or more containers

[[email protected] sysconfig]# docker stop 2bb2ed1f9867

2bb2ed1f9867

[[email protected] sysconfig]# docker rm 2bb2ed1f9867

2bb2ed1f9867

 

列出本地所有镜像;

[[email protected] sysconfig]# docker images

REPOSITORY               TAG                 IMAGE ID            CREATED            VIRTUAL SIZE

yehaifeng/learn             6.7                 a0318133f1fa        2 weeks ago          190.6 MB

yehaifeng/ubuntu           v4                  6b416869c565        2 weeks ago         137.2 MB

docker.io/mysql             5.6                 dd751b1fac67        2 weeks ago          299 MB

docker.io/httpd             latest              fe37d22f8f5f        3 weeks ago          177.3 MB

docker.io/ubuntu           latest              bd92ca350bbd        3 weeks ago          122 MB

docker.io/nginx            latest              2ecc072be0ec        3 weeks ago          108.3 MB

docker.io/debian           jessie              de2958a3c124        3 weeks ago          123.4 MB

docker.io/centos           6.7                 27c25e48cd03        7 weeks ago          190.6 MB

docker.io/hello-world       latest              bef02f2f6467        7 weeks ago          1.84 kB

docker.io/jiaxiangkong/jumpserver_docker  0.3.2               80e9ddba8da6        11 months ago        179.2 MB

docker.io/ubuntu           15.10               bfaaabeea063        15 months ago        137.2 MB

docker.io/training/webapp     latest              02a8815912ca        2.472794 years ago   348.7 MB

docker.io/ubuntu             13.10               195eb90b5349        3.379130 years ago   184.5 MB

  • REPOSITORY表示镜像的仓库源
  • TAG镜像的标签
  • IMAGE ID镜像ID
  • CREATED镜像创建时间
  • SIZE镜像大小

 

从镜像仓库查找下载镜像:

[[email protected] sysconfig]# docker search nginx

INDEX       NAME                 DESCRIPTION                       STARS     OFFICIAL   AUTOMATED

docker.io   docker.io/nginx           Official build of Nginx.                  7172      [OK]

docker.io   docker.io/jwilder/nginx-prox Automated Nginx reverse proxy for docker c...   1159                 [OK]

docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 468                  [OK]

 

[[email protected] sysconfig]# docker pull nginx

latest: Pulling from docker.io/nginx

33e4f169980f: Pull complete

4732224668e2: Pull complete

d7a36ce98ade: Pull complete

d8b26cb8ad00: Pull complete

15fc5d030907: Pull complete

878f1c38771b: Pull complete

b18c56787b43: Pull complete

9c0ba5cb2724: Pull complete

2bbcf0f8fd73: Pull complete

6362f5ebce41: Pull complete

Digest: sha256:19d924bb7e9cfc412703c433e61803768cca7b2b8ef2ba1250be6647868a6acf

Status: Downloaded newer image for docker.io/nginx:latest

You have mail in /var/spool/mail/root

[[email protected] sysconfig]# docker image nginx

docker: ‘image‘ is not a docker command. See ‘docker --help‘.

[[email protected] sysconfig]# docker images

REPOSITORY                                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

docker.io/nginx                            latest              6362f5ebce41        6 days ago          108.4 MB

docker.io/mysql                            5.6                 dd751b1fac67        2 weeks ago         299 MB

docker.io/jiaxiangkong/jumpserver_docker   0.3.2               80e9ddba8da6        11 months ago       179.2 MB

 

创建镜像:

当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

  • 1.从已经创建的容器中更新镜像,并且提交这个镜像
  • 2.使用 Dockerfile 指令来创建一个新的镜像

更新镜像:

#更新镜像之前,我们需要使用镜像来创建一个容器。

[[email protected] sysconfig]# docker run -t -i ubuntu:15.10 /bin/bash

Unable to find image ‘ubuntu:15.10‘ locally

15.10: Pulling from docker.io/ubuntu

8e40f6313e6b: Pull complete

e2224f46fc07: Pull complete

8c721b8e6e1c: Pull complete

a73b3adec5de: Pull complete

bfaaabeea063: Pull complete

Digest: sha256:cc767eb612212f9f5f06cd1f4e0821d781a5f83bc24d1182128a1088907d3825

Status: Downloaded newer image for docker.io/ubuntu:15.10

[email protected]:/# ls

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

#在运行的容器内使用命令进行修改操作

[email protected]:/# apt-get update

Ign http://archive.ubuntu.com wily InRelease

Ign http://archive.ubuntu.com wily-updates InRelease

Ign http://archive.ubuntu.com wily-security InRelease

Ign http://archive.ubuntu.com wily Release.gpg

Ign http://archive.ubuntu.com wily-updates Release.gpg

Ign http://archive.ubuntu.com wily-security Release.gpg

Ign http://archive.ubuntu.com wily Release

#将修改好的副本提交docker仓库中,并指定版本为v2

[[email protected] sysconfig]# docker commit -m="has update" -a="runoob"  7ea24f7664d0 yehaifeng/ubuntu:v2

2f84022778678a442c943c19b492232a4b5867d853ed57e11d0246150dda9c28

[[email protected] sysconfig]# docker images

REPOSITORY                                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

yehaifeng/ubuntu                           v2                  2f8402277867        4 seconds ago       137.2 MB

docker.io/nginx                            latest              6362f5ebce41        6 days ago          108.4 MB

docker.io/mysql                            5.6                 dd751b1fac67        2 weeks ago         299 MB

docker.io/jiaxiangkong/jumpserver_docker   0.3.2               80e9ddba8da6        11 months ago       179.2 MB

docker.io/ubuntu                           15.10               bfaaabeea063        15 months ago       137.2 MB

  • -m:提交的描述信息
  • -a:指定镜像作者
  • e218edb10161容器ID
  • runoob/ubuntu:v2:指定要创建的目标镜像名

 

构建镜像docker build

从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

[[email protected] ~]# cat Dockerfile

FROM    centos:6.7

MAINTAINER      Fisher "[email protected]"

 

RUN     /bin/echo ‘root:123456‘ |chpasswd

RUN     useradd runoob

RUN     /bin/echo ‘runoob:123456‘ |chpasswd

RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local

EXPOSE  22

EXPOSE  80

CMD     /usr/sbin/sshd –D

 

以上是关于docker基本命令的主要内容,如果未能解决你的问题,请参考以下文章

Docker 常用基本命令

Docker 常用基本命令

Docker 常用基本命令

Docker 常用基本命令

Docker基本命令

docker 基本命令