您的位置:首页 > 运维架构 > Docker

Docker 常用命令

2016-07-25 13:09 483 查看
洒家近日玩了一阵子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或镜像名

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

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

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

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

列出容器

[b]user@ubuntu:~$ sudo docker ps -a[/b]

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

[b]user@ubuntu:~$ sudo docker port ac4c[/b]

80/tcp -> 0.0.0.0:8080

从dockerfile 创建镜像

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

上例中,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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: