Docker安装及常用命令

  • Post author:
  • Post category:其他

docker常用命令

docker操作命令

systemctl start docker          启动docker服务
systemctl stop docker          停止docker服务
systemctl restart docker         重启docker服务
systemctl enable docker         设置开机启动
sudo systemctl status docker           查看docker启动状态
sudo docker -v         查看docker版本
docker network ls         查看docker网络列表
docker volume ls         查看docker持久化存储情况

image操作命令

docker login –username=lucifer_ztl registry.cn-hangzhou.aliyuncs.com         登录远程仓库
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository:[镜像版本号]         给镜像打标签
docker push registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository:[镜像版本号]         推送镜像到远程仓库
docker images          查看docker镜像
docker pull mysql:5.7          拉取mysql5.7版本的镜像
docker run image         运行镜像

# -d后台启动,--name设置容器名称,-p端口映射,-v将容器内的数据持久化保存在宿主机上/root/data/mysql位置,-e添加环境变量设置密码
docker run -d --name mysql -p 3306:3306 -v /root/data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7

docker run -d –name mysql5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root –privileged mysql:5.7         运行mysql5.7版本镜像并创建容器,需指定密码
docker exec -it         容器id bash 进入某个容器
docker image rm [imageid]         根据镜像id删除某个镜像
docker rmi -f [imageid]         根据镜像id删除某个镜像
docker rmi -f $(docker image ls)         删除所有镜像

容器操作命令

docker start 容器id/容器名称         根据容器id或者名称启动容器
docker exec -it 容器id bash         进入某个容器
service mysql status         查看mysql状态【前提是先进入mysql容器】
service mysql start         启动mysql【前提是先进入mysql容器】
mysql -uroot -p         连接mysql

mysql> create database testdb; #创建数据库testdb
mysql> show databases; #查看数据库列表
mysql> use testdb; #使用数据库testdb
mysql> show tables; #查看数据库中的表
mysql> create table `user`(
    ->   `id` bigint(20) NOT NULL,
    ->   `name` varchar(100) NOT NULL,
    ->   `email` varchar(200) DEFAULT NULL,
    ->   `address` varchar(255) DEFAULT NULL,
    ->   `phone` varchar(20) DEFAULT NULL,
    ->   PRIMARY KEY(`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql> insert into `user`(`id`,`name`,`email`,`address`,`phone`) values(1,'Lucifer','xxx123456@qq.com','xian','123456789');
mysql> select * from user;
+----+---------+------------------+---------+-----------+
| id | name    | email            | address | phone     |
+----+---------+------------------+---------+-----------+
|  1 | Lucifer | xxx123456@qq.com | xian    | 123456789 |
+----+---------+------------------+---------+-----------+
1 row in set (0.00 sec)

mysql> 

docker ps         查看运行中的容器
docker ps -a         查看所有容器
docker ps -aq         查看所有容器
docker rm 容器id    删除某个容器
docker rm -f $(docker ps -a)         删除所有容器
docker stop 容器id/容器名称         停止某个容器
docker top 容器id/容器名称         列出容器中运行进程
docker rename 旧容器名称 新容器名称         修改容器名称
docker logs 容器id/容器名称         查看容器服务的实时日志
docker logs –tail 20 容器id/容器名称         查看容器服务后20行日志
docker inspect 容器id         查看容器详细信息
docker stats 容器id         查看容器详情(CPU占用、内存使用、IO数)

docker数据持久化命令

  1. -v mysql-volume:/var/lib/mysql        通过volume将物理机/var/lib/docker/volumes/mysql-volume/_data目录和容器虚拟机/var/lib/mysql目录绑定
  2. -v /root/data/mysql:/var/lib/mysql          直接将物理机/root/data/mysql目录和容器虚拟机/var/lib/mysql目录绑定

docker volume ls         查看docker持久化存储情况
docker exec -it mysql bash         进入mysql容器中
ls /var/lib/mysql         可以在这个目录下查看到mysql的数据
docker volume inspect volume_name         可以查看到物理机中的目录
ls /var/lib/docker/volumes/eefab808edf91781c5605f4d8c1e42074af848c8ebb112305fcf2a4d8769a663/_data         可以发现物理机目录下和mysql容器/var/lib/mysql目录下内容相同
docker volume create mysql-volume         创建volume
docker run -d –name mysql5.7 -v mysql-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7         创建容器并使用自定义的volume
ls /var/lib/docker/volumes/mysql-volume/_data         查看自定义volume对应的物理机目录下内容
-v mysql-volume:/var/lib/mysql就等同于在Dockerfile中定义的VOLUME命令

验证持久化效果:

mysql> create database db_test;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db_test            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye
root@29d465c7bc45:/# exit
exit
[root@localhost ~]# docker exec -it mysql5.7 bash
root@29d465c7bc45:/# ls /var/lib/mysql
auto.cnf    ca.pem	     client-key.pem  ib_buffer_pool  ib_logfile1  ibtmp1  performance_schema  public_key.pem   server-key.pem
ca-key.pem  client-cert.pem  db_test	     ib_logfile0     ibdata1	  mysql   private_key.pem     server-cert.pem  sys
root@29d465c7bc45:/# exit
exit
[root@localhost ~]# ls /var/lib/docker/volumes/mysql-volume/_data
auto.cnf    ca.pem           client-key.pem  ib_buffer_pool  ib_logfile0  ibtmp1  performance_schema  public_key.pem   server-key.pem
ca-key.pem  client-cert.pem  db_test         ibdata1         ib_logfile1  mysql   private_key.pem     server-cert.pem  sys
[root@localhost ~]# 

防火墙相关命令

systemctl  stop firewalld          关闭防火墙
systemctl disable firewalld         禁止开机启动防火墙
systemctl status  firewalld          查看是否关闭防火墙

搭建jenkins

docker pull jenkins         拉取jenkins镜像
docker tag jenkins myjenkins         给镜像重命名
mkdir -p /opt/jenkins 创建目录/opt/jenkins
chmod 777 /opt/jenkins         对文件夹进行授权
docker run -d –name jenkins -p 10240:8080 -v /opt/jenkins:/var/jenkins_home jenkins        根据jenkins镜像创建容器并启动
docker start jenkins         启动jenkins容器
docker exec -it jenkins bash         进入jenkins容器
cat /var/jenkins_home/secrets/initialAdminPassword         查看jenkins admin password

docker 安装及镜像、容器操作

1、centos7安装docker engine

参考文档:https://docs.docker.com/engine/install/centos/

(1)卸载之前的docker

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

(2)安装yum-utils,设置docker仓库

sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

(3)查看可以安装的版本

yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64            3:20.10.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.7-3.el7                    @docker-ce-stable
docker-ce.x86_64            3:20.10.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.4-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.3-3.el7                    docker-ce-stable

(5)安装最新或指定版本

安装最新版本:

sudo yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

安装指定版本20.10.7:

sudo yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io docker-compose-plugin

(6)启动docker并设置为开机启动

sudo systemctl start docker && sudo systemctl enable docker

(7)测试docker安装是否成功

sudo docker run hello-world

(8)配置镜像加速器

设置方式参照:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

2、Docker安装tomcat和mysql

(1)tomcat:8.0

# 拉取镜像:
docker pull tomcat:8.0
# 根据镜像创建容器:
docker run -d --name tomcat01 -p 8088:8080 tomcat:8.0

(2)mysql

# -d设置后台启动, --name给容器命名, -p端口映射, -e添加环境变量
docker run -d --name mysql5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=jack666 --
privileged mysql:5.7

3、将jar包制作镜像

(1)创建一个Spring Boot项目,写一个controller

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello Docker!!!";
    }
}

注意在项目的pom.xml中一定要加下面配置,否则启动jar镜像会报错:no main manifest attribute

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>

(2)mvn clean package,并在target下找到jar包,如”springboot-demo2-1.0-SNAPSHOT.jar”

(3)上传”springboot-demo2-1.0-SNAPSHOT.jar”到centos,并创建Dockerfile文件

FROM openjdk:8
COPY springboot-demo2-1.0-SNAPSHOT.jar sb-jar-image.jar
CMD ["java","-jar","sb-jar-image.jar"]

或者

# 基础镜像使用java
FROM java:8
#将jar包添加到容器中并更名为: sb-jar-image.jar
ADD   springboot-demo2-1.0-SNAPSHOT.jar   sb-jar-image.jar
#运行jar包
RUN bash -c 'touch sb-jar-image.jar'
ENTRYPOINT ["java","-jar","sb-jar-image.jar"]
#暴露端口
EXPOSE 8080

(4)基于Dockerfile构建镜像并查看

docker build -t sb-jar-image .
docker images

(5)基于image创建container

docker run -d --name sb-jar -p 8080:8080 sb-jar-image

(6)查看启动日志:docker logs sb-jar

(7)宿主机上访问curl localhost:8080/hello

[root@localhost ~]# curl localhost:8080/hello
Hello Docker!!![root@localhost ~]# 

或者主机浏览器访问虚拟机ip:8080/hello

4、阿里镜像仓库

阿里镜像仓库地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

(1)进入个人实例:https://cr.console.aliyun.com/cn-hangzhou/instance/dashboard

(2)创建命名空间,比如 lucifer-k8s

(3)在centos上登录

[root@localhost ~]# sudo docker login --username=lucifer_ztl registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

(4)给image打tag

[root@localhost ~]# sudo docker tag 84ea3746c8e6 registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository:v1.0
[root@localhost ~]# docker images
REPOSITORY                                                             TAG       IMAGE ID       CREATED         SIZE
sb-jar-image                                                           latest    84ea3746c8e6   2 days ago      678MB
registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository   v1.0      84ea3746c8e6   2 days ago      678MB
openjdk                                                                8         e24ac15e052e   11 months ago   526MB
hello-world                                                            latest    feb5d9fea6a5   14 months ago   13.3kB
java      

(5)推送镜像到镜像仓库

[root@localhost ~]# sudo docker push registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository:v1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository]
0634aee8ab8a: Pushed 
a0d2245cc87f: Pushed 
35c20f26d188: Pushed 
c3fe59dd9556: Pushed 
6ed1a81ba5b6: Pushed 
a3483ce177ce: Pushed 
ce6c8756685b: Pushed 
30339f20ced0: Pushed 
0eb22bfb707d: Pushed 
a2ae92ffcd29: Pushed 
v1.0: digest: sha256:55044767d6a5abcf62595732449f3dafb3cc7ce09f5ac222f37e56d915211003 size: 2424

(6)下载镜像并运行

[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository:v1.0
[root@localhost ~]# docker run -d --name docker01 -p 8081:8081 registry.cn-hangzhou.aliyuncs.com/lucifer-k8s/lucifer-k8s-repository:v1.0

mysql高可用集群保证数据一致性:

(1)从库连接主库,(2)主库dump线程获取binlog发送给从库,(3)从库IO线程写数据到relay log,(4)sql线程读取relay log执行sql更新数据到从库

PXC

Percona Xtradb Cluster

多主+同步复制+强一致性

筛选 -> 网上填表 -> 笔试(算法,选择) -> 面试(公司介绍+个人介绍+架构设计+基础)  -> 体检


版权声明:本文为luciferlongxu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。