10

My compose-yaml has 3 services I am able to run the containers sucessfully when i am using docker-compose up. Now i want to build these 3 containers into single image. Is it possible? here is my compose-yaml

version: '2'

services:
  tomcat:
    container_name: tomcatcomposejdk
    build: .
    image: 'apexits/ubuntu-oracle-jdk8-tomcat9'
    ports:
      - "8787:8080"
      - "5003:5003"
    networks:
      b:
        ipv4_address: 10.5.0.6
    expose:
       - "8787"
       - "5003"

  mysql:
    container_name: mysqlcompose
    build: .
    image: 'mysql:5.6.36'
    ports:
      - "3306:3306"
    expose:
      - "3306"  
    networks:
      b:
        ipv4_address: 10.5.0.7
    environment:
      MYSQL_DATABASE: "bird251"
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    volumes:
      - ./BIRD251.sql:/tmp/BIRD251.sql
      - ./import.sh:/tmp/import.sh

  elasticsearch:
    container_name: escompose
    build: .
    image: 'elasticsearch:2.3.4'
    ports:
      - "9200:9200"
      - "9300:9300"
    expose:
      - "9200"
      - "9300"
    networks:
      b:
        ipv4_address: 10.5.0.8

networks:
  b:
    driver: bridge
    ipam:
     config:
       - subnet: 10.5.0.0/16
         gateway: 10.5.0.1
yamenk
  • 46,736
  • 10
  • 93
  • 87
Sunil Sunny
  • 531
  • 7
  • 30
  • 1
    Not recommended and won't be supported officially. Instead what you can do is build a custom Dockker image, for example, having 'apexits/ubuntu-oracle-jdk8-tomcat9' as a base and you install and configure mysql and elasticsearch services instead of mixing full-fledged Docker images which will always cause big nightmares. – Vane Trajkov Nov 25 '17 at 11:51

1 Answers1

6

This is not recommended at all. You will need to reverse engineer each image and copy the needed binaries/files into the combined image. The approach for that is to use docker multistage build:

FROM apexits/ubuntu-oracle-jdk8-tomcat9 as tomcat

FROM mysql:5.6.36 as mysql

FROM elasticsearch:2.3.4
COPY --from=tomcat /.../tomcat-installtion .../tomcat-installation
COPY --from=mysql /.../mysql-installtion .../mysql-installation
...

This approach is very trick and you need to reverse engineer each image to figure out which files/folder/config need to be copied onto the combined image...

Alternatively, you can start from one of the images and install the other programs using standard installation guidelines for each.

Even if you are successful with that, you will need to start multiple processes in same container which is not recommended and will introduce many complexities.

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • 2
    Thnxx for your comments..Now i tried building mysql,tomcat,elasticsearch and an another service on windows single image..i am not able to run all these services..i am getting an error as Error occurred during initialization of VM Could not reserve enough space for object heap ...please help me on this.. – Sunil Sunny Nov 28 '17 at 10:58
  • Did you follow steps in https://docs.docker.com/engine/admin/multi-service_container/ – yamenk Nov 28 '17 at 10:59