My linux world » Docker create service

Docker create service


First let’s create a tomcat app

Create file ‘Dockerfile’ with the following content

# Use an official image of centos
FROM centos

# Download and install tomcat
RUN yum -y install wget
RUN mkdir -p /opt/java
RUN wget -O /opt/java/apache-tomcat-8.5.14.tar.gz http://apache.crihan.fr/dist/tomcat/tomcat-8/v8.5.14/bin/apache-tomcat-8.5.14.tar.gz
RUN cd /opt/java && tar xvfz apache-tomcat-8.5.14.tar.gz
RUN ln -s /opt/java/apache-tomcat-8.5.14 /opt/java/apache-tomcat
RUN yum -y install java-1.8.0-openjdk 

# Set the working directory to $CATALINA_HOME
ENV CATALINA_HOME /opt/java/apache-tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
WORKDIR $CATALINA_HOME

# Make port 8080 available to the world outside this container
EXPOSE 8080

# Run catalina.sh when container launches
CMD ["catalina.sh", "run"]

And create my tomcat app:

docker build -t centos-tomcat85 .

Now you can test it, run the command

docker run -p 1234:8080 centos-tomcat85 .

And meet you here : http://localhost:1234

Now let’s create our service

Create file ‘docker-compose.yml’ with the following content

version: "3"
services:
  web:
    image: centos-tomcat85:latest
    deploy:
      replicas: 2
      resources:
        limits:
          cpus: "0.5"
          memory: 500M
      restart_policy:
        condition: on-failure
    ports:
      - "80:8080"
    networks:
      - webnet
networks:
  webnet:

Note :

Init swarm :

docker swarm init

And deploy the service

docker stack deploy -c docker-compose.yml centos-tomcat85

Now you can test it, run the command

docker stack ps centos-tomcat85

And meet you here : http://localhost

To stop the app :

docker stack rm centos-tomcat85

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.