Docker Practical Command Manual

Docker Practical Command Manual

Hello everyone, I am Bittao. This article summarizes the super practical Docker command manual. This article is suitable for students who have a certain Docker foundation. If you are not familiar with Docker, you may not be able to use these commands directly. But don't worry, Docker itself is a tool, and it doesn't take much time if you just use it. You can enter here to learn: Docker from entry to practice .

1. Installation

The use of Docker commands does not distinguish between operating systems. The only thing to note is that Windows does not have sudocommands. The other is to select a path that the host can recognize on the volume mapping. In terms of installation, among the mainstream operating systems Linux, Mac OS, and Windows, Linux has the best performance. Because Docker can directly reuse the Linux kernel of the host machine, you can install Docker Engine directly from the official website. Windows and Mac can only install Docker Desktop, which is visual. Windows can be configured to use its own WSL kernel internally. It is recommended to configure it to improve efficiency. Otherwise, Docker will run a virtual Linux system locally to support Docker. Dockers Compose comes with Mac and Windows, but it needs to be downloaded and configured separately in Linux.

2. Command form

There are three forms of Docker commands that we usually use, and these three forms can be converted to each other:

  1. Command line
    This is the same as typing other commands, all need to be typed manually, as shown in the figure below:
    insert image description hereSome command lines may be very long, we can use \separators to divide them, so that the visual can be seen more clearly. insert image description here
    This form can be typed directly, and then press Enter to execute.

  2. Dockerfile
    requires a handwritten Dockerfile file in the following format:

    FROM anapsix/alpine-java
    ARG APP_NAME
    ENV APP_NAME=${APP_NAME}
    ADD ./flow-eda-${APP_NAME}-0.0.1-SNAPSHOT.jar ./flow-eda-${APP_NAME}.jar
    ARG APP_PORT
    EXPOSE ${APP_PORT}
    ENTRYPOINT java -jar flow-eda-${APP_NAME}.jar
    

    Execute docker build -t ***:1.0 .to compile the file into the image of the machine, and then run it.

  3. DockerCompose
    Compose is a tool for defining and running multi-container Docker applications. It is more useful for writing the container arrangement required for an application. Usually docker-compose.ymlnamed after, the format is as follows:

    version: '3'
    	services:
    	  elasticsearch:
    	    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2-amd64
    	    container_name: "elasticsearch"
    	    ports:
    	      - 9200:9200
    	      - 9300:9300
    	    environment:
    	      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    	      - cluster.name=tommy-es
    	      - bootstrap.memory_lock=true
    	      - discovery.type=single-node
    	    volumes:
    	      - D:/note-data/es-data:/usr/share/elasticsearch/data
    	
    	  kibana:
    	    image: docker.elastic.co/kibana/kibana:7.9.2
    	    container_name: kibana
    	    depends_on:
    	      - elasticsearch
    	    ports:
    	      - 5601:5601
    

    Commonly used in Docker Compose docker-compose up -dto start in the background, docker-compose downclose the container and delete the image. It should be noted that the image in DockerCompose will only be built when it is used for the first time. If you want to update incrementally for subsequent modifications, you need to use the following command:

    docker-compose build
    docker-compose up --build
    

3. Commonly used command lines

3.1、MySQL

# Mysql 8
docker run -d --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e YSQL_ROOT_HOST:=% mysql
# Mysql 5.7
docker run -p 3307:3306 --name mysql -v /opt/docker/mysql/log:/var/log/mysql -v /opt/docker/mysql/data:/var/lib/mysql -v /opt/docker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

If you want to map the configuration file, you need to manually create the Mysql configuration file in advance, /opt/docker/mysql/confthe configuration file my.cnfis as follows:
Mysql 8

[mysqld]
#datadir=/usr/local/mysql/data
default_authentication_plugin=mysql_native_password  #使用mysql8以前的密码插件,以便navicat等工具能够正常连接
default-storage-engine=INNODB
character_set_server = utf8
secure_file_priv=/var/lib/mysql
[mysqld_safe]
character_set_server = utf8
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[client]
default-character-set = utf8

Mysql 5.7

[mysqld]
character-set-server=utf8
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

Mapping directly with the configuration file of the host machine may cause security issues and may not take effect. If prompted, enter the container and execute the following command:

chmod 644 /etc/my.cnf
# 设置编码格式
show variables like 'character_set_%';
set character_set_server=utf8;

3.2、Redis

# 简洁版
docker run -d --name redis -p 6379:6379 redis:latest redis-server --appendonly yes
# 细化配置
docker run -d --privileged=true --restart always --name redis -p 6379:6379 -v /opt/docker/redis/data:/data redis --requirepass 123456 --appendonly yes

3.3、Rabbitmq

docker run -d --privileged=true --restart=always --name rabbitmq -p 5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=123456 -v /d/Docker/Rabbitmq/data:/var/lib/rabbitmq  -v /d/Docker/Rabbitmq/log/:/var/log/rabbitmq/log/ rabbitmq:management

3.4、Nacos

docker run -d --privileged=true --restart=always  --name nacos -p 8848:8848 -p 9848:9848 -p 9849:9849 --env MODE=standalone nacos/nacos-server
docker run -d --privileged=true  --name nacos -p 8848:8848 -p 9848:9848 -p 9849:9849 --env MODE=standalone  -v D:\docker\nacos\logs:/home/nacos/logs -v D:\docker\nacos\conf\application.properties:/home/nacos/conf/application.properties nacose:v1

3.5、Nginx

# 生成容器
docker run --name nginx -p 9001:80 -d nginx
# 将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /usr/local/docker/nginx/conf/nginx.conf
# 将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /usr/local/docker/nginx/conf/conf.d
# 删除掉这个用来拿配置文件的容器
docker rm nginx
# 重新开启一个
docker run -d --privileged=true --restart=always -p 9001:80 --name nginx -v /root/web/html:/usr/share/nginx/html -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/conf/conf.d:/etc/nginx/conf.d -v /usr/local/docker/nginx/log:/var/log/nginx nginx

3.6、Minimum

docker run  -p 9090:9090  -p 9091:9091 --name minio \
 -d --restart=always \
 -e "MINIO_ROOT_USER=admin" \
 -e "[email protected]" \
 -v /data/oss/jun/data:/data \
 -v /data/oss/jun/config:/root/.minio \
  minio/minio server /data  --console-address ":9090"  --address ":9091"

3.7、Gitlab

sudo docker run --detach --publish 8930:443 --publish 8929:8929 --publish 8928:22 --name gitlab --restart always --volume $GITLAB_HOME/config:/etc/gitlab --volume $GITLAB_HOME/logs:/var/log/gitlab --volume $GITLAB_HOME/data:/var/opt/gitlab --shm-size 256m registry.gitlab.cn/omnibus/gitlab-jh:latest

3.8、Nexus

docker run -d --name nexus3 --restart=always -p 8081:8081 --mount src=nexus-data,target=/nexus-data sonatype/nexus3

3.9、Jenkins

docker run -d -u root -p 8888:8080 -v /opt/docker/jenkins-data:/var/jenkins_home -v /opt/docker.sock:/var/run/docker.sock -v "$HOME":/home --privileged=true --restart=always --name jendemo jenkinsci/blueocean

3.10、Mariadb

docker run -d -p 3306:3309 --name mariadb -v /opt/mariadb/data/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mariadb:latest
mysql -h 127.0.0.1:3309 -u root -p root

3.11、xxl-job

docker run \ 
-e PARAMS="--spring.datasource.url=jdbc:mysql://172.17.0.3:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=123456" \
-p 28080:8080 \
--name xxl-job-admin \
-d xuxueli/xxl-job-admin:2.3.1

4. Commonly used DockerFile

4.1、Mysql

FROM mysql/mysql-server:8.0.30

ENV TZ=Asia/Shanghai

RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

COPY ./glmx.sql /docker-entrypoint-initdb.d

4.2. Java application

# 环境
FROM glmx-base

RUN mkdir -p /root/glmx

WORKDIR /root/glmx
# 拷贝jar
COPY glmx /root/glmx
# 设置暴露的端口号
EXPOSE 24081

ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms512m -Xmx1024m -Djava.ext.dirs=lib"

# 执行命令
CMD java -Xms512m -Xmx1024m -Djava.ext.dirs=lib -jar gfpt-gfpt-1.0-SNAPSHOT.jar

4.3, compile and start

docker build -t  demo-app:1.0  .

sudo docker run -d -p 24081:24081 --name demo-app \
--privileged=true --restart always  \
-v /home/administrator/demo/app:/root/demo\
demo-app

5. Commonly used Docker Compose

5.1. Spring Boot project

version: "3.8"

services:
    mysql:
        build:
            context: ./db
        container_name: glmx-mysql
        ports: 
            - "6001:3306"
        environment:
            MYSQL_ROOT_HOST: "%"
            MYSQL_ROOT_PASSWORD: 123456
        networks:
            - glnet
        restart: always
        privileged: true

    redis:
        image: glmx-redis
        container_name: glmx-redis
        ports:
            - "6002:6379"
        command: ["redis-server","--requirepass 123456","--appendonly yes"]
        networks:
            - glnet
        restart: always
        privileged: true

    rabbitmq:
        image: glmx-rabbitmq
        container_name: glmx-rabbitmq
        ports:
            - 6003:5672
            - 6004:15672
        networks:
            - glnet
        restart: always
        privileged: true
    

    nacos:
        image: glmx-nacos
        container_name: nacos-nacos
        environment: 
            - MODE=standalone
        ports: 
            - "6005:8848"
        networks:
            - glnet
        restart: always
        privileged: true

    nginx:
        image: glmx-nginx
        container_name: glmx-nginx
        ports:
            - "80:80"
            - "8080:8080"
            - "8081:8081"
            - "6868:6868"
        volumes:
            - "D:/Docker/glmx/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf"
            - "D:/Docker/glmx/nginx/www:/usr/share/nginx/html"
            - "D:/Docker/glmx/nginx/log:/var/log/nginx"
        networks:
            - glnet
        restart: always
        privileged: true

    app:
        image: glmx-app
        container_name: glmx-app
        ports:
            - "24081:24081"
        volumes:
            - "D:/Docker/glmx/app:/root/glmx"
        networks:
            - glnet
        links:
            - mysql
            - redis
            - rabbitmq
            - nacos
        depends_on:
            - mysql
            - redis
            - rabbitmq
            - nacos
            - nginx
        restart: always
        privileged: true

networks:
    glnet:
        driver: bridge

6. Wonderful skills

In case of no permission:

# 没有权限
sudo groupadd docker
sudo gpasswd -a $USER docker
newgrp docker

view log

# 查看***容器的日志
docker logs --tail=1000 ***
# 实时查看更新
docker logs -f ***
# 日志保存位置
/var/lib/docker/containers/容器ID/容器ID-json.log
# 避免Ctrl + c 退出此容器
docker attach --sign-proxy=false ***

backup export import

# 重命名Docker容器的tag名称,***代表现有名字,###代表新名字
docker tag *** ### 
# 保存***容器到###.tar中,***容器后面可以跟多个容器
docker save -o ###.tar ***1 ***2
# 恢复容器
docker load --input ***.tar  或者  docker load < ***.tar

Install Docker Compose

【DockerCompose】
mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
  • -net=host

Only available on Linux, not available on Mac and Windows due to issues with Docker's networking implementation.

  • host.docker.internal

This command can represent the host, but if hosts domain name mapping is used in DockerCompose

extra_hosts:
            - "host.docker.internal:host-gateway"

View Docker related configuration

docker inspect {
    
    CONTAINER ID}
docker netowrk inspect {
    
    NETWORK ID}

7. Conclusion

Docker is actually just a tool, just like a Vmware virtual machine, although their principles are different. But from the point of view of use, there is not much difference, nothing more than Docker is basically in the command line form. Due to the fast and ready-to-go nature of Docker, once you use it, you can't go back.

Guess you like

Origin blog.csdn.net/u012558210/article/details/129124044