docker基础

note

  • docker FQA
  • resources
  • https://hub.docker.com

常用命令

docker

cp 复制

docker cp container-name:/path/ /local/path
docker cp /local/path container-name:/path

image

docker image ls
docker image rm 75835a67d134 2a4cca5ac898
# 删除所有未运行的镜像 
docker image prune
docker image prune -a
docker image prune -a --filter "until=12h"

docker images
docker search httpd
docker pull httpd
# e218edb10161 is container id
docker commit -m="has update" -a="zhengcj01" e218edb10161  zhengcj01/elasticsearch-with-ik-pinyin:7.2.0-v2
# in the directory where there is a Dockerfile
docker build -t zhengcj01/elasticsearch-with-ik-pinyin:7.2.0-v3 .
# add tag to image, 860c279d2fec container id
docker tag 860c279d2fec runoob/centos:dev

docker rmi mysql
docker history zhengcj01/elasticsearch-with-ik-pinyin:7.2.0

docker save -o mysql.tar zhengcj01/mysql:v1
docker load -i mysql.tar
docker load < mysql.tar

container

docker container ls -a
docker container rm cc3f2ff51cab cd20b396a061
docker container ls -a --filter status=exited --filter status=created
docker container prune --filter "until=12h"
# 删除所有未运行的容器
docker container prune
docker container stop $(docker container ls -aq)

docker ps  
docker ps -l
docker ps -a
docker ps -aq

docker ps -n 5 
docker ps -a | awk '{print $1}'

docker create  --name mymysql  mysql:latest

# -d:让容器在后台运行
# -P :是容器内部端口随机映射到主机的高端口
docker run -d -P ubuntu:15.10
docker run ubuntu:15.10 /bin/echo "Hello world"
docker run -it ubuntu:15.10 /bin/echo "Hello world"

docker top mysql
for i in  `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done

docker start mysql
docker stop mysql
docker restart mysql 
docker kill -s HUP mysql
docker kill -s KILL mysql

volume

docker volume ls
docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
docker volume prune

network

docker network ls
docker network rm c520032c3d31
docker network prune
docker network prune -a --filter "until=12h"

综合

# 查看镜像占用资源
docker stats

# 推送到仓库
docker push zhengcj01/swagger

docker exec -it mysql mysql -uroot -proot
docker exec -it mysql /bin/bash

# see logs
docker logs `docker ps|awk '/mysql/{print $1}'`
# like tail -f
docker logs -f `docker ps|awk '/mysql/{print $1}'`

# stop container
docker stop `docker ps|awk '/mysql/{print $1}'`
docker stop mysql

docker rm mysql
docker pause mysql
docker unpause mysql

docker port bf08b7f2cd89
docker port mysql

# 删除所有停止的容器、未被使用的镜像和网络
docker system prune
# 删除所有未被使用的volumes
docker system prune --volumes

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

docker inspect es7_01
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'  es7_01
docker stats es7_01
docker stats 

docker-compose

# docker-compose 补全工具
sudo curl -L https://raw.githubusercontent.com/docker/compose/${{DOCKER_COMPOSE_VERSION}}/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

docker-compose up
docker-compose up -d
docker-compose ps
docker-compose rm eureka
docker-compose scale eureka=3
docker-compose scale user=3 movie=3
docker-compose stop eureka
docker-compose start eureka

demo

mysql安装

docker

docker pull mysql:5.7

# 创建实例并启动
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root  \
-d mysql:5.7

> 参数说明(前者为主机参数、后者为docker容器参数)
- -p 3306:3306:将容器的3306端口映射到主机的3306端口
- -v /mydata/mysql/conf:/etc/mysql:将配置文件夹挂在到主机
- -v /mydata/mysql/log:/var/log/mysql:将日志文件夹挂载到主机
- -v /mydata/mysql/data:/var/lib/mysql/:将配置文件夹挂载到主机
- -e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码

# 进入容器文件系统
docker exec -it mysql /bin/bash

# 通过容器的mysql命令行工具连接
docker exec -it mysql mysql -uroot -proot
# 容器中,设置远程访问
grant all privileges on *.* to 'root' @'%' identified by 'root';
flush privileges;

docker-compose

  • docker-compose.yml
version: '3'
services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: always
    ports:
      - 3306:3306
    volumes:
      - "./data:/var/lib/mysql"
      - "./conf:/etc/mysql"
      - "./log:/var/log/mysql"
      - "./init/create:/docker-entrypoint-initdb.d/:ro"
      - "./init/start:/tmp/mysql/init"
    environment:
      MYSQL_ROOT_PASSWORD: "root"
#      MYSQL_USER: "test"
#      MYSQL_PASSWORD: "test"
    command: mysqld --init-file="/tmp/mysql/init/init.sql"
#    network_mode: "host"
  • ./init/create/mysql_create.sql
--
-- execute when mysql create
--
DROP USER IF EXISTS 'test';
CREATE USER 'test'@'%' IDENTIFIED BY 'test';
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';
  • ./init/start/init.ql
--
-- execute when mysql start every time
--
DROP DATABASE  IF EXISTS test;
CREATE DATABASE test;
use test;
create table user
(
    id int auto_increment primary key,
    username varchar(64) unique not null,
    phone varchar(120) unique not null
);
insert into user values(1, "zhengcj","13300000000");
insert into user values(2, "dongyc","18800008888");
  • start mysql
docker-compose up

redis安装

docker

docker pull redis:3.2
docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -d redis:3.2 redis-server --appendonly yes
docker exec -it redis redis-cli

docker-compose

version: '3'
services:
  redis:
    image: redis
    container_name: redis
    ports:
      - 6379:6379
    volumes:
      - redis:/data
    command: redis-server --appendonly yes

volumes:
  redis:
    driver: local

networks:
  webservice_web-service:
    external: true
  back:

nginx安装

  • 可以先启动nginx,复制容器nginx下相关配置信息到主机,然后修改配置,重新挂载到容器
  • docker cp nginx:/etc/nginx /mydata/nginx/conf
  • docker cp /mydata/nginx/conf nginx:/etc/nginx
  • 挂载参数: -v

docker

docker search nginx
docker pull nginx:1.10

mkdir -p /mydata/nginx/www /mydata/nginx/logs /mydata/nginx/conf
docker cp 6dd4380ba708:/etc/nginx /mydata/nginx/conf
docker cp 6dd4380ba708:/usr/share/nginx/html /mydata/nginx/html

docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx  \
-v /mydata/nginx/conf:/etc/nginx  \
-d nginx:1.10

docker-compose

version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - 80:80
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/logs:/var/log/nginx
      - ./nginx/conf:/etc/nginx

rabbitmq

docker

docker pull rabbitmq:management

docker run -d --name rabbitmq --publish 5671:5671 \
 --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672 \
rabbitmq:management

docker-compose

version: '3'
services:
  rabbitmq1:
    image: "rabbitmq"
    container_name: rabbitmq1
    environment:
      - RABBITMQ_ERLANG_COOKIE='secret_cookie'
    networks:
      - back
    hostname: rabbitmq1
    ports:
      - "15672:15672"
      - "5672:5672"
    tty: true
    volumes:
      - rabbitmq1:/var/lib/rabbitmq
      - ./rabbitmq/conf/:/etc/rabbitmq/
    command:  bash -c "sleep 10; rabbitmq-server;"
    
  rabbitmq2:
    image: "rabbitmq"
    container_name: rabbitmq2
    environment:
      - RABBITMQ_ERLANG_COOKIE='secret_cookie'
    networks:
      - back
    hostname: rabbitmq2
    depends_on:
      - rabbitmq1
    ports:
      - "15673:15672"
      - "5673:5672"
    tty: true
    volumes:
      - rabbitmq2:/var/lib/rabbitmq
      - ./rabbitmq/conf/:/etc/rabbitmq/
    command:  bash -c "sleep 10; rabbitmq-server;"
    
volumes:
  rabbitmq1:
    driver: local # docker volume create --driver local --name rabbitmq1
  rabbitmq2:
    driver: local

networks:
  back:

elasticsearch

docker-compose 安装 ES集群及ELK等相关软件

mongodb安装

docker

docker pull mongo:3.2

docker run -p 27017:27017 --name mongo -v /mydata/mongo/db:/data/db -d mongo:3.2

docker exec -it mongo mongo

docker-compose

version: '3'
services:
  mongo:
    image: mongo
    container_name: mongodb
    restart: always
    ports:
      - 27017:27017
    volumes:
      - "./mongo/data/db:/data/db"
      - "./mongo/setup:/docker-entrypoint-initdb.d/:ro"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin

  # 如果不需要MongoDB的网页端,以下内容可以不加
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:  #这里只能使用与上方MONGO_INITDB_ROOT_USERNAME相同的root账号
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: admin

Apache httpd

docker

docker search httpd
docker pull httpd
docker run -p 80:80 -v /mydata/www/:/usr/local/apache2/htdocs/ -v  /mydata/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v  /mydata/logs/:/usr/local/apache2/logs/ -d httpd

SpringBoot应用命令部署

  • pom.xml 中相关配置,点击github中查看详情
    
        zhengcj01
        1.4.10
    


    
        org.springframework.boot
        spring-boot-maven-plugin
    
    
    
        com.spotify
        dockerfile-maven-plugin
        1.4.9
        
            ${docker.image.prefix}/${project.artifactId}
        
    
    

    
    
        org.apache.maven.plugins
        maven-dependency-plugin
        
            
                unpack
                package
                
                    unpack
                
                
                    
                        
                            ${project.groupId}
                            ${project.artifactId}
                            ${project.version}
                        
                    
                
            
        
    
    

  • Dockerfile,点击github中查看详情
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.zhengcj.demo.swagger.SwaggerApplication"]
  • docker-compose.yml, 点击github中查看详情
version: '3'
services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: always
    ports:
      - 3306:3306
    volumes:
      - "/home/jim/project/dev_env/mysql/data:/var/lib/mysql"
      - "/home/jim/project/dev_env/mysql/conf:/etc/mysql"
      - "/home/jim/project/dev_env/mysql/log:/var/log/mysql"
    environment:
      MYSQL_ROOT_PASSWORD: "root"
  swagger-service:
    image: zhengcj01/swagger
    container_name: swagger-service
    restart: always
    ports:
      - 8080:8080
    links:
      - mysql:mysql
    environment:
      - spring.profiles.active=dev
  • 说明
    • ./mvnw install dockerfile:build 生成镜像(生成jar包->解压->根据Dockerfile生成镜像)
    • docker-compose.ymlswagger-service访问 mysql,需要设置links,在项目 application.yml 中设置数据库链接字符串为jdbc:mysql://mysql:3306/demo?characterEncoding=utf-8
    • docker-compose up 启动mysqlswagger-service
    • 第一次启动mysql时,需要初始化数据库数据(没有demo数据库),初始sql见github
    • 参考

参考

  • How To Remove Docker Containers, Images, Volumes, and Networks
  • Spring Boot with Docker
  • Spring Boot Docker

你可能感兴趣的:(docker基础)