Saturday, January 26, 2019

Create a dockerfile for a project (in Ubuntu18)


We learned how to use Docker and Docker-compose in these posts:
  1. How to install Docker
  2. How to make an image/a container
  3. Use Dockerfile and docker-compose
Now we will make a dockerfile and docker-compose file for this project JavaChatBoard.

Create Dockerfile

Make a folder named "java".
Copy and paste this to a txt file and name it as "Dockerfile" in the java folder.
# Most Dockerfiles start from a parent image.
# FROM *** indicates what base image you will use.
FROM centos:7

# Maintainer name
# MAINTAINER lechat thecat <frozenyogurt845@gmail.com>

# To use commands, use "RUN".
RUN yum -y update 
RUN yum install -y epel-release
RUN yum -y install https://centos7.iuscommunity.org/ius-release.rpm
RUN yum -y install git 
RUN yum -y install java-1.8.0-openjdk
RUN yum -y install maven
RUN yum -y install mariadb-server
RUN yum -y install vim

ENV MYSQL_ROOT_PASSWORD=root

# "EXPOSE" specifies which port you will open.
EXPOSE 8080

ENV PATH /opt/jdk/jdk-1.8.0/bin:$PATH

# CMD is used to start modules.
# For example: CMD ["command", "argument 1","argument 2"]
RUN cd /
RUN git clone https://github.com/lechatthecat/JavaChatBoard.git
# Caution! This downloads & installs all dependencies. It might take minutes.
RUN cd JavaChatBoard \
    && mvn clean package 
CMD exec java -jar /JavaChatBoard/target/chatboard-0.0.1-SNAPSHOT.jar --spring.config.location=file:/JavaChatBoard/src/main/resources/docker.application.properties

Create a docker-compose file

Copy and paste this to a txt file and save it as "docker-compose.yml". The path must be same with the java folder we created just now.
version: '3'
services:
    db:
        image: mariadb
        environment:
            MYSQL_ROOT_PASSWORD: 'root'
            MYSQL_USER: 'root'
            MYSQL_PASS: 'root'
        volumes:
        #This docker-entrypoint-initdb.d is used to make the initial data from .sql file.   
        - ../src/main/resources/sql:/docker-entrypoint-initdb.d
        - ./mariadb:/var/lib/mysql # Creating this folder for persisting data of the mariadb in your host
        ports:
        - "3306:3306"
        networks:
        - app_net
        container_name: mydb
    web-app:
        build: ./java
        ports:
        - "8080:8080"
        stdin_open: true
        tty: true
        depends_on:
        - db
        container_name: webapp
        networks:
        - app_net
networks:
    # Containers in same network can access each other by using its container name as host name
    app_net:
        driver: "bridge"

Start the project in docker by docker-compose

Go to the directory where we cloned the JavaChatBoard project and run these commands:
$ cd [PathToTheClonedJavaChatBoard]/JavaChatBoard/docker
$ docker-compose build
$ docker-compose up -d
After the last command "docker-compose up -d", you can see the project is running on "http://localhost:8080". The project is running in a containner using MariaDB running in another container of Docker.
To stop the project:
$ sudo docker stop $(sudo docker ps -aq)
To delete docker objects:
$ docker system prune --volumes -f
See here for the pruning.

Useful commands

To build the containers written in docker-compose:
$ docker-compose build
To up the containers with the detached mode:
$ docker-compose up -d
To build and up at the same time (this might not work if this is the first time you up the containers):
$ docker-compose up -d --build
To build without using cached data:
$ docker-compose build --no-cach
To stop the project:
$ sudo docker stop $(sudo docker ps -aq)
To delete docker objects:
$ docker system prune --volumes -f
To check containers' status:
$ docker ps -a
To access running container and use bash inside (please note that "webapp" is the target container name):
$ docker exec -it webapp /bin/bash
To Delete all info:
docker-compose down -v --rmi all --remove-orphans
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
docker volume rm $(docker volume ls -q)
To check last 50 (error) messages of a container (please note that "webapp" is the target container name):
$ docker logs --tail 50 --follow --timestamps webapp