Docker学习之Dockerfile

制作ubunt-ssh-supervisor镜像

a、Dockerfile 文件

FROM ubuntu
MAINTAINER salman  ([email protected])
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
    && apt-get install -y  openssh-server sudo \
    && mkdir -p /var/run/sshd    \
    &&useradd salman   \
    && echo "salman:123456" | chpasswd  \
    && echo "salman   ALL=(ALL)       ALL" >> /etc/sudoers  \
#### 改变用户shell ###########
    && usermod -s /bin/bash salman  \
    && apt-get install -y supervisor \
    && mkdir -p /var/log/supervisor
###### add  supervisord config file #########
ADD supervisord.conf /etc/supervisor/conf.d/

EXPOSE 22

CMD [ "/usr/bin/supervisord" ]

b、supervisord.conf 文件内容

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

c、创建镜像

cd  /mnt/ubuntu-ssh-supervisor                        ##一个镜像创建一个目录                   
docker build -t="ubuntu-ssh-supervisor"  .        ##注意不要漏掉最后面的"."
docker  images                                                   ##查看本地镜像
docker run -t -d  --name "my1" -p 55:22 ubuntu-ssh-supervisor   ##启动容器

制作tomcat镜像

创建一个新的目录放Dockerfile文件,jdk和tomcat从官网下载,打包成tar包,ADD不支持解压ZIP格式

root@ubuntu:/mnt/tomcat# ll
total 190604
drwxr-xr-x 2 root root      4096 Nov  8 16:29 ./
drwxr-xr-x 6 root root      4096 Nov  8 10:49 ../
-rw-r--r-- 1 root root       254 Nov  7 17:47 Dockerfile
-rw-r--r-- 1 root root 185516936 Nov  7 17:19 jdk.tar.gz
-rw-r--r-- 1 root root       263 Nov  7 17:26 supervisord.conf
-rw-r--r-- 1 root root   9638216 Nov  7 17:19 tomcat.tar.gz

a 、 Dockerfile 文件

FROM ubuntu-ssh-supervisor
MAINTAINER salman  ([email protected])

ENV DEBIAN_FRONTEND noninteractive
ADD  tomcat.tar.gz /opt/
ADD  jdk.tar.gz  /opt/
ADD  supervisord.conf /etc/supervisor/conf.d/
EXPOSE 22  8080

CMD [ "/usr/bin/supervisord" ]

b、supervisord.conf 文件

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

[program:tomcat]
command=/opt/tomcat/bin/catalina.sh run
####指定jdk环境变量####
environment=JAVA_HOME="/opt/jdk/",JAVA_BIN="/opt/jdk/bin"
directory=/opt/tomcat
autostart = true
autorestart= true
redirect_stderr= true

c、创建镜像

cd  /mnt/tomcat                               
docker build -t="tomcat"  .                                  ##注意不要漏掉最后面的"."
docker  images                                                   ##查看本地镜像
docker run -t -d  --name "my1" -p  22:22 -p 8080:8080  tomcat    ##启动容器

制作mysql 镜像

准备:mysql的编译安装之后的完整包。mysql的安装目录/opt/mysql
a、Dockerfile 文件

FROM ubuntu-ssh-supervisor
MAINTAINER salman  ([email protected])

######add mysql  ########
ADD  mysql.tar.gz  /opt/
ADD supervisord.conf /etc/supervisor/conf.d/
EXPOSE 22  3306
CMD [ "/usr/bin/supervisord" ]

b、supervisor文件

[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D

[program:mysql]
####mysqld_safe安全模式默认是不允许root用户启动,加--user=root强制使用root ###
### 为了节省安装步骤,没有加mysql用户
command=/opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/my.cnf --user=root
autostart=true
startretries=5
autorestart=true
user=root

c 、创建镜像

 docker build -t="salman/mysql"   .
 

docker启动mysql  把数据文件挂载到本地  
##解压 mysql.tar.gz  将 mysql/data  目录拷贝到/data/mysql/data下   
###将本地目录挂载到容器下,需要保证本地目录和容器目录内容一致。不一致容器中看到的data目录为空,数据库无法启动
docker run  -t -d --name "mysql"  -p 3306:3306 -v /data/mysql/data:/opt/mysql/data   salman/mysql
###登录容器添加远程权限
root@ubuntu:/mnt/mysql# docker exec  -it  mysqlTest  /bin/bash
root@66a9c2fb0545:/opt# /opt/mysql/bin/mysql 
mysql>  use mysql 
Database changed
mysql>  update user  set  Password=PASSWORD('cwits123') where User='root' ;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 5  Changed: 4  Warnings: 0

mysql>  grant all privileges on *.* to root@"%" identified by "cwits123" with grant option; 
Query OK, 0 rows affected (0.01 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)


###验证远程登录
Hbase3:~ # mysql -uroot -p  -h192.168.1.142  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.42-debug lq-edition

Copyright (c) 2000, 2016, 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> 

附录:

ubuntu 安装mysql
(1)安装编译源码需要的包
sudo apt-get install make cmake gcc g++ bison libncurses5-dev build-essential

(2)下载并解压缩
mysql-5.6.42.tar.gz
tar -zxvf mysql-5.6.42.tar.gz
cd mysql-5.6.42

(3)编译安装
编译配置:
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/opt/mysql/data -DSYSCONFDIR=/opt/mysql  -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/opt/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DCOMPILATION_COMMENT="lq-edition" -DENABLE_DTRACE=0 -DOPTIMIZER_TRACE=1 -DWITH_DEBUG=1
编译安装:
make && make install

配置MySQL
(1)新建运行Mysql的用户和组
 groupadd mysql
 useradd -g mysql mysql
(2)设置Mysql安装目录的权限
 chown -R mysql:mysql  /opt/mysql 
(3)建立配置文件
cp /opt/mysql/support-files/my-default.cnf  /opt/mysql/my.cnf
修改配置文件:
cat  /etc/my.cnf
[client]
port = 3306
socket = /opt/data/mysql.sock
[mysqld]
port = 3306
socket = /opt/data/mysql.sock
basedir = /opt
datadir  = /opt/data

(4)初始化数据库
 /opt/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/mysql  --datadir=/opt/mysql/data/
 
(5)启动mysql服务
启动
/opt/mysql/bin/mysqld_safe --defaults-file=/opt/mysql/my.cnf    --user=mysql  &
关闭 
/usr/bin/mysqladmin -u root -p shutdown

(7)修改root密码(因为默认密码为空)
 /opt/mysql/bin/mysql -u root -p
mysql> UPDATE user SET Password=PASSWORD('你想要的密码') where USER='root';
mysql> FLUSH PRIVILEGES;

(8)授权root账户远程登录
 /opt/mysql/bin/mysql
mysql> grant ALL PRIVILEGES ON *.* to root@"%" identified by "root" WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;  

你可能感兴趣的:(Docker)