1.在centos中安装docker
-
Docker 要求 CentOS 系统的内核版本高于 3.10,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。
通过 uname -r 命令查看你当前的内核版本
-
如果安装过请先卸载
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
-
安装依赖设置yum仓库
安装依赖
yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
设置仓库
yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
-
安装docker
yum install docker-ce docker-ce-cli containerd.io
-
启动并加入开机启动
systemctl start docker
systemctl enable docker
-
验证是否成功
docker version
docker run hello-world
显示如下即安装成功!
[root@iZ2ze68ge5c1uwlkmnb9ixZ zcapp]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
2.docker的命令
1.因为docker的镜像服务器位于国外,所以可以使用国内的镜像服务器来下载docker镜像
配置方式
使用root权限,在/etc/docker/文件下创建一个daemon.json 文件
使用vi daemon.json 打开文件添加如下内容
{
"registry-mirrors" : ["https://docker.mirrors.ustc.edu.cn"]
}
拉取镜像
docker pull [options] NAME[:TAG]
- 通过此命令可以docker远程仓库拉取镜像到本地.
- name是拉取镜像的名称,:TAG表示是可选的,如果不选表明时latest,如果选择表明是指定版本的.
- options是拉去的一些参数.
- 当不加请求地址的时候回去docker的官网拉取镜像.
docker images [options] [REPOSITORY[:TAG]]
options是选项,后面是指定镜像的名称.这个用的不多,可能当本地镜像非常多的时候要指定查看某一个镜像.
docker run [options] IMAGE[:TAG] [COMMAND] [ARG…]
- IMAGE是镜像的名字
- COMMAND是运行起来的时候要执行什么命令.
- ARG表示这条命令运行需要的参数.
docker run 【可选参数】 镜像名称
docker ps
- 查看当前正在运行的容器(可运行多个相同的镜像产生不同的容器实例)
- -a 列出当前正在运行的容器+带出历史运行过的容器
- -q 只列出容器的编号
- -n? 显示最近创建的容器
docker rm 容器id
- 删除指定容器,不能删除正在运行的容器
- docker rm -f 容器id 使用-f可以强制删除正在运行的容器
docker rm $(docker ps -qa)
- 删除全部容器
docker stop 容器id
- 停止当前正在运行的容器
docker start 容器id
- 启动停止运行的容器
docker kill 容器id
- 强制停止当前容器
docker kill 容器id
- 强制停止当前容器
docker exec -it 容器id /bin/bash
- 进入容器开启一个新终端,可以在你们操作
- 使用ctrl+d或者exit的方式返回退出
docker cp 文件名称 容器id:容器内部路径
- 将主机的文件复制到容器内部的指定路径
docker cp 容器Id :容器内部路径 目的主机路径
- 从容器拷贝内容到主机上
docker rmi -f '镜像id’
- 删除镜像
3.docker打包springboot项目
-
1IDEA中安装Docker工具
-
使用Maven中的package打包
-
加入一个名为Dockerfile的文件
内容如下
FROM java:8 COPY *.jar /app.jar CMD ["--server.port=8080"] EXPOSE 8080 ENTRYPOINT ["java","-jar","app.jar"]
-
将target文件夹下的以jar结尾的问价和Dockerfile上传之服务器
-
使用**[docker build -t 镜像的名称 .]**进行镜像的打包
-
使用docker images可以查看刚才打包的镜像
出现如下情况是JDK与Tomcat版本不适配[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4rlQW4Q4-1610459583203)(docker的使用.assets/image-20201214004438726.png)]
在打包成镜像时,应当pull相应的Java版本或者是tomcat版本
FROM openjdk:15-jdk-alpine//确定正确的版本
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
4.docker导出镜像文件
通过docker save 命令导出镜像文件
docker save 镜像名 -o 导出文件名
建议导出为.tar文件
通过docker load 命令导入镜像
docker load -i 镜像包
docker load -i centos_kn.tar
docker exec -it 764327167cf9 /bin/bash
COPY dist /usr/vuejs/nginx/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d1PQdd1h-1610459583205)(docker的使用.assets/image-20201216162637020.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uZOvYRva-1610459583207)(docker的使用.assets/image-20201216162642189.png)]
5.docker打包vue项目
制作Dockerfile
FROM nginx:1.16.1
COPY nginx.conf /etc/nginx/nginx.conf
COPY dist /usr/local/dist1/dist
CMD ["nginx","-g","daemon off;"]
制作nginx.conf
#uer nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 8080;
server_name localhost;
location / {
root /usr/local/dist1/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
location ^~ /api {
rewrite ^/api/(.*)$ /$1 break;
#proxy_pass http://121.196.40.164:8080;
proxy_pass http://47.100.36.240:8080; //设置请求转发。
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
}
执行docker build -t name .制作镜像。