Docker命令

本文总结了常用的Docker命令。

images

列出所有镜像:

1
2
3
hgs:~ hegongshan$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 5e13f8dd4c1a 2 years ago 120MB

run

运行容器:

1
2
hgs:~ hegongshan$ docker run -itd --name ubuntu -p 6666:22 ubuntu:16.04
5aef2e6988fab4d4b3259b80adae969949e9c9852b50da95eeb76f9b2f6c3d33
  • --link <list>:添加对另一个容器的链接

  • --net <string>:将容器与某个网络连接起来

例如,将某个Web服务与数据库容器连接起来时,可能会遇到如下错误:

Cannot link to /xxx, as it does not belong to the default network.

此时只需要按照如下步骤执行即可:

1
2
3
4
# 1.检查数据库容器使用的网络
docker inspect <db> | grep -i network

# 2.将--net设置为数据库容器的网络
  • --restart <string>:指定重启策略,可能的取值有

no:退出时不重启(默认)

always:退出时重启

on-failure:非正常退出时重启

ps

列出所有容器:

1
2
3
hgs:~ hegongshan$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5aef2e6988fa ubuntu:16.04 "/bin/bash" 14 seconds ago Up 13 seconds 0.0.0.0:6666->22/tcp ubuntu

stop

1
2
3
4
hgs:~ hegongshan$ docker stop ubuntu
ubuntu
hgs:~ hegongshan$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

start

1
2
hgs:~ hegongshan$ docker start ubuntu
ubuntu

restart

1
2
hgs:~ hegongshan$ docker restart ubuntu
ubuntu

rename

修改容器名,使用方法如下:

1
Usage:	docker rename CONTAINER NEW_NAME

实践:

1
2
3
4
5
6
7
hgs:~ hegongshan$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5aef2e6988fa ubuntu:16.04 "/bin/bash" 50 seconds ago Up 1 second 0.0.0.0:6666->22/tcp ubuntu
hgs:~ hegongshan$ docker rename ubuntu ubuntu2
hgs:~ hegongshan$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5aef2e6988fa ubuntu:16.04 "/bin/bash" About a minute ago Up 12 seconds 0.0.0.0:6666->22/tcp ubuntu2

rm

删除一个或多个容器。

1
Usage:	docker rm [OPTIONS] CONTAINER [CONTAINER...]
1
2
hgs:~ hegongshan$ docker rm ubuntu
ubuntu

rmi

删除一个或多个镜像。

1
Usage:	docker rmi [OPTIONS] IMAGE [IMAGE...]

cp

在本地和容器之间复制文件。

save

1
2
3
4
5
6
7
8
hgs:~ hegongshan$ docker save --help

Usage: docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
-o, --output string Write to a file, instead of STDOUT
1
2
3
hgs:~ hegongshan$ docker save -o ubuntu-hgs.tar ubuntu
hgs:~ hegongshan$ ls | grep ubuntu-hgs.tar
ubuntu-hgs.tar

load

1
2
3
4
5
6
7
8
9
hgs:~ hegongshan$ docker load --help

Usage: docker load [OPTIONS]

Load an image from a tar archive or STDIN

Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
1
2
hgs:~ hegongshan$ docker load -i ubuntu-hgs.tar 
Loaded image: ubuntu:16.04

build

从Dockerfile中构建镜像。

1
Usage:	docker build [OPTIONS] PATH | URL | -
  • -t:指定镜像的名字和版本,格式为name:tag,tag为可选
1
2
3
4
5
6
7
8
9
hgs:ase-lab1 hegongshan$ ls
Dockerfile ase.tar generatorConfig.xml pom.xml src student.sql target
hgs:ase-lab1 hegongshan$ cat Dockerfile
FROM openjdk:8-jdk-alpine

ADD target/ase.jar /app/
CMD ["java", "-jar", "/app/ase.jar"]

EXPOSE 8080
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
hgs:ase-lab1 hegongshan$ docker build -t ase:latest .
Sending build context to Docker daemon 106.2MB
Step 1/4 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/4 : ADD target/ase.jar /app/
---> 716001f12f27
Step 3/4 : CMD ["java", "-jar", "/app/ase.jar"]
---> Running in 38ea01429260
Removing intermediate container 38ea01429260
---> 3ac568acbd39
Step 4/4 : EXPOSE 8080
---> Running in 0b900e1efe04
Removing intermediate container 0b900e1efe04
---> 47629e3c6267
Successfully built 47629e3c6267
Successfully tagged ase:latest

----------本文结束感谢您的阅读----------
坚持原创技术分享,您的支持将鼓励我继续创作!