Docker相关

Docker实战

2019-02-21  本文已影响0人  索伦x

Docker 构建 Tomcat

查找 Docker Hub 上的 Tomcat 镜像

root@UbuntuBase:/usr/local/docker/tomcat# docker search tomcat
NAME                           DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tomcat                         Apache Tomcat is an open source implementa...   1550                [OK]                
dordoka/tomcat                 Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   43                                      [OK]
tomee                          Apache TomEE is an all-Apache Java EE cert...   42                  [OK]                
davidcaste/alpine-tomcat       Apache Tomcat 7/8 using Oracle Java 7/8 wi...   21                                      [OK]
consol/tomcat-7.0              Tomcat 7.0.57, 8080, "admin/admin"              16                                      [OK]
cloudesire/tomcat              Tomcat server, 6/7/8                            15                                      [OK]
maluuba/tomcat7                                                                9                                       [OK]
tutum/tomcat                   Base docker image to run a Tomcat applicat...   8                                       
jeanblanchard/tomcat           Minimal Docker image with Apache Tomcat         8                                       
andreptb/tomcat                Debian Jessie based image with Apache Tomc...   7                                       [OK]
bitnami/tomcat                 Bitnami Tomcat Docker Image                     5                                       [OK]
aallam/tomcat-mysql            Debian, Oracle JDK, Tomcat & MySQL              4                                       [OK]
antoineco/tomcat-mod_cluster   Apache Tomcat with JBoss mod_cluster            1                                       [OK]
maluuba/tomcat7-java8          Tomcat7 with java8\.                             1                                       
amd64/tomcat                   Apache Tomcat is an open source implementa...   1                                       
primetoninc/tomcat             Apache tomcat 8.5, 8.0, 7.0                     1                                       [OK]
trollin/tomcat                                                                 0                                       
fabric8/tomcat-8               Fabric8 Tomcat 8 Image                          0                                       [OK]
awscory/tomcat                 tomcat                                          0                                       
oobsri/tomcat8                 Testing CI Jobs with different names.           0                                       
hegand/tomcat                  docker-tomcat                                   0                                       [OK]
s390x/tomcat                   Apache Tomcat is an open source implementa...   0                                       
ppc64le/tomcat                 Apache Tomcat is an open source implementa...   0                                       
99taxis/tomcat7                Tomcat7                                         0                                       [OK]
qminderapp/tomcat7             Tomcat 7                                        0

这里我们拉取官方的镜像

docker pull tomcat

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 tomcat 的镜像。

docker images

运行容器:

暴露tomcat容器到宿主机的8080端口

docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat

命令说明:

查看容器启动情况

root@UbuntuBase:/usr/local/docker/tomcat/webapps# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
38498e53128c        tomcat              "catalina.sh run"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->8080/tcp   tomcat

通过浏览器访问


访问容器端口

Docker 构建 MySQL

查找 Docker Hub 上的 MySQL 镜像

root@UbuntuBase:/usr/local/docker/mysql# docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relati...   5177                [OK]                
mariadb                                                MariaDB is a community-developed fork of M...   1602                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Crea...   361                                     [OK]
percona                                                Percona Server is a fork of the MySQL rela...   298                 [OK]                
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          72                                      
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       62                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be li...   53                                      [OK]
sameersbn/mysql                                                                                        48                                      [OK]
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server ...   36                                      [OK]
tutum/mysql                                            Base docker image to run a MySQL database ...   27                                      
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          17                                      [OK]
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic back...   16                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   15                                      
linuxserver/mysql                                      A Mysql container, brought to you by Linux...   12                                      
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   6                                       
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                                       
frodenas/mysql                                         A Docker Image for MySQL                        3                                       [OK]
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                3                                       [OK]
circleci/mysql                                         MySQL is a widely used, open-source relati...   2                                       
cloudposse/mysql                                       Improved `mysql` service with support for ...   0                                       [OK]
astronomerio/mysql-sink                                MySQL sink                                      0                                       [OK]
ansibleplaybookbundle/rhscl-mysql-apb                  An APB which deploys RHSCL MySQL                0                                       [OK]
cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0                                       
astronomerio/mysql-source                              MySQL source                                    0                                       [OK]
jenkler/mysql                                          Docker Mysql package                            0                                       

这里我们拉取官方的镜像

docker pull mysql

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 mysql 的镜像

运行容器:

docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-v /usr/local/docker/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql

命令参数:

查看容器启动情况

root@UbuntuBase:/usr/local/docker/mysql# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
bc49c9de4cdf        mysql:latest        "docker-entrypoint..."   4 minutes ago       Up 4 minutes        0.0.0.0:3306->3306/tcp   mysql

使用客户端工具连接 MySQL

使用客户端工具连接 MySQL
*如果连接不上,设置一下登录权限
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 
FLUSH PRIVILEGES;

部署项目到容器

解决多容器的网络问题

#创建子网
sudo docker network create --subnet 172.10.0.1/16 my_net
#mysql加入子网
docker run -p 3306:3306 --net=my_net --name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-v /usr/local/docker/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql
#tomcat加入子网
docker run --name tomcat --net=my_net -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/ROOT -d tomcat

Docker 常用命令

查看 Docker 版本

docker version

从 Docker 文件构建 Docker 映像

docker build -t image-name docker-file-location

运行 Docker 映像

docker run -d image-name

查看可用的 Docker 映像

docker images

查看最近的运行容器

docker ps -l

查看所有正在运行的容器

docker ps -a

停止运行容器

docker stop container_id

删除一个镜像

docker rmi image-name

删除所有镜像

docker rmi $(docker images -q)

强制删除所有镜像

docker rmi -r $(docker images -q)

删除所有虚悬镜像

docker rmi $(docker images -q -f dangling=true)

删除所有容器

docker rm $(docker ps -a -q)

进入 Docker 容器

docker exec -it container-id /bin/bash

查看所有数据卷

docker volume ls

删除指定数据卷

docker volume rm [volume_name]

删除所有未关联的数据卷

docker volume rm $(docker volume ls -qf dangling=true)

从主机复制文件到容器

sudo docker cp host_path containerID:container_path

从容器复制文件到主机

sudo docker cp containerID:container_path host_path
上一篇下一篇

猜你喜欢

热点阅读