1. 程式人生 > >docker基本命令

docker基本命令

onf 器) 端口 網絡端口 停止 內部 val false 目標

運行交互式的容器:

[root@elk02 ~]# docker run -i -t ubuntu:15.10 /bin/bash #

root@66d539b6313e:/# ls

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

  • -t:在新容器內指定一個偽終端或終端。
  • -i:允許你對容器內的標準輸入 (STDIN) 進行交互。

啟動容器(後臺模式):

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

5a2a7609bb50410323b30532988422412a0bd76f7ca36a7b79fc7dfd58348402

[root@elk02 ~]# 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

[root@elk02 ~]# 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

停止容器:

[root@elk02 ~]# 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

網絡端口映射:

[root@elk01 sysconfig]# docker run -d -P training/webapp python app.py

6ab91fee56ff81c3ee71f89dfad0ed6439aaf698c563b4b90d9355f5231a8240

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

[root@elk01 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:查看最後創建的容器(包含沒有啟動的容器)

[root@elk01 sysconfig]# docker run -d -p 5000:5000 training/webapp python app.py

2bb2ed1f9867438b287b837151c88527cd21770eaec700323879815b5d1feea4

[root@elk01 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

查看網絡端口映射:

[root@elk01 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

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

5000/tcp -> 0.0.0.0:5000

[root@elk01 sysconfig]# docker port high_mcclintock

5000/tcp -> 0.0.0.0:5000

查看容器的應用日誌:

和tail -f一樣

[root@elk01 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 容器的配置和狀態信息。

[root@elk01 sysconfig]# docker inspect 2bb2ed1f9867

[{

"AppArmorProfile": "",

"Args": [

"app.py"

],

"Config": {

"AttachStderr": false,

"AttachStdin": false,

"AttachStdout": false,

"Cmd": [

"python",

"app.py"

],

刪除容器:

容器必須先停止,才能刪除

[root@elk01 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

[root@elk01 sysconfig]# docker stop 2bb2ed1f9867

2bb2ed1f9867

[root@elk01 sysconfig]# docker rm 2bb2ed1f9867

2bb2ed1f9867

列出本地所有鏡像;

[root@elk01 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鏡像大小

從鏡像倉庫查找下載鏡像:

[root@elk01 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]

[root@elk01 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

[root@elk01 sysconfig]# docker image nginx

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

[root@elk01 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 指令來創建一個新的鏡像

更新鏡像:

#更新鏡像之前,我們需要使用鏡像來創建一個容器。

[root@elk01 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

root@7ea24f7664d0:/# ls

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

#在運行的容器內使用命令進行修改操作

root@7ea24f7664d0:/# 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

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

2f84022778678a442c943c19b492232a4b5867d853ed57e11d0246150dda9c28

[root@elk01 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 如何構建我們的鏡像。

[root@elk01 ~]# 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基本命令