1、什么是docker
ocker 是一个开源的应用容器引擎,基于 Go语言 并遵从Apache2.0协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。
2.安装
1)docker只能在linux上运行,首先在VMware上安装一个Centos7.6的虚拟机,链接:https://mirrors.tuna.tsinghua.edu.cn/centos/7.6.1810/isos/x86_64/
2)配置NAT网络
3)安装docker
yum -y install docker
4)开机启动
systemctl enable docker
5)启动docker
systemctl start docker
6)查看版本
[root@localhost yum.repos.d]# docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 Go version: go1.10.3 Git commit: b2f74b2/1.13.1 Built: Wed May 1 14:55:20 2019 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 Go version: go1.10.3 Git commit: b2f74b2/1.13.1 Built: Wed May 1 14:55:20 2019 OS/Arch: linux/amd64 Experimental: false
3.docker常用操作
1)查找 docke serach
[root@localhost yum.repos.d]# docker search mysql INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/mysql MySQL is a widely used, open-source relati... 8388 [OK] docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 2884 [OK] docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 624 [OK] docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 438 [OK] docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 60 [OK] docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 59 docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 47 docker.io docker.io/deitch/mysql-backup Automated and scheduled mysql database dum... 39 [OK] docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 32 docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 28 [OK] docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 27 [OK] docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 21 docker.io docker.io/prom/mysqld-exporter 19 [OK] docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 15 docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 13 docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 12 docker.io docker.io/arey/mysql-client Run a MySQL client from a docker container 10 [OK] docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 docker.io docker.io/yloeffler/mysql-backup This image runs mysqldump to backup data u... 6 [OK] docker.io docker.io/fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron t... 4 [OK] docker.io docker.io/genschsa/mysql-employees MySQL Employee Sample Database 2 [OK]
2)拉取
[root@localhost yum.repos.d]# docker pull mysql Using default tag: latest Trying to pull repository docker.io/library/mysql ... latest: Pulling from docker.io/library/mysql fc7181108d40: Pull complete 787a24c80112: Pull complete a08cb039d3cd: Pull complete 4f7d35eb5394: Pull complete 5aa21f895d95: Pull complete a742e211b7a2: Waiting 0163805ad937: Download complete 87f18876c3ff: Waiting 78082f25f167: Waiting 0a510f055c17: Waiting 312b0999e433: Waiting f864cfdc0264: Waiting
注意:这一步特别慢,那么就 ctrl+C 然后使用加速器
阿里云容器镜像服务
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://2x3h3a6l.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
果然很快
[root@localhost docker]# docker pull mysql Using default tag: latest Trying to pull repository docker.io/library/mysql ... latest: Pulling from docker.io/library/mysql fc7181108d40: Pull complete 787a24c80112: Pull complete a08cb039d3cd: Pull complete 4f7d35eb5394: Pull complete 5aa21f895d95: Pull complete a742e211b7a2: Pull complete 0163805ad937: Pull complete 87f18876c3ff: Pull complete 78082f25f167: Pull complete 0a510f055c17: Pull complete 312b0999e433: Pull complete f864cfdc0264: Pull complete Digest: sha256:415ac63da0ae6725d5aefc9669a1c02f39a00c574fdbc478dfd08db1e97c8f1b Status: Downloaded newer image for docker.io/mysql:latest
3)查看本地所有镜像
[root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mysql latest c7109f74d339 5 weeks ago 443 MB
4)删除指定容器
[root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mysql latest c7109f74d339 5 weeks ago 443 MB docker.io/java latest d23bdf5b1b1b 2 years ago 643 MB [root@localhost docker]# docker rmi c7109f74d339 Untagged: docker.io/mysql:latest Untagged: docker.io/mysql@sha256:415ac63da0ae6725d5aefc9669a1c02f39a00c574fdbc478dfd08db1e97c8f1b Deleted: sha256:c7109f74d339896c8e1a7526224f10a3197e7baf674ff03acbab387aa027882a Deleted: sha256:35d60530f024aa75c91a123a69099f7f6eaf5ad7001bb983f427f674980d8482 Deleted: sha256:49d8bb533eee600076e3a513a203ee24044673fcef0c1b79e088b2ba43db2c17 Deleted: sha256:2e7d501ae7df8ee0eca0fe0b16c6e9fad9a3664811445f38f32636b47366eec3 Deleted: sha256:6a362ed9781f7b3f0c63b061dfb0f14d9ba10d021df10712a131158c103b0d18 Deleted: sha256:3e58b58898622709169f72e7fa20244e084316f3ee66142728bce3f35bb9538b Deleted: sha256:4beb9add2b61bbae54c13e9d9f374b1fe8573226c2fe889bdd5dc8eed19ffbc1 Deleted: sha256:b3a6af4f3db06bbe8f661019f7dbc354b5e63318fca01d8f1e6218f0afbaa5eb Deleted: sha256:037818566b488757260ae3d0064fbf6e47365cd08dc6409b7a018a512ef17f25 Deleted: sha256:000ec65531a617f550ee04f67c875b70f4b395af00b5f5bb7ed480b17078bc0b Deleted: sha256:11d387a2efcce7596530d6be2299b6528b0afe070aba3caf3432ca31c3b0132d Deleted: sha256:a883a9d05facd6220d9251787142b41cc6675f247bc47c94071e665adf65dbbf Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e [root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/java latest d23bdf5b1b1b 2 years ago 643 MB
4.docker的容器操作
1)运行
[root@localhost ~]# docker run --name mysql_3 -e MYSQL_ROOT_PASSWORD=mysql -p 3306:3306 -d mysql 6a104e2ff87d9d2a01d1200c8aef22648b93fb821cb15e33c444e461ca4b447b [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a104e2ff87d mysql "docker-entrypoint..." 22 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql_3 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a104e2ff87d mysql "docker-entrypoint..." 9 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql_3 [root@localhost ~]# docker exec -it mysql_3 "docker exec" requires at least 2 argument(s). See 'docker exec --help'. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container [root@localhost ~]# docker exec -it mysql_3 /bin/bash root@6a104e2ff87d:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 11 Server version: 8.0.16 MySQL Community Server - GPL Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql'; Query OK, 0 rows affected (0.21 sec) mysql> flush privileges; Query OK, 0 rows affected (0.14 sec)
这一步有很多坑:
1.启动mysql必须把密码选项带上
2.centos7的防火墙不只是iptables -F 特别注意要systemctl stop firewalld.service
3.mysql 密码设置 Authentication plugin ‘caching_sha2_password’ cannot be loaded:
$ docker ps // 查看所有正在运行容器
$ docker stop containerId // containerId 是容器的ID
$ docker ps -a // 查看所有容器
$ docker ps -a -q // 查看所有容器ID
$ docker stop $(docker ps -a -q) // stop停止所有容器
$ docker rm $(docker ps -a -q) // remove删除所有容器
拷贝:docker cp /opt/test.js testtomcat:/usr/local/tomcat/webapps/test/js
docker cp testtomcat:/usr/local/tomcat/webapps/test/js/test.js /opt
docker exec -it oracle11g bash
参考资料:1.Docker 拉取 oracle 11g镜像配置