Docker

0 概述

 

 

1:安装Docker?

版本1 

版本2

Step1:安装 Docker

sudo apt-get install -y docker.io

Step2:启动 Docker

systemctl start docker

Step3:运行系统引导时启用 docker

systemctl enable docker

Step4:核对一下 docker 版本:

docker version

2:Docker常用命令?有时需要加sudo

1.查看所有已下载的images

docker images

2. 从Docker Hub中拉取或者更新指定镜像

docker pull 名称:版本号

3.运行容器

docker run

-d 标识是让 docker 容器在后台运行。

-p 标识通知 docker 将容器内部使用的网络端口映射到我们使用的主机上。

--name 定义一个容器的名字,如果在执行docker run时没有指定Name,那么docker会自动生成一个随机数字符串当做UUID。

-e 设置环境变量,或者覆盖已存在的环境变量。

例如:

docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -dmysql/mysql-server:latest

含义:容器的名字为mysql,将容器的3306端口映射到本机的3306端口,mysql数据库的密码为password ,运行的镜像为mysql/mysql-server:latest

4.查看容器

docker ps 

-a 查看已经创建的容器

-s 查看已经启动的容器

5.启动容器

docker start con_name //启动容器名为con_name的容器。

6.停止容器

docker stop con_name //停止容器名为con_name的容器

7.删除容器

docker rm con_name //删除容器名为con_name的容器

8.删除镜像

docker rmi img_name //删除镜像名为img_name的镜像

9.重命名容器

docker rename old_name new_name //重命名一个容器

3:Docker下安装mysql?

//Step1:拉取所需版本的mysql

sudo docker pull mysql:5.7.12 //5.7.12是版本号

//Step2:运行mysql(没有映射配置文件和数据文件,如需要,则此时停下来看下面如何修改mysql默认配置)

sudo docker run --name lab-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.12

密码是:123456

4:如何修改mysql的默认配置?

//Step1在~目录下新建如下文件夹并上传配置文件

>mkdir mysql >cd mysql && mkdir config >rz my.cnf

//Step2:关闭和销毁当前容器

sudo docker stop 容器id sudo docker rm 容器id

//Step2:将本地配置文件和mysql Docker容器内部的配置文件的数据文件夹映射(这么做的好处是可以在外部文件修改配置文件)

sudo docker run -d -p 3306:3306 -v ~/mysql/config/my.cnf:/etc/mysql/my.cnf -v ~/mysql/data:/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=123456 --name lab-mysql mysql:5.7.12

//解释

 -d:是后台运行

-p:

3306:3306 本机的3306端口映射到容器的3306端口

-v:

 ~/mysql/config/my.cnf:/etc/mysql/my.cnf:本地配置文件和容器配置文件的映射

/mysql/data:/var/lib/mysql 本地数据文件和容器数据文件的映射

-e

MYSQL_ROOT_PASSWORD=123456:数据库的密码是123456

--name lab-mysql:给该容器取名为lab-mysql

mysql:5.7.12:运行的镜像名称

//到此为止数据库已经跑起来了

5:如何在容器运行mysql命令行?

>sudo docker ps -a //查看当前运行的容器有哪些?

>sudo docker exec -it 2549 /bin/bash //2549是容器id,之后会进入到容器内部

>mysql -u root -p //输入密码进入root用户

>show variables like '%sql_mode' //查看当前的模式

6 mysql linux下表名区分大小写的问题?

配置文件中加入如下(如何修改配置文件见上)

lower_case_table_names=1

7 Docker配置redis运行

docker run -d --privileged=true -p 6379:6379 -v

~/redis/config/redis.conf:/etc/redis/redis.conf -v ~/redis/data:/data --name webapp-redis redis:latest redis-server /etc/redis/redis.conf --appendonly yes

8 Docker镜像备份?

//Step1 查看需要备份的容器

sudo docker ps

//Step2 备份

sudo docker save  -o ./mysql-backup.tar mysql-backup

//Step3 查看镜像是否备份成功

sudo docker images

//Step4 恢复镜像

sudo docker load -i ./mysql-backup.tar

//Step5 运行容器 mysql-backup就是之前备份镜像的名称

sudo docker run -d -p 3306:3306 -v ~/mysql/config/my.cnf:/etc/mysql/my.cnf -v ~/mysql/data:/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=123456 --name lab-mysql mysql-backup

附录一:
//# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port  = 3306
socket  = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket  = /var/run/mysqld/mysqld.sock
nice  = 0

[mysqld]
skip-host-cache
skip-name-resolve
user  = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket  = /var/run/mysqld/mysqld.sock
port  = 3306
basedir  = /usr
datadir  = /var/lib/mysql
tmpdir  = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

lower_case_table_names=1
sql_mode = NO_AUTO_CREATE_USER,NO_ENGIN_SUBSTITUTION

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1

#log-error = /var/log/mysql/error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

docker run -d -p 9000:80 -p 3306:3306 \
        -e USER="root" -e PASSWD="752799184" \
        -e BIND_ADDRESS="false" \
        -e SMTP_HOST="163.177.90.125 smtp.exmail.qq.com" \
        -v ~/zentao/:/opt/zbox/ \
        --name zentao-server \
        idoop/zentao:latest

9:解决Docker每次都要使用sudo的问题?

//注意:重新登录该用户才能生效

sudo usermod -aG docker 当前用户名