[Docker]Docker Common Commands Cheat Sheet

A recent project involved MongoDB clusters, Spark clusters, Hadoop clusters, Python 2.7, Python 3.5, various recommendation algorithms, Java Resin environment, LNMP environment, vsftpd, and more. Because it needed to be deployed across intranet, testing, and production environments, each deployment was painful. This is where Docker’s benefits really shone – packing Python environments, LNMP environments, vsftpd, and everything else into Docker containers was extremely satisfying. After 2 months of deployment and integration testing, I compiled some practical Docker commands. System environment: CentOS 7

  1. Download, install Docker, and start the service
    https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
    Download docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm

    1
    2
    
    yum install docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm 
    service docker start
    
  2. Common Docker commands
    Fix error: WARNING: IPv4 forwarding is disabled. Networking will not work.

    Step 1: On the host machine, run echo "net.ipv4.ip_forward=1" >>/usr/lib/sysctl.d/00-system.conf

    Step 2: Restart network and Docker services

    1
    
    [root@localhost /]# systemctl restart network && systemctl restart docker
    

    Failed to get D-Bus connection: Operation not permitted

    1
    
    docker run -d --name centos7 --privileged=true --net=host -v /home/code:/home/code centos:mysql_python3_jdk1.8 /usr/sbin/init
    

    Run a Docker container for a long time with host path mounting

    1
    
    docker run -dit -v /home/code:/home/code python35:latest /bin/bash
    

    Enter a running Docker container

    1
    
    docker exec -it container_id /bin/bash
    

    Start a container image

    1
    
    docker run -it python35:latest /bin/bash
    

    Docker export:

    1
    
    docker export -o /home/python35.tar container_id
    

    docker save *.tar

    Docker import:

    1
    
    docker import /home/python35.tar
    

    docker load < *.tar

    Docker tagging

    1
    
    docker tag image_id REPOSITORY:TAG
    

    Docker specify host network

    1
    
    docker run -dit --net=host python35:latest /bin/bash
    

    Docker sync container and host time

    1
    
    docker run -dit -v /etc/localtime:/etc/localtime python35:latest /bin/bash
    

    Install JDK 1.8

    1
    
    yum install java-1.8.0-openjdk* -y
    

    Install MySQL 5.6

    1
    2
    3
    4
    5
    6
    7
    8
    
    rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
    yum -y install mysql-community-server
    systemctl enable mysqld
    systemctl start mysqld
    Initialize:
    mysql_secure_installation
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'remote_login_password' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    

    Python 3 encoding

    1
    
    echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc
    
Licensed under CC BY-NC-SA 4.0