Spring -- Docker

容器 / 虚拟机

容器是应用层的抽象,是标准化的单元,容器内部不包含操作系统的细节和内容,比虚拟机轻量

Docker

开发:简化开发环境的搭建;运维:交付系统更为流畅,伸缩性更好

常用命令

镜像相关

  1. docker search <image>
  2. docker pull <image>

容器相关

  1. docker run
  2. docker start/stop <container>
  3. docker ps <container>
  4. docker logs <container>

docker run

  1. docker run [option…] image [command] [arg…]
  2. -d:后台运行容器
  3. -e:设置环境变量
  4. --expose/-p 宿主端口:容器端口
  5. --name:指定容器名称
  6. --link:链接其他容器
  7. -v 宿主目录:容器目录,挂载磁盘卷

mongo

1
2
3
4
5
6
$ docker search mongo
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mongo MongoDB document databases provide high avai… 6196 [OK]
mongo-express Web-based MongoDB admin interface, written w… 516 [OK]
tutum/mongodb MongoDB Docker image – listens in port 27017… 228 [OK]
...

docker pull

1
2
3
4
5
6
$ docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
Digest: sha256:d9e20d05063ba34bac4da916e335c70d6add38241cee1e99ad96c47660bd6955
Status: Image is up to date for mongo:latest
docker.io/library/mongo:latest

docker images

1
2
3
4
5
6
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest cdc6740b66a7 4 weeks ago 361MB
zookeeper latest 4ebfb9474e72 5 months ago 150MB
mysql latest 7bb2586065cd 5 months ago 477MB
...

docker run

1
2
$ docker run -d --name mongo -p 27017:27017 -v ~/docker-data/mongo:/data/db -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=123456 mongo
19fcb168261d63aa094ed79b7d93d997f1b58330537fc76bd1bebf24f8cbee1f

docker ps

1
2
3
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19fcb168261d mongo "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:27017->27017/tcp mongo

docker exec

1
2
3
4
5
6
7
8
9
10
11
12
$ docker exec -it mongo /bin/bash
root@19fcb168261d:/# mongo -uroot -p123456
MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9413b54e-cf03-4951-9dbb-8e069565a503") }
MongoDB server version: 4.2.0
Welcome to the MongoDB shell.

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
0%