Summary of common commands for Docker and Dockerfile and packaging of Docker images for microservices

1. Summary of common commands of Docker

insert image description here

1. Help command

# 查看docker的所有命令详情
docker --help

# 查看docker对应命令的使用详情
docker 命令 --help

# 如:查看docker images的使用方法
docker images --help

[root@fussy ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs

2. Mirroring commands

# 从DockerHub上查询nginx镜像
docker search nginx
[root@fussy ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        16041     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2104                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   820                  [OK]

# 拉取nginx镜像
docker pull nginx

# 拉取nginx指定版本镜像
docker pull nginx:1.9

# 查看所有镜像
docker images -a
[root@fussy nginx]# docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       latest    5d0da3dc9764   3 months ago     231MB

# 修改镜像的版本信息(原镜像不变,生成一个修改版本信息后的副本镜像)
docker tag 源镜像:TAG 目标镜像:TAG
docker tag centos:latest my/centos:1.0
[root@fussy nginx]# docker tag centos:latest my/centos:1.0
[root@fussy nginx]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       latest    5d0da3dc9764   3 months ago     231MB
my/centos    1.0       5d0da3dc9764   3 months ago     231MB

# 查看所有镜像的ID
docker images -aq
[root@fussy nginx]# docker images -aq
92b28ed32bef
5d0da3dc9764

# 删除指定的镜像
docker rmi -f 镜像

# 删除所有的镜像
docker rmi -f $(docker images -aq)

# 查询指定镜像详情
docker inspect 镜像
docker inspect centos

# 构建镜像
docker build -f 文件所在路径 -t 生成的镜像名称:TAG .
docker build -f Dockerfile -t myimages:1.0 .

3. Container commands

# 启动容器
docker run 镜像
docker run centos

# 启动容器并进入命令行
docker run -it 镜像

# 启动容器并后台运行
docker run -d 镜像

# 启动容器并指定自定义的网络
docker run --net mynet 镜像

# 启动容器并指定容器名称,同时进入容器的命令行
docker run -it --name centos01 --net mynet centos /bin/bash

# 启动容器并指定端口映射(可指定多个)
docker run -p 主机端口:容器端口 镜像
docker run -p 3500:80 -p 3600:8080 nginx

# 启动容器并随机映射端口
docker run -P 镜像

# 启动容器并指定具名数据卷(可指定多个)
docker run -v 主机路径:容器路径 镜像
docker run -v /usr/local/nginx/conf:/etc/nginx -v /usr/local/mysql/data:/etc/mysql/data 镜像

# 查看容器详情
docker inspect 容器名称或容器ID

# 进入指定的容器
# 第一种方式(重新进入命令行)
docker exec -it 容器名称或容器ID /bin/bash
docker exec -it 44a231940d74 /bin/bash
[root@fussy nginx]# docker exec -it 44a231940d74 /bin/bash
[root@44a231940d74 /]# 

# 第二种方式(进入上一次的命令行)
docker attach 容器名称或容器ID
docker attach 44a231940d74
[root@fussy nginx]# docker attach 44a231940d74
[root@44a231940d74 /]# 

# 退出并停止容器
exit

# 退出但不停止容器
ctrl + q + p

# 查看所有运行的容器
docker ps

# 查看所有容器(包括停止的)
docker ps -a

# 查看所有容器的ID
docker ps -aq

# 删除所有容器
docker rm -f $(docker ps -aq)

# 停止、启动容器
docker stop/start 容器名称或容器ID

# 将容器提交到仓库中,变成一个镜像
docker commit -m  提交信息  -a  作者  容器名称或容器ID  镜像名称:TAG
docker commit -m '第一次提交' -a 'liulusheng' ccff597ebb23 myimages:1.0

4. Network commands

# 查看docker的所有网络
docker network ls

# 查看对应网络的详细信息
docker network inspect 网络名称或网络ID
docker network inspect mynet
[root@fussy ~]# docker network inspect mynet
[
    {
    
    
        "Name": "mynet",
        "Id": "447f6cf844a45e15bc746a7c97736ed61937b19b29f40b3b9d8bb364de26d949",
        "Created": "2021-12-30T13:48:14.4242933+08:00",
        "Scope": "local",
        "Driver": "bridge",
]


# 创建自定义网络
docker network create -d 网络模式(默认是bridge桥接模式) --subnet 子网掩码 --gateway 网关 网络名称
docker network create -d bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet

# 删除指定的网络
docker network rm 网络名称
docker network rm mynet

5. Other commands

# 查看容器ip详情
ip addr
[root@44a231940d74 /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0
       valid_lft forever preferred_lft forever
       
# 登陆DockerHub仓库
docker login
[root@fussy nginx]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 18879676724
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

# 退出DockerHub仓库
docker logout

# 提交镜像到远程仓库
# 目标镜像名称一定要是`DockerHub用户名/镜像名称`的形式才能发布到DockerHub,以保证镜像名称的唯一性。
# 如果目标镜像名称不符合要求,则可以使用如下命令修改。`docker tag 源镜像:TAG 目标镜像:TAG`
docker push 目标镜像:TAG
docker push 18879676724/centos:1.0

[root@fussy nginx]# docker push 18879676724/centos:1.0
The push refers to repository [docker.io/18879676724/centos]
74ddd0ec08fa: Pushing [===>                                               ]  17.45MB/231.3MB
74ddd0ec08fa: Pushed 
1.0: digest: sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc size: 529

2. Summary of common commands in Dockerfile

The script commands in the Dockerfile are all capitalized. After the Dockerfile is written, an executable image can be built through the docker build -t xxx . command.

Order:docker build -f 文件所在路径 -t 镜像名称:TAG .

If the name of the Dockerfile script file to be written is Dockerfile, the -f parameter can be omitted

1. FROM

Customized images are based on FROM, and nginx here is the basic image required for customization

FROM nginx

2. RUN

RUN : the shell command executed when docker build builds the image

FROM nginx
# 以下执行会创建 3 层镜像,需要注意的是:每RUN一次,都会在 docker 上新建一层镜像.
# 过多无意义的层,会造成镜像膨胀过大。
RUN yum -y install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN tar -xvf redis.tar.gz
FROM nginx
# 可以通过&&符号连接命令,这样只会生成一层镜像
RUN yum -y install wget \
    && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
    && tar -xvf redis.tar.gz

3. MAINTAINER

Declare the author and contact information of the image

FROM nginx
# 作者和联系方式
MAINTAINER Author [email protected]

4. COPY

Copy instructions, copy files or directories from the context directory to the specified path in the container

COPY <源路径1>... <目标路径>
# <源路径>:源文件或者源目录,这里可以是通配符表达式,其通配符规则要满足 Go 的 filepath.Match 规则
# <目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。
COPY hom*.jar /mydir/app.jar

5. ADD

The ADD instruction is similar to COPY in terms of format and function. The difference is that if the source file is a compressed file, the source file will be decompressed and decompressed into the directory path.

ADD jdk1.8.tar.gz /usr/local/jdk

6. ENV

Set the environment variable, define the environment variable, then in the subsequent instructions, you can use this environment variable.

ENV <key> <value>
ENV <key1>=<value1> <key2>=<value2>...
# 示例
ENV NODE_VERSION 7.2.0
RUN echo $NODE_VERSION

7. EXPOSE

Just declare the port. Help mirror users understand the guard port of this mirror service, so as to facilitate configuration mapping. When using random port mapping at runtime, that is, when docker run -P, the port of EXPOSE will be automatically mapped randomly.

EXPOSE <端口1> [<端口2>...]

8. WORKDIR

The working path, which is the path entered by default after the container starts

WORKDIR /usr/local

9. LABEL

The LABEL command is used to add some metadata to the image, in the form of key-value pairs. The syntax is as follows:

LABEL <key>=<value> <key>=<value> <key>=<value> ...
# 示例,添加作者
LABEL authors="runoob"

10. VOLUME

Define anonymous data volumes. If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume to avoid loss of important data due to container restart.

VOLUME ["<路径1>", "<路径2>"...]
# 将容器内的 /etc/nginx 和 /etc/mysql/data 目录中的内容挂载到主机上
# 在启动容器 docker run 的时候,我们也可以通过 -v 参数修改挂载点
VOLUME ["/etc/nginx", "/etc/mysql/data"]

11. CMD

Similar to the RUN command, it is used to execute the shell command program, but the timing of the two runs is different: CMD is run when docker runthe container started, and RUN is docker buildrun when the image is built.

The program specified by the CMD command can be overridden by the program to be run specified in the docker run command line parameters

If there are multiple CMD instructions in the Dockerfile, only the last one will take effect.

CMD <shell 命令> 
CMD ["<param1>","<param2>",...]  # 该写法是为 ENTRYPOINT 指令指定的程序提供默认参数

12. ENTRYPOINT

ENTRYPOINTThe instruction is basically equivalent to CMDthe instruction , which is also docker runexecuted when the container is started, but it will not docker runbe overwritten by the instruction specified by the command line parameters of the , but will pass these command line parameters to the program specified by the ENTRYPOINT instruction for execution.

If there are multiple ENTRYPOINT directives in the Dockerfile, only the last one will take effect.

ENTRYPOINT ["<executeable>","<param1>","<param2>",...]
  • It can be used with the CMD command: CMD is usually used only when the parameter is changed, and the CMD here is equivalent to passing parameters to ENTRYPOINT
FROM nginx
ENTRYPOINT ["nginx", "-c"] # 定参
CMD ["/etc/nginx/nginx.conf"] # 变参 

# 1. 不传参运行
docker run nginx:test

# 容器内会默认运行以下命令,启动主进程
nginx -c /etc/nginx/nginx.conf


# 2. 传参运行
docker run nginx:test -c /etc/nginx/new.conf

# 容器内会默认运行以下命令,启动主进程
nginx -c /etc/nginx/new.conf

3. SpringBoot microservice packaging Docker image

1. Write Dockerfile

Create a new Dockerfilefile and write script commands.

# 基于jdk1.8
FROM java:8

# 声明作者
MAINTAINER author [email protected]

# 将jar包拷贝至运行容器的根目录下
COPY *.jar /app.jar

# 容器运行时执行的命令
ENTRYPOINT java -jar /app.jar > app.log

# 容器运行时,传递给ENTRYPOINT的参数
CMD ["--server.port=8080"]

# 最终会执行 java -jar /app.jar > app.log --server.port=8080

2. Build the image

JarPlace the package Dockerfilein the same folder as the file

# 根据Dockerfile文件构建镜像
docker build -t app:1.0 .

3. Start the container

# 通过app镜像启动容器,并映射端口8181->8080,随后可以通过`主机IP:8181`访问该项目
docker run -d -p 8181:8080 app:1.0

Guess you like

Origin blog.csdn.net/L28298129/article/details/122236704