docker-compose部署应用并保存镜像

使用docker-compose部署容器并制作镜像

在安装好docker-compose后可以使用docker-compose文件部署应用环境。

下面记录一个部署mysql容器并且将compose部署的容器做成镜像留作以后使用

  • 新建一个compose工作目录
mkdir  /opt/db/mysql

在/opt/db目录下编写docker-compos.yml文件

version: '3'
services:
  database:
    image: mysql:5.6
    restart: always
    container_name: mysql-server
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/my.cnf:/etc/my.cnf
      - /data/mysql:/var/lib/mysql
      - /var/log/mysql:/var/log/mysql

    environment:
      MYSQL_ROOT_PASSWORD: 123456
    networks:
      - mysqld

networks:
  mysqld:
  • 在docker宿主机创建上面挂载的相应目录
mkdir -p /data/mysql/binlog
mkdir -p /var/log/mysql
  • 在/opt/db/mysql目录中存放my.sql配置文件,内容如下(配置内容可以根据需求修改):
[mysqld]
datadir=/data/mysql
socket=/var/run/mysqld/mysqld.sock
log-bin=mysql-bin.log
binlog-format="STATEMENT"
expire_logs_days= 15
[mysqld_safe]
log-error=/var/log/mysql/mysqld.log
pid-file=/var/run/mysql/mysqld.pid

  • 使用compose文件运行容器,使用docker-compose指令必须要进入docker-compose.yml文件所在的目录才习惯,在此之外的目录无法执行;
[root@localhost db]# pwd
/opt/db
[root@localhost db]# ls
docker-compose.yml  mysql

#部署
[root@localhost db]# docker-compose up -d
Creating network "db_mysqld" with the default driver
Pulling database (mysql:5.6)...
5.6: Pulling from library/mysql
a5a6f2f73cd8: Already exists
936836019e67: Already exists
283fa4c95fb4: Already exists
1f212fb371f9: Already exists
e2ae0d063e89: Already exists
ee4f72e1975b: Already exists
44df6e1fae20: Already exists
f0038784249f: Already exists
43eb2805d29c: Already exists
6c4093ac5972: Already exists
8df42a7acb6e: Already exists
Digest: sha256:9e4754be7edf813fed12f9283e86678694da71b0a7d4fefe082dfb9020a3ac99
Status: Downloaded newer image for mysql:5.6
Creating mysql-server ... done

  • 查看容器运行
[root@localhost db]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
9f7bcc794d24        mysql:5.6           "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes        0.0.0.0:3306->3306/tcp   mysql-server
2e9e2ee44ef3        nginx:1.14          "nginx -g 'daemon of…"   18 hours ago        Up 3 hours          0.0.0.0:80->80/tcp       server_web_1

将compose运行的容器做成镜像,方便以后直接使用

[root@localhost db]# docker commit mysql-server gudaoyufu/mysql:5.6_v2
sha256:eedf57513e8dd7fcfa8c893971deaf477ab44ba0966148fc13b1a8ac1058febb
  • 查看新的镜像
[root@localhost db]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
gudaoyufu/mysql     5.6_v2              eedf57513e8d        About a minute ago   256MB
gudaoyufu/mysql     5.6                 1b3e6027c875        3 hours ago          256MB

将镜像上传到docker hub仓库

[root@localhost db]# docker login -u gudaoyufu
Password: 
Login Succeeded
  • 上传
[root@localhost db]# docker push gudaoyufu/mysql:5.6_v2
The push refers to repository [docker.io/gudaoyufu/mysql]
5953264ab234: Pushed 
119c93d6ccc5: Layer already exists 
1dadbfda87dc: Layer already exists 
89bb4ce949c4: Layer already exists 
46ecad004d82: Layer already exists 
dae415ebeca7: Layer already exists 
3c392b9bb7a4: Layer already exists 
2566141f200b: Layer already exists 
0ad177796f33: Layer already exists 
0f1205f1cd43: Layer already exists 
a588c986cf97: Layer already exists 
ef68f6734aa4: Layer already exists 
5.6_v2: digest: sha256:a4e8a47f7e9b14f167df52307e44eb244fb97972a0906f06fe03422bc013ec34 size: 2828

  • 登录到docker hub查看刚刚上传的新镜像

《docker-compose部署应用并保存镜像》

  • 宿主机登录测试正常,本地登录mysql要使用如下的方式指定mysql-server容器的ip地址
mysql -uroot -p -h 172.28.0.2

从docker hub拉镜像并运行

从docker hub上拉取下来自己做的镜像运行时要指定好相应的参数,比如上面刚上传的镜像,运行时要映射好对应的端口,数据目录,日志文件目录,配置文件等,就是docker-compse.yml中挂载的几个目录。

[root@localhost db]# docker run --name mysql -itd -p 3306:3306 -v /opt/db/mysql/my.cnf:/etc/my.cnf -v /data/mysql:/var/lib/mysql -v /var/log/mysql:/var/log/mysql gudaoyufu/mysql:5.6_v2

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
922f6334e6cc        gudaoyufu/mysql:5.6_v2   "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds       0.0.0.0:3306->3306/tcp   mysql
2e9e2ee44ef3        nginx:1.14               "nginx -g 'daemon of…"   19 hours ago        Up 4 hours          0.0.0.0:80->80/tcp       server_web_1
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注