在Ubuntu上使用apt源安装docker
参考文档:https://docs.docker.com/install/linux/docker-ce/ubuntu/
配置源
$ sudo apt-get update Install packages to allow apt to use a repository over HTTPS: $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common Add Docker’s official GPG key: $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint. $ sudo apt-key fingerprint 0EBFCD88 pub rsa4096 2017-02-22 [SCEA] 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 uid [ unknown] Docker Release (CE deb) <docker@docker.com> sub rsa4096 2017-02-22 [S] Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels. Note: The lsb_release -cs sub-command below returns the name of your Ubuntu distribution, such as xenial. Sometimes, in a distribution like Linux Mint, you might need to change $(lsb_release -cs) to your parent Ubuntu distribution. For example, if you are using Linux Mint Tessa, you could use bionic. Docker does not offer any guarantees on untested and unsupported Ubuntu distributions. x86_64 / amd64 armhf arm64 ppc64le (IBM Power) s390x (IBM Z) $ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" INSTALL DOCKER ENGINE - COMMUNITY Update the apt package index.
安装docker
$ sudo apt-get update Install the latest version of Docker Engine - Community and containerd, or go to the next step to install a specific version: $ sudo apt-get install docker-ce docker-ce-cli containerd.io
安装后
Create the docker group. $ sudo groupadd docker Add your user to the docker group. $ sudo usermod -aG docker $USER $ newgrp docker Verify that you can run docker commands without sudo. scutech@scutech:~$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
检查服务是否激活(自动启动),如果没有激活就enable。
# systemctl is-enabled docker enabled
docker安装mysql
下载pull myql
root@scutech:~# docker pull mysql Using default tag: latest latest: Pulling from library/mysql Digest: sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2c Status: Image is up to date for mysql:latest docker.io/library/mysql:latest root@scutech:~# root@scutech:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 9b51d9275906 44 hours ago 547MB ubuntu latest 72300a873c2c 13 days ago 64.2MB hello-world latest fce289e99eb9 14 months ago 1.84kB
运行mysql
docker run -i -t -e MYSQL_ROOT_PASSWORD=dingjia --name=mysql1 mysql
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cee4988f659a mysql "docker-entrypoint.s…" 13 hours ago Up 13 hours 3306/tcp, 33060/tcp friendly_aryabhata
连接进入docker中的mysql的两种方式
docker exec -it mysql1 mysql -uroot -p
docker exec -it mysql bash
挂载主机的目录/infokist/mysql8到mysql的数据目录,将主机的33306映射到docker的3306端口:
docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql8:/var/lib/mysql -p 33306:3306 --name=mysql8 mysql
创建用户
create USER 'scutech'@'%' IDENTIFIED WITH mysql_native_password by 'dingjia'; grant all privileges on *.* to 'scutech'@'%';
客户端连接
root@infokist:/infokist# mysql -uroot -pdingjia -P 33306 --protocol=tcp mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show processlist; +----+-----------------+------------------+------+---------+------+------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-----------------+------------------+------+---------+------+------------------------+------------------+ | 4 | event_scheduler | localhost | NULL | Daemon | 1297 | Waiting on empty queue | NULL | | 13 | root | 172.17.0.1:60128 | NULL | Query | 0 | starting | show processlist | +----+-----------------+------------------+------+---------+------+------------------------+------------------+ 2 rows in set (0.00 sec) mysql>
在运行一个5.7的mysql数据库
root@infokist:/infokist# docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql57:/var/lib/mysql -p 33357:3306 --name=mysql57 mysql:5.7 193c9d6af33b54970021e0d5b5efe7eecd5c9c04f3cb1ae7271b8a2cc98bb71c root@infokist:/infokist# root@infokist:/infokist# mysql mysql -uroot -pdingjia -P 33357 --protocol=tcp^C root@infokist:/infokist# mysql -uroot -pdingjia -P 33357 --protocol=tcp mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.29 MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show processlist -> ; +----+------+------------------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+------------------+------+---------+------+----------+------------------+ | 2 | root | 172.17.0.1:44616 | NULL | Query | 0 | starting | show processlist | +----+------+------------------+------+---------+------+----------+------------------+ 1 row in set (0.00 sec) mysql>
创建网络
创建一个mysql-replicatio的网络
scutech@infokist:~$ docker network create -d bridge mysql-replication a9c563ffbe7306758700aaebeb4a7816599ddba8fce1543b1a644fe9fed2b40a scutech@infokist:~$ docker network ls NETWORK ID NAME DRIVER SCOPE 1075ff680429 bridge bridge local caf15dbbb6aa host host local a9c563ffbe73 mysql-replication bridge local 9f9ca1e119a5 none null local
分布运行三个版本的mysql,测试之间的互联
docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql56:/var/lib/mysql -p 33356:3306 --network mysql-replication --name=mysql56 mysql:5.6 docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql57:/var/lib/mysql -p 33357:3306 --network mysql-replication --name=mysql57 mysql:5.7 docker run -itd -e MYSQL_ROOT_PASSWORD=dingjia -v /infokist/mysql8:/var/lib/mysql -p 33380:3306 --network mysql-replication --name=mysql80 mysql docker exec -it mysql57 bash root@18c98aab21f4:/# mysql -uroot -pdingjia --protocol=tcp -hmysql80 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit
ping试一下
root@da2ac5ce6f38:/# ping mysql57 PING mysql57 (172.18.0.3) 56(84) bytes of data. 64 bytes from mysql57.mysql-replication (172.18.0.3): icmp_seq=1 ttl=64 time=0.179 ms 64 bytes from mysql57.mysql-replication (172.18.0.3): icmp_seq=2 ttl=64 time=0.047 ms ^C --- mysql57 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1020ms rtt min/avg/max/mdev = 0.047/0.113/0.179/0.066 ms root@da2ac5ce6f38:/# ping mysql80 PING mysql80 (172.18.0.4) 56(84) bytes of data. 64 bytes from mysql80.mysql-replication (172.18.0.4): icmp_seq=1 ttl=64 time=0.110 ms 64 bytes from mysql80.mysql-replication (172.18.0.4): icmp_seq=2 ttl=64 time=0.050 ms ^C --- mysql80 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1029ms rtt min/avg/max/mdev = 0.050/0.080/0.110/0.030 ms root@da2ac5ce6f38:/# ping mysql56 PING mysql56 (172.18.0.2) 56(84) bytes of data. 64 bytes from da2ac5ce6f38 (172.18.0.2): icmp_seq=1 ttl=64 time=0.037 ms 64 bytes from da2ac5ce6f38 (172.18.0.2): icmp_seq=2 ttl=64 time=0.028 ms ^C --- mysql56 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1021ms rtt min/avg/max/mdev = 0.028/0.032/0.037/0.007 ms root@da2ac5ce6f38:/#