docker 常用命令
镜像命令
搜索镜像:docker search
- docker search 镜像名字
例子:docker search mysql
[root@iZ2vc2rht08hck1x6us154Z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14736 [OK]
mariadb MariaDB Server is a high performing open sou… 5622 [OK]
percona Percona Server is a fork of the MySQL relati… 623 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 919 [OK]
bitnami/mysql Bitnami MySQL Docker Image 105 [OK]
bitnami/mysqld-exporter 6
cimg/mysql 2 [OK]
- docker search 镜像名字:版本号
例子:docker search mysql:5.7
[root@iZ2vc2rht08hck1x6us154Z ~]# docker search mysql:5.7
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
migs/mysql-5.7 mysql:5.7 plus some basic enhancements 2 [OK]
idyci/mysql mysql:5.7.25 1
xiao806852034/mysql57 基于 mysql:5.7 0
framgiaciteam/mysql-256 FROM mysql:5.7 CMD ["--block_encryption_mod… 0
metatronx/mysql mysql:5.7 + druid account & database 0
haswind/mysql mysql:5.7 0
stsiwo/sts-blog-db base image mysql:5.7.28 0
NAME:镜像的名称。
DESCRIPTION:镜像的描述或说明。
STARS:镜像的星级评分,表示镜像的受欢迎程度。更高的星级通常表示更受欢迎的镜像。
OFFICIAL:一个指示标志,表示官方的Docker镜像。官方镜像是由Docker官方团队维护和支持的镜像。
AUTOMATED:一个指示标志,表示镜像是否通过自动化流程进行构建和更新
拉取镜像:docker pull
- docker pull 镜像名字:版本号
例子:docker pull redis:6.0.1
[root@iZ2vc2rht08hck1x6us154Z ~]# docker pull redis:6.0.1
6.0.1: Pulling from library/redis
54fec2fa59d0: Pull complete
9c94e11103d9: Pull complete
04ab1bfc453f: Pull complete
a22fde870392: Pull complete
def16cac9f02: Pull complete
1604f5999542: Pull complete
Digest: sha256:1ef229049219dc2a64df9a83a0b165d79990392cfa6273a0f960fbb61964f812
Status: Downloaded newer image for redis:6.0.1
docker.io/library/redis:6.0.1
- docker pull 镜像名字
默认拉取最新的镜像
查看镜像:docker images
- docker images 列出本地所有镜像详细信息
列子:docker images
[root@iZ2vc2rht08hck1x6us154Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java-demo v1.0 c6e1da18a973 18 hours ago 370MB
nginx latest 605c77e624dd 24 months ago 141MB
redis latest 7614ae9453d1 2 years ago 113MB
mysql 5.7 c20987f18b13 2 years ago 448MB
- docker images -a 列出本地所有镜像详细信息
等价于docker images
列子:docker images -a
[root@iZ2vc2rht08hck1x6us154Z ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
java-demo v1.0 c6e1da18a973 18 hours ago 370MB
nginx latest 605c77e624dd 24 months ago 141MB
redis latest 7614ae9453d1 2 years ago 113MB
mysql 5.7 c20987f18b13 2 years ago 448MB
- docker images -q 列出本地所有镜像唯一标识id
[root@iZ2vc2rht08hck1x6us154Z ~]# docker images -q
c6e1da18a973
605c77e624dd
7614ae9453d1
c20987f18b13
- docker images -aq 列出本地所有镜像唯一标识id
例子:docker images -aq
等价于docker images -q
[root@iZ2vc2rht08hck1x6us154Z ~]# docker images -aq
c6e1da18a973
605c77e624dd
7614ae9453d1
c20987f18b13
删除镜像:docker rmi
- docker rmi 镜像id
例子:**docker rmi **
[root@iZ2vc2rht08hck1x6us154Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java-demo v1.0 c6e1da18a973 19 hours ago 370MB
nginx latest 605c77e624dd 24 months ago 141MB
redis latest 7614ae9453d1 2 years ago 113MB
mysql 5.7 c20987f18b13 2 years ago 448MB
redis 6.0.1 f9b990972689 3 years ago 104MB
[root@iZ2vc2rht08hck1x6us154Z ~]# docker rmi f9b990972689
Untagged: redis:6.0.1
Untagged: redis@sha256:1ef229049219dc2a64df9a83a0b165d79990392cfa6273a0f960fbb61964f812
Deleted: sha256:f9b9909726890b00d2098081642edf32e5211b7ab53563929a47f250bcdc1d7c
Deleted: sha256:7bd7286c61dee81eb691f022d070202ba2840574c460f6dcbc21792f8bf22a7f
Deleted: sha256:ba618723adf8eb28ec3d2b2003442599e3f947b4d66875b80dc6582c61f89a27
Deleted: sha256:cd3c331f934414f0d7a2a3ceb80935a54252dba553f1c1ee823961a07e8d1957
Deleted: sha256:09f9b5d7722b181ae2acd61396c20549afdea66ee31f921850191db5afe6738a
Deleted: sha256:3bd2921d0d3dd52dbcdc462f35b11cb89df901205284b0fea03c19bb8359907a
Deleted: sha256:c2adabaecedbda0af72b153c6499a0555f3a769d52370469d8f6bd6328af9b13
- dockers rmi -f 镜像id: 强制删除镜像id
例子:**docker rmi -f **
运行镜像:docker run
docker run 命令参数 镜像名
docker run [参数] 镜像名
参数
--name=="名字":指定容器的名称,如果正在运行该名称的容器,会报错。
--rm:用完即删除,通常用来测试。
-d:后台方式运行。
-it:使用交互方式运行,可以进入容器查看内容。
-e:指定运行环境。
-P:随机指定端口。
-p:指定容器的端口,如:-p 8080:8080。还可以有以下写法:
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
-v 主机目录:容器目录 。目录映射,容器目录挂载到宿主机目录,格式: <host目录>:<容器目录>。
docker run -it 镜像名 /bin/bash : 运行镜像并进入容器内
例子:docker run -it 镜像名字 /bin/bash
[root@iZ2vc2rht08hck1x6us154Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java-demo v1.0 c6e1da18a973 20 hours ago 370MB
nginx latest 605c77e624dd 24 months ago 141MB
redis latest 7614ae9453d1 2 years ago 113MB
mysql 5.7 c20987f18b13 2 years ago 448MB
redis 6.0.1 f9b990972689 3 years ago 104MB
[root@iZ2vc2rht08hck1x6us154Z ~]# docker run -it redis:6.0.1 /bin/bash
root@69d9ed6e4c59:/data# pwd
/data
root@69d9ed6e4c59:/data# ls
root@69d9ed6e4c59:/data#
root@69d9ed6e4c59:/data# exit
exit
#退出容器
docker 后台运行
docker run -d -p 8888:80 \
-v /data/html:/usr/share/nginx/html:ro \
-v /data/conf/nginx.conf:/etc/nginx/nginx.conf \
--name mynginx \
nginx
-d后台运行docker
-p 主机端口:容器端口
-v 目录映射,容器目录挂载到宿主机目录.通过修改主机,同步到容器目录
--name 起别名
nginx 镜像名
容器命令
查看容器 :docker ps
- docker ps
docker ps [参数]
参数
-a:查看所有容器(包括正在运行的和已经停止的)。
-n:显示最近创建的容器,设置显示个数。
-q:只显示容器的编号。
- docker ps 查看正在运行docker
[root@iZ2vc2rht08hck1x6us154Z ~]# clear
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df1d6b3e9740 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 20 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
- docker ps -a 查看所有容器(停止和运行)
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69d9ed6e4c59 redis:6.0.1 "docker-entrypoint.s…" 14 minutes ago Exited (127) 11 minutes ago eager_gates
bf3ae3fe8406 redis:6.0.1 "docker-entrypoint.s…" 14 minutes ago Exited (0) 14 minutes ago jolly_pare
6a7e17a2ed10 mysql:5.7 "docker-entrypoint.s…" 18 minutes ago Exited (1) 18 minutes ago heuristic_banach
df1d6b3e9740 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 20 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
de5d2f91e033 605c77e624dd "/docker-entrypoint.…" 20 hours ago Exited (0) 20 hours ago pedantic_dirac
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
9a05471456dc nginx "/docker-entrypoint.…" 22 hours ago Exited (0) 22 hours ago nostalgic_ganguly
512c447bc56e nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago agitated_wescoff
- docker ps -q查看所有容器编号(运行)
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps -q
df1d6b3e9740
9d59e3a0a559
95e24b2ed28b
e8f8898348e2
运行容器:docker start
docker start 容器id:启动容器
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps #当前运行容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df1d6b3e9740 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 20 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps -a #全部容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69d9ed6e4c59 redis:6.0.1 "docker-entrypoint.s…" 16 minutes ago Exited (127) 13 minutes ago eager_gates
bf3ae3fe8406 redis:6.0.1 "docker-entrypoint.s…" 16 minutes ago Exited (0) 16 minutes ago jolly_pare
6a7e17a2ed10 mysql:5.7 "docker-entrypoint.s…" 19 minutes ago Exited (1) 19 minutes ago heuristic_banach
df1d6b3e9740 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 20 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
de5d2f91e033 605c77e624dd "/docker-entrypoint.…" 20 hours ago Exited (0) 20 hours ago pedantic_dirac
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
9a05471456dc nginx "/docker-entrypoint.…" 22 hours ago Exited (0) 22 hours ago nostalgic_ganguly
512c447bc56e nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago agitated_wescoff
[root@iZ2vc2rht08hck1x6us154Z ~]# docker start 69d9ed6e4c59 #运行新的容器
69d9ed6e4c59
停止容器:docker stop
docker stop 容器id:停止容器
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps #查看运行容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69d9ed6e4c59 redis:6.0.1 "docker-entrypoint.s…" 18 minutes ago Up About a minute 6379/tcp eager_gates
df1d6b3e9740 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 20 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
[root@iZ2vc2rht08hck1x6us154Z ~]# docker stop 69d9ed6e4c59 #停止容器
69d9ed6e4c59
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df1d6b3e9740 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 20 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 21 hours ago Up 21 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
关闭容器出现错误:Error response from daemon: cannot stop container: ef8a2ff7151e: permission denied
参考链接:docker错误:Error response from daemon: cannot stop container_Tomatosky的博客-CSDN博客
重启容器:docker restart
docker restart 容器id
进入容器:docker exec
docker exec 参数 容器id/容器名字 路径
docker exec [参数] 容器 路径
参数
-d:后台运行。
-it:交互模式进入。
[root@iZ2vc2rht08hck1x6us154Z ~]# docker exec -it mynginx /bin/bash
root@df1d6b3e9740:/# pwd
退出容器:exit
exit
[root@iZ2vc2rht08hck1x6us154Z ~]# docker exec -it mynginx /bin/bash # 进入容器
root@df1d6b3e9740:/# pwd
/
root@df1d6b3e9740:/# exit # 退出容器
exit
[root@iZ2vc2rht08hck1x6us154Z ~]# pwd
/root
删除容器:docker rm
docker rm 容器id
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69d9ed6e4c59 redis:6.0.1 "docker-entrypoint.s…" 25 minutes ago Exited (0) 6 minutes ago eager_gates
bf3ae3fe8406 redis:6.0.1 "docker-entrypoint.s…" 25 minutes ago Exited (0) 25 minutes ago jolly_pare
6a7e17a2ed10 mysql:5.7 "docker-entrypoint.s…" 28 minutes ago Exited (1) 28 minutes ago heuristic_banach
df1d6b3e9740 nginx "/docker-entrypoint.…" 3 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 21 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
de5d2f91e033 605c77e624dd "/docker-entrypoint.…" 21 hours ago Exited (0) 20 hours ago pedantic_dirac
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
9a05471456dc nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago nostalgic_ganguly
512c447bc56e nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago agitated_wescoff
[root@iZ2vc2rht08hck1x6us154Z ~]# docker rm 69d9ed6e4c59 # 删除容器
69d9ed6e4c59
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf3ae3fe8406 redis:6.0.1 "docker-entrypoint.s…" 25 minutes ago Exited (0) 25 minutes ago jolly_pare
6a7e17a2ed10 mysql:5.7 "docker-entrypoint.s…" 28 minutes ago Exited (1) 28 minutes ago heuristic_banach
df1d6b3e9740 nginx "/docker-entrypoint.…" 3 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 21 hours ago Up 20 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
de5d2f91e033 605c77e624dd "/docker-entrypoint.…" 21 hours ago Exited (0) 20 hours ago pedantic_dirac
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
9a05471456dc nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago nostalgic_ganguly
512c447bc56e nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago
杀死容器: docker kill(运行)
docker kill 容器id
[root@iZ2vc2rht08hck1x6us154Z ~]# docker kill bf3ae3fe8406
Error response from daemon: Cannot kill container: bf3ae3fe8406: Container bf3ae3fe84068a50d942e68eab413daaf0fc6476fd9ccfb1a4db6123f392be51 is not running
[root@iZ2vc2rht08hck1x6us154Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df1d6b3e9740 nginx "/docker-entrypoint.…" 3 hours ago Up 2 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
9d59e3a0a559 java-demo:v1.0 "java -jar /app.jar" 21 hours ago Up 21 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sky-app
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 21 hours ago Up 21 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 22 hours ago Up 22 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
[root@iZ2vc2rht08hck1x6us154Z ~]# docker kill df1d6b3e9740 #杀死运行容器
df1d6b3e9740
容器内文件拷贝到主机:docker cp
docker cp 容器id:容器内路径 目的主机路径
# 容器
root@df1d6b3e9740:/test# pwd
/test
root@df1d6b3e9740:/test# ls
test.text
#主机
[root@iZ2vc2rht08hck1x6us154Z ~]# docker cp df1d6b3e9740:/test /root
Successfully copied 2.05kB to /root
#查看cp
数据卷
dockerfile
docker 流程图
基础知识
- dockerfile 文件所在目录为上下文目录:即dockerfile文件 所在目录下所有文件会被打包成镜像
- Docker以从上到下的顺序运行Dockerfile的指令。为了指定基本映像,第一条指令必须是FROM。一个声明以
#
字符开头则被视为注释。 所有命令全是大写 - dockerfile在构建指令过程中存在缓存,相同的会使用缓存
命令集合
- FROM:指定基础镜像,必须为第一个命令
格式:
FROM <image>
FROM <image>:<tag>
FROM <image>@<digest>
示例:
FROM mysql:5.6
注:
tag或digest是可选的,如果不使用这两个值时,会使用latest版本的基础镜像
例子:From mysql:5.6
- RUN:构建镜像时执行的命令
格式:
RUN <command>
exec执行
格式:
RUN ["executable", "param1", "param2"]
示例:
RUN apk update
RUN ["/etc/execfile", "arg1", "arg1"]
注:
RUN指令创建的中间镜像会被缓存,并会在下次构建中使用。如果不想使用这些缓存镜像,可以在构建时指定--no-cache参数,如:docker build --no-cache
**例子:RUN yum install -y vim **
例子:RUN ["yum","install","-y","vim"]
FROM centos:7
RUN yum install -y vim
测试:
[root@iZ2vc2rht08hck1x6us154Z dockerfile]# docker build -t centos7 . #运行dockerfile
[+] Building 148.4s (6/6) FINISHED docker:default
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load build definition from dockerfile
=> => transferring dockerfile: 75B
=> [internal] load metadata for docker.io/library/centos:7
=> [1/2] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
=> => resolve docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
=> => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB
=> => sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 1.20kB / 1.20kB
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB
=> => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc
=> [2/2] RUN yum install -y vim
=> exporting to image
=> => exporting layers
=> => writing image sha256:7e8bd174d9bf5948b897ebb31d060f2ee52b4a36ce90a2957c703c6299aa987b
=> => naming to docker.io/library/centos7
- EXPOSE:指定于外界交互的端口
格式:
EXPOSE <port> [<port>...]
示例:
EXPOSE 80 443
EXPOSE 8080
EXPOSE 11211/tcp 11211/udp
注:
EXPOSE并不会让容器的端口访问到主机。要使其可访问,需要在docker run运行容器时通过-p来发布这些端口,或通过-P参数来发布EXPOSE导出的所有端口
注意事项:
如果不指定expose 暴露端口 ,即时使用docker run -p 主机端口:容器端口,其容器端口也不会生效,即此命令也会失效
例子 EXPOSE 80 443
- WORKDIR:工作目录,类似于cd命令,目录不存在会自动创建
格式:
WORKDIR /path/to/workdir
示例:
WORKDIR /a (这时工作目录为/a)
WORKDIR b (这时工作目录为/a/b)
WORKDIR c (这时工作目录为/a/b/c)
注:
通过WORKDIR设置工作目录后,Dockerfile中其后的命令RUN、CMD、ENTRYPOINT、ADD、COPY等命令都会在该目录下执行。在使用docker run运行容器时,可以通过-w参数覆盖构建时所设置的工作目录。
注意
可以写绝对路径,也可以写相对路径
例子 WORKDIR /data :当使用docker run -it 创建的镜像id :会进入到容器中/data目录下,不存在docker自动创建
- ADD:将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问网络资源,类似wget
格式:
ADD <src>... <dest>
ADD ["<src>",... "<dest>"] 用于支持包含空格的路径
示例:
ADD hom* /mydir/ # 添加所有以"hom"开头的文件
ADD hom?.txt /mydir/ # ? 替代一个单字符,例如:"home.txt"
ADD test relativeDir/ # 添加 "test" 到 `WORKDIR`/relativeDir/
ADD test /absoluteDir/ # 添加 "test" 到 /absoluteDir/
FROM centos:7
RUN yum install -y vim
WORKDIR /data
WORKDIR bb
ADD test.txt /data/bb
测试
[root@iZ2vc2rht08hck1x6us154Z dockerfile]# docker build -t centos7:1 . #打包docker
[+] Building 0.8s (10/10) FINISHED docker:default
=> [internal] load build definition from dockerfile
=> => transferring dockerfile: 121B
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load metadata for docker.io/library/centos:7
=> [1/5] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s
=> [internal] load build context
=> => transferring context: 49B
=> CACHED [2/5] RUN yum install -y vim
=> [3/5] WORKDIR /data
=> [4/5] WORKDIR bb
=> [5/5] ADD test.txt /data/bb
=> exporting to image
=> => exporting layers
=> => writing image sha256:e36ba9e6cf4c0f1d99f66f85b617a644b53843ff02c3e5815d48a45af6c39466
=> => naming to docker.io/library/centos7:1
[root@iZ2vc2rht08hck1x6us154Z dockerfile]# ll
total 8
-rw-r--r-- 1 root root 84 Dec 29 21:44 dockerfile
-rw-r--r-- 1 root root 0 Dec 29 21:44 DOCKERFILE
-rw-r--r-- 1 root root 14 Dec 29 21:45 test.txt
# 进入打包好的docker中
[root@iZ2vc2rht08hck1x6us154Z dockerfile]# docker run -it centos7:1
[root@a076665e1bc5 bb]# pwd #默认路径等于WORKDIR /data/bb
/data/bb
[root@a076665e1bc5 bb]# ll # ADD 命令copy 文件
total 4
-rw-r--r-- 1 root root 14 Dec 29 13:45 test.txt
[root@a076665e1bc5 bb]#
- COPY::将本地文件添加到容器中====功能类似ADD,但是是不会自动解压文件,也不能访问网络资源
- VOLUME:用于指定持久化目录
格式:
VOLUME ["/path/to/dir"]
示例:
VOLUME ["/data"]
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
注:
一个卷可以存在于一个或多个容器的指定目录,该目录可以绕过联合文件系统,并具有以下功能:
1 卷可以容器间共享和重用
2 容器并不一定要和其它容器共享卷
3 修改卷后会立即生效
4 对卷的修改不会对镜像产生影响
5 卷会一直存在,直到没有任何容器在使用它
数据卷技术VOLUME
例子: VOLUME /data: **表示容器中/data 可以被挂载出来 和主宿主机 **
- ENV:设置环境变量
格式:
ENV <key> <value> #<key>之后的所有内容均会被视为其<value>的组成部分,因此,一次只能设置一个变量
ENV <key>=<value> ... #可以设置多个变量,每个变量为一个"<key>=<value>"的键值对,如果<key>中包含空格,可以使用\来进行转义,也可以通过""来进行标示;另外,反斜线也可以用于续行
示例:
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat=fluffy
FROM centos:7
RUN yum install -y vim
WORKDIR /data
WORKDIR bb
ADD test.txt /data/bb
使用ENV
FROM centos:7
RUN yum install -y vim
WORKDIR /data
WORKDIR bb
ENV base /data/bb
ADD test.txt $base$
- ENTRYPOINT:配置容器,使其可执行化。配合CMD可省去"application",只使用参数。
格式:
ENTRYPOINT ["executable", "param1", "param2"] (可执行文件, 优先)
ENTRYPOINT command param1 param2 (shell内部命令)
示例:
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
注:
ENTRYPOINT与CMD非常类似,不同的是通过docker run执行的命令不会覆盖ENTRYPOINT,而docker run命令中指定的任何参数,都会被当做参数再次传递给ENTRYPOINT。Dockerfile中只允许有一个ENTRYPOINT命令,多指定时会覆盖前面的设置,而只执行最后的ENTRYPOINT指令
- CMD:构建容器后调用,也就是在容器启动时才进行调用
格式:
CMD ["executable","param1","param2"] (执行可执行文件,优先)
CMD ["param1","param2"] (设置了ENTRYPOINT,则直接调用ENTRYPOINT添加参数)
CMD command param1 param2 (执行shell内部命令)
示例:
CMD echo "This is a test." | wc -
CMD ["/usr/bin/wc","--help"]
注:
CMD不同于RUN,CMD用于指定在容器启动时所要执行的命令,而RUN用于指定镜像构建时所要执行的命令。
命令图片
运行dockerfile
docker build
-t selfname:tag 自己起的镜像名字:版本号
-f dockerfile文件所在目录
. 别忘了还有点
docker build -t mycentos:v0 -f /root/data/dockerfile/ .
dockerfile实战 springboot
- 构建dockerfile 文件
FROM openjdk:8-jre #基于java-8 镜像构建
WORKDIR /app #进入容器时所在的位置
ADD test.jar app.jar #将*.jar 复制到工作目录/app 同时修改为app.jar
EXPOSE 8080# 容器暴露端口
ENTRYPOINT ["java","-jar"] # 启动应用固定命令
CMD ["app.jar"] # 执行命令
- 运行dockerfile文件
docker build -t springboot .
[root@iZ2vc2rht08hck1x6us154Z springboot]# docker build -t springboot:1 .
[+] Building 197.8s (8/8) FINISHED docker:default
=> [internal] load build definition from dockerfile
=> => transferring dockerfile: 145B
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load metadata for docker.io/library/openjdk:8
=> [1/3] FROM docker.io/library/openjdk:8@sha256:8a9d5c43f540e8d0c003c723a2c8bd20ae350a2efed6fb5719cae33b026f8e7c
=> => resolve docker.io/library/openjdk:8@sha256:8a9d5c43f540e8d0c003c723a2c8bd20ae350a2efed6fb5719cae33b026f8e7c
=> => sha256:9b829c73b52b92b97d5c07a54fb0f3e921995a296c714b53a32ae67d19231fcd 5.15MB / 5.15MB
=> => sha256:cb5b7ae361722f070eca53f35823ed21baa85d61d5d95cd5a95ab53d740cdd56 10.87MB / 10.87MB
=> => sha256:8a9d5c43f540e8d0c003c723a2c8bd20ae350a2efed6fb5719cae33b026f8e7c 1.29kB / 1.29kB
=> => sha256:9413213335131c4e06b21a8e379bd9b6a20afcd6b762540463d5f7c72942dcdd 1.79kB / 1.79kB
=> => sha256:e24ac15e052e04a3462ef4984b5d83214f7f65c06f54acd3745a1926e226be16 7.81kB / 7.81kB s
=> => sha256:0e29546d541cdbd309281d21a73a9d1db78665c1b95b74f32b009e0b77a6e1e3 54.92MB / 54.92MB
=> => sha256:6494e4811622b31c027ccac322ca463937fd805f569a93e6f15c01aade718793 54.57MB / 54.57MB
=> => sha256:668f6fcc5fa5532a1dd793456f64daf954192e0521fd65d42af584d5e2d93f55 5.42MB / 5.42MB
=> => sha256:c0879393b07ef5fa816c292b00e3eb4945890bc2a69ab0d1754240cbe9cedf21 212B / 212B
=> => sha256:bef50c41a74d450f2d708be5971c3ba635ed1a714af7f4fa1497886adb2fa734 106.00MB / 106.00MB
=> => extracting sha256:0e29546d541cdbd309281d21a73a9d1db78665c1b95b74f32b009e0b77a6e1e3
=> => extracting sha256:9b829c73b52b92b97d5c07a54fb0f3e921995a296c714b53a32ae67d19231fcd
=> => extracting sha256:cb5b7ae361722f070eca53f35823ed21baa85d61d5d95cd5a95ab53d740cdd56
=> => extracting sha256:6494e4811622b31c027ccac322ca463937fd805f569a93e6f15c01aade718793
=> => extracting sha256:668f6fcc5fa5532a1dd793456f64daf954192e0521fd65d42af584d5e2d93f55
=> => extracting sha256:c0879393b07ef5fa816c292b00e3eb4945890bc2a69ab0d1754240cbe9cedf21
=> => extracting sha256:bef50c41a74d450f2d708be5971c3ba635ed1a714af7f4fa1497886adb2fa734
=> [internal] load build context
=> => transferring context: 74.45MB
=> [2/3] WORKDIR /app
=> [3/3] ADD test.jar app.jar
=> exporting to image
=> => exporting layers
=> => writing image sha256:190539b052b810155ff8c4d51350d472d3e9da8b5a4b2383201ceddc6a2178cd
=> => naming to docker.io/library/springboot:1
[root@iZ2vc2rht08hck1x6us154Z springboot]# ls
dockerfile test.jar
- 运行容器
docker run -d -p 8080:8080 --name sb springboot:1
[root@iZ2vc2rht08hck1x6us154Z springboot]# docker run -d -p 8080:8080 --name sb springboot:1
d8fd5b1fc935ac27b7fcf1a3147e2c350b9fb96a94a5700e68ecf38455630dcc
[root@iZ2vc2rht08hck1x6us154Z springboot]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d8fd5b1fc935 springboot:1 "java -jar app.jar" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sb
df1d6b3e9740 nginx "/docker-entrypoint.…" 24 hours ago Up 21 hours 0.0.0.0:8888->80/tcp, :::8888->80/tcp mynginx
95e24b2ed28b mysql:5.7 "docker-entrypoint.s…" 43 hours ago Up 43 hours 33060/tcp, 0.0.0.0:3333->3306/tcp, :::3333->3306/tcp mysql
e8f8898348e2 redis:latest "docker-entrypoint.s…" 43 hours ago Up 43 hours 0.0.0.0:6666->6379/tcp, :::6666->6379/tcp myredis
idea 配合dockerfile 部署springboot
详细博客:https://www.cnblogs.com/aaronlinv/p/15228488.html
参考博客
https://www.cnblogs.com/ccbloom/p/11174186.html
https://www.cnblogs.com/linyouyi/p/11407875.html
https://www.cnblogs.com/edisonchou/p/dockerfile_inside_introduction.html
DockerCompose
dockercompose 安装
[root@iZ2vc2rht08hck1x6us154Z ~]# yum install -y docker-compose
[root@iZ2vc2rht08hck1x6us154Z ~]# docker-compose -version
docker-compose version 1.18.0, build 8dd22a9
dockercompose.yml 编写
# 指定版本
version: "3"
# 服务的集合
services:
# 其中一个服务,服务名为:webapp
webapp:
# 指定该服务使用的镜像
image: examples/web
# 端口映射
ports:
- "80:80"
# 数据卷
volumes:
- "/data"
docker-compose 运行
# 后台启动 yaml 定义的所有容器
docker-compose up -d
# 仅启动 mysql 这个service,会启动其依赖的 service
docker-compose up mysql 指定启动的server名称,
# 停止容器并移除自动创建的网桥
docker-compose down
# 重启所有 service 后面可以指定上某个具体的 service
docker-compose restart
# 暂停 和 恢复
docker-compose pause
docker-compose unpause
# 进入 redis 这个 service 使用 exit 退出
docker-compose exec redis bash
# 列出当前 yaml 中定义的容器的信息
docker-compose ps
# 删除当前 yaml 中定义的容器,需要先 stop,后面可以指定上某个具体的 service
docker-compose rm
# 查看各个 service 容器内运行的进程情况
docker-compose top
# 查看日志默认查看 yaml 所有的,可以跟上具体 service
# -f 可以保持跟踪,新的日志会马上显示在屏幕上
docker-compose logs
Docker Compose 实践及梳理 - AaronLin - 博客园 (cnblogs.com)
参考链接
docker视频:https://www.bilibili.com/video/BV1ZT4y1K75K?p=19&vd_source=5e8f069711510b3788382a0a03ff38
https://www.cnblogs.com/edisonchou/p/dockerfile_inside_introduction.html
Docker — 从入门到实践 | Docker 从入门到实践 (docker-practice.com)
标签:ago,0.0,tcp,hours,常用命令,docker,root From: https://www.cnblogs.com/life1314/p/17936582.html