1.docker各种常用命令 container is a running environment for image
docker images images列表
docker pull redis
docker run redis:4.0会pull4.0版本的redis
docker ps 运行的docker
docker rmi dockerid image
docker rm dockerid container
docker run redis
docker run -d redis 后台运行容器,并返回容器ID;
docker ps -a显示所有docker
docker start dockerid
docker stop dockerid
2.绑定不同端口不同redis版本
docker run -p6000:6379 redis
docker run -p6001:6379 redis:4.0
3.docker启动,并给docker命名
docker run -d -p:6001:6379 --name redis-older redis:4.0
4.查看docker log
docker logs dockerid
docker logs
docker logs redis-older
docker logs redis-older tail查看尾部的logs
5.docker exec
docker exec -it dockerid /bin/bash进入docker
docker exec -it redis-older /bin/bash
docker exec -it redis-older /bin/sh 如果bash没安装,可以用/bin/sh
6.docker 拉取镜像
docker network ls
docker pull mongo
docker pull mongo-express
7.docker创建网络
docker network create mongo-network
8.启动mongo 链接时指定用户名和密码
docker run -d \
-p 27017:27017 \
-e mongo_initdb_root_usename=admin \
-e mongo_initdb_root_password=password
--name mongodb \
--net mongo-network \
mongo
9.启动mongo-express
docker run -d \
-p 8081:8081 \
-e me_config_mongodb_adminusername=admin \
-e me_config_mongodb_adminpassword=password
--name mongo-express \
--net mongo-network \
-e me_config_mongodb_server=mongodb \
mongo-express
10.用文件启动docker docker compose mongo-docker-compose.yaml
version:'3'
services:
mongodb:
image:mongo
ports:
- 27017:27017
environment:
- mongo_initdb_root_usename=admin
- mongo_initdb_root_password=password
mongo-express:
image:mongo-express
ports:
- 8080:8081
environment:
- me_config_mongodb_adminusername=admin
- me_config_mongodb_adminpassword=password
- me_config_mongodb_server=mongodb
命令运行mongo-docker-compose.yaml 会自动创建mongo-network,不需要在yaml中配置 docker-compose -f mongo-docker-compose.yaml up docker-compose -f mongo-docker-compose.yaml down删除容器,并删除network
11.打包docker dockerfile文件 新建dockerfile在my-app目录
FROM node:13-alpine
ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PWD=password
RUN mkdir -p /home/app
COPY . /home/app
CMD ["node","/home/app/server.js"]
12.创建docker .代表当前目录my-app
docker build -t my-app:1.0 .
docker run my-app:1.0
如果需要修改dockerfile
docker stop dockerid
docker rmi dockerid image
docker rm dockerid container
然后重新docker build -t my-app:1.0 .
docker registry
aws注册registry
认证
docker login
docker tag
docker push
13.运行my-app docker 私有库,需要docker login
mongo-docker-compose.yaml
version:'3'
services:
my-app:
image:d32434.dkr.ecr.eu-central-l.amazonaws.com/my-app:1.0
ports:
- 3000:3000
mongodb:
image:mongo
ports:
- 27017:27017
environment:
- mongo_initdb_root_usename=admin
- mongo_initdb_root_password=password
mongo-express:
image:mongo-express
ports:
- 8080:8081
environment:
- me_config_mongodb_adminusername=admin
- me_config_mongodb_adminpassword=password
- me_config_mongodb_server=mongodb
14.docker volumes
mongo-docker-compose.yaml
version:'3'
services:
my-app:
image:d32434.dkr.ecr.eu-central-l.amazonaws.com/my-app:1.0
ports:
- 3000:3000
mongodb:
image:mongo
ports:
- 27017:27017
environment:
- mongo_initdb_root_usename=admin
- mongo_initdb_root_password=password
volumes:
- mongo-data:/data/db 路径根据不同数据库不同,具体可查docker mongo
mongo-express:
image:mongo-express
ports:
- 8080:8081
environment:
- me_config_mongodb_adminusername=admin
- me_config_mongodb_adminpassword=password
- me_config_mongodb_server=mongodb
volumes:
mongo-data:
driver:local
docker学习链接视频: https://www.bilibili.com/video/BV12S4y1P7CU?p=69
|