Docker and docker-compose are essential in daily work, installation is very simple, not to mention, just say the command and some common methods

docker

Common commands

View the running container
docker ps
Copy the code
View all containers

You can view closed containers

docker ps -a
Copy the code
View all local mirrors
docker images
Copy the code
Deleting a Local Mirror
Docker RMI image IDCopy the code
Delete the dangling mirror

A false image is an error that occurs when you build your own image. When docker images are viewed, both REPOSITORY and TAG are available. This image cannot be used either, but it still takes up space

docker image prune
Copy the code
Start the container

There are many arguments to start a container, the most commonly used are as follows

docker run -it --rm -p 80:80 -v ./log:/tmp/log xxx:xxx bash
Copy the code
  • -itStart interactively
  • --rmThe container is automatically deleted after exiting the containerdocker ps -aView closed containers againDocker RM Container ID
  • -p 80:80Map the host port to the container, 80 on the left is the host port and 80 on the right is the container port
  • -v ./log:/tmp/logMount the log directory in the current directory on the host to/TMP /log. The programs in the container will generate files in/TMP /log, which will also generate files on the host. These files will remain on the host even if the container is closed or removed
Into the container
Docker exec -it container name or ID bashCopy the code
Execute the program inside the container
Docker exec -it Container name or ID container commandCopy the code
Copy files from the container
Docker cp Container name or ID: host directory of the files in the containerCopy the code
View container metadata
Docker inspect Container name or IDCopy the code

You can view information such as the container’s entryPoint and exposed ports

Viewing container Logs
Docker logs container name or ID // tail Check the docker logs container name or ID --tail 1000 -fCopy the code
Package the image to a file
Docker save Image name or ID -o xxx.tar Docker save Image name or ID > xxx.tarCopy the code
Loads an image from a file
docker load -i xxx.tar
docker load < xxx.tar
Copy the code
TAG the image
Docker tag Image ID XXX: XXXCopy the code

Docker network related commands

Viewing an Existing Network
docker network ls
Copy the code
Create a network
Docker network create Network nameCopy the code
To connect to the Internet

Connects the specified container to the specified network

Docker Network Connect Network name Specifies the container name or IDCopy the code
Disconnect the network
Docker network Disconnect Network name Indicates the container name or IDCopy the code

Commonly used configuration

The configuration file of docker is /etc/dock/daemon. json, which is a JSON file, so the format must conform to JSON specifications

registry-mirrors

Set up the mirror source, this is an array, you can specify multiple, recommend https://docker.mirrors.ustc.edu.cn

insecure-registries

Set an insecure mirror source domain name. Set this if you generate an SSL certificate when you build harbor

data-root

If the docker root directory is set, docker downloaded images can be saved to a specified directory. If the system disk is not large, you can use this parameter to save the downloaded images to the data disk when mounting the data disk

The log – driver and log – opts

Docker will default to container stdout and stderr unified storage in the form of json to/var/lib/docker/containers / < container – id > / < > container – id – json. The log, After modifying the docker root directory through data-root, it will be saved to the corresponding new directory

Set the docker log saving mode and size limit as follows

{
  "log-driver": "json-file"."log-opts": {
    "max-size": "100m"."max-file": "5"}}Copy the code

docker-compose

Why docker-compose

  1. Docker compose can simplify the use of docker, no need to input a long list of commands every time (docker commands can be very long, such as mounting many directories, setting up many environment variables, setting up the network, etc.), convert docker commands into docker-comedy.yml configuration. When you need to start the container, go to the docker-comemess. yml directory and execute the start command

  2. Docker-comemess. yml file is used to arrange docker containers. In compose, each container is called a service. In docker-comemess. yml, multiple containers can be defined at one time, and dependencies between containers can be set. Start one container before starting the next

  3. Multiple containers defined in the same docker-comemage. yml share the same Docker network

Service Orchestration file

Docker-comemage. yml is compose’s choreographer file with the following common configuration parameters

  • Container_name Specifies the name of the container
  • Restart Restart policy. Always indicates that the container automatically starts after the restart
  • Image Specifies the image of the container. Do not set image if the image is built yourself
  • Build specifies that the container needs to build the image itself, or if the image has already been built, use the built image directly
  • Ports is an array that specifies the ports mapped from the host to the container. If network_mode is set to host, ports cannot be specified
  • Network_mode Specifies the network mode, which is actually the network namespace. The default value is bridge, which is required for connecting containers. When the value is set to host, the network namespace of the host is used
  • Environment Sets environment variables in the container
  • Volumes is an array that mounts the host’s directory into a container
  • Command is an array that overrides CMD and EntryPoint in the image and is executed when the container is started
  • Entrypoint is an. Sh script file that overrides entryPoint in the image and is executed when the container is started
  • User Indicates the user of the container, for example, root
  • Depends_on is an array that specifies other containers that the current container depends on. The current container will only be started if all other containers are started
  • Extra_hosts is an array that adds content, such as add, to the container’s /etc/hosts127.0.0.1 test
  • Ulimits specifies some kernel limits in containers, such as noFile Soft and hard
  • Hostname specifies the hostname of the container

The following uses the Docker of Zen Dao as an example to describe common configuration parameters:

version: '3.1'  Docker-compose version, which must be a string
services:  # Define service
  zentao:  # service name
    build: build  The first time you need to build an image, the directory to build the image is in the build directory of the current directory
    restart: always  Restart container automatically after restart
    container_name: zentao  # container name
    environment:  # specify environment variables in the container
      MYSQL_ROOT_PASSWORD:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
    ports:  Map host port to container
      - 180: 80
      - 13306: 3306
    volumes:  Mount the host directory to the container
      - ./app:/app/zentaopms
      - ./data:/var/lib/mysql
Copy the code

Here’s another example of a network bypass:

Start by creating a few Docker networks

docker network create mysql
docker network create redis
docker network create minio
Copy the code

Specify the network in docker-comemage.yml

version: '3.1'
services:
  api:
    build: build
    container_name: api
    ports:
      - 8088: 8088
    networks:
      - mysql
      - redis
      - minio

networks:
  mysql:
    external:
      name: mysql
  redis:
    external:
      name: redis
  minio:
    external:
      name: minio
Copy the code

The API container can be directly accessed by mysql, Redis and minio without specifying IP addresses in the program

datasource:
  druid:
    Mysql > select * from mysql
    url: jdbc:mysql://mysql:3306/xerfu? serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
Copy the code

Docker-comemage. yml is very easy to use after being proficient in writing docker-comemage. yml

Common commands

It must be executed in the docker-comemess. yml directory

Start the
docker-compose up
docker-compose up -d
Copy the code
Start a specified service
Docker-compose start service nameCopy the code
stop
docker-compose down
Copy the code
Stopping a specified service
Docker-compose stop service nameCopy the code
See the log
Docker-compose logs -f Docker-compose logs -f service nameCopy the code
Build the mirror

If the docker-compose. Yml service’s image is a build, the image will be automatically built when it is started for the first time. If the Dockerfile is modified, you need to manually run the docker-compose build command to build a new image

docker-compose build
docker-compose build --no-cache
Copy the code

–no-cache indicates that docker cache is not used. If apt-get update needs to be performed during the build process, do not enable the cache. Otherwise, some software may fail to be downloaded and installed

Docker command to docker-comemage.yml

Remember the docker command parameters corresponding to docker-comemage. yml parameters

Take the following docker command as an example:

docker run -it --rm -p 80:80 -v ./log:/tmp/log xxx:xxx bash
Copy the code

-p and -v need to be converted as follows:

version: '3.1'
services:
  xxx:
    image: xxx:xxx
    container_name: xxx
    restart: always
    ports:
      - 80: 80
    volumes:
      - ./log:/tmp/log
Copy the code

Docker-compose. Yml, accumulated later to build the service will be very cool, put the finished file into the server, execute docker-compose up -d