Docker 常用命令


灑家近日玩了一陣子docker,寫點筆記備查。

本文URL: http://www.cnblogs.com/go2bed/p/5703117.html

官方文檔: https://docs.docker.com/

搜索鏡像

鏡像可以去 https://hub.docker.com/ 搜索,或者使用命令

如:

user@ubuntu:~$ sudo docker search debian

NAME

 DESCRIPTION

STARS

OFFICIAL

 AUTOMATED

debian

 Debian is a Linux distribution that's comp...

1519

[OK]

 

neurodebian

NeuroDebian provides neuroscience research...

25

[OK]

 

jesselang/debian-vagrant

 Stock Debian Images made Vagrant-friendly ...

 8

 

[OK]

armbuild/debian

 ARMHF port of debian

8

 

[OK]

docker hub 幾個鏡像:

nickistre/ubuntu-lamp

https://hub.docker.com/r/nickistre/ubuntu-lamp/~/dockerfile/

php

https://hub.docker.com/_/php/

mysql

https://hub.docker.com/_/mysql/

下載鏡像

sudo docker pull ubuntu

顯示本地鏡像

user@ubuntu:~$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu              latest              4ef6a5ece191        3 days ago          120.1 MB

reinblau/lamp       latest              e9df29833f32        9 days ago          703.8 MB

運行鏡像,映射端口

開啟lamp鏡像,映射主機8080端口到容器80端口

e9d是 鏡像id或鏡像名

user@ubuntu:~$ sudo docker run -it -p 8080:80 e9d apache2

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.1. Set the 'ServerName' directive globally to suppress this message

 

user@ubuntu:~$ sudo docker run -it -p 8080:80 e9d /bin/bash

root@ac4c74c9ac8a:/var/www/html# apache2

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

 

在后台運行,啟動后容器內自動運行 /root/run.sh

sudo docker run -itd -p 8080:80 e9d /root/run.sh

參數:

加上 -i -t 可以 ctrl+p ctrl+q 退出。

-d, --detach=false         Run container in background and print container ID

-i, --interactive=false    Keep STDIN open even if not attached

-P, --publish-all=false    Publish all exposed ports to random ports

-p, --publish=[]           Publish a container's port(s) to the host

-t, --tty=false            Allocate a pseudo-TTY 分配一個偽TTY?

依附到運行中的容器

ac4c 是容器號

sudo docker attach ac4c

退出,一定不要用ctrl+c,那樣就是讓docker容器停止了。

要用如下快捷鍵:先按,ctrl+p;再按,ctrl+q

 

列出容器

user@ubuntu:~$ sudo docker ps -a

CONTAINER ID

IMAGE

COMMAND

CREATED

STATUS

PORTS

NAMES

ac4c74c9ac8a

e9d:latest

"/bin/bash"

7 minutes ago

Up 7 minutes

0.0.0.0:8080->80/tcp

insane_mayer 

3a4b37b41ea7

e9d:latest

"apache2"

7 minutes ago

Exited (0) 7 minutes ago 

 

suspicious_darwin

參數

-a, --all=false       Show all containers (default shows just running)

-q, --quiet=false     Only display numeric IDs

查看端口映射

ac4c是容器id

user@ubuntu:~$ sudo docker port ac4c

80/tcp -> 0.0.0.0:8080

從dockerfile 創建鏡像

sudo docker build -t mylamp/test /home/user/Docker/mylamp_test/

上例中,dockerfile存在於 /home/shen/Docker/mylamp_test/,鏡像tag為mylamp/test

參數

 -t, --tag=            Repository name (and optionally a tag) for the image

刪除鏡像

先刪除所有依賴容器,再刪除鏡像。

后面跟上標簽或ID,跟標簽會先刪除標簽(untag),如果沒有標簽指向鏡像,就刪除(delete)鏡像。

跟ID,刪除所有相關標簽(untag),再刪除(delete)鏡像。

sudo docker rmi 2318

sudo docker rmi ubuntu

刪除容器

sudo docker rm e81

批量操作容器

停止正在運行的容器

sudo docker stop $(sudo docker ps -q)

刪除(已經停止的,正在運行的不能刪除)容器

sudo docker rm $(sudo docker ps -a -q)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM