SLES 15
Sponsored Link

Docker : Use Docker Compose
2019/01/22
 
Install Docker Compose to create or run multiple Containers easily.
[1] Install Docker Compose.
# add extension module first

dlp:~ #
SUSEConnect -p PackageHub/15/x86_64

dlp:~ #
zypper -n install docker-compose
[2] For example, create a config to create 2 containers, one has Apache httpd, one has MariaDB.
# create Dockerfile with httpd

dlp:~ #
vi Dockerfile
FROM fedora
MAINTAINER ServerWorld <admin@srv.world>

RUN dnf -y upgrade
RUN dnf -y install httpd

EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

# define application configuration

dlp:~ #
vi docker-compose.yml
version: '3'
services:
  db:
    image: mariadb
    volumes:
      - /var/lib/docker/disk01:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: fedora
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: fedora_db
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/docker/disk02:/var/www/html

# build and run application

dlp:~ #
docker-compose up -d

Creating network "root_default" with the default driver
Building web
Step 1/6 : FROM fedora
 ---> 26ffec5b4a8a
Step 2/6 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Using cache
 ---> f366bb5fd2ba
Step 3/6 : RUN dnf -y upgrade
 ---> Running in f3ddd418f683
.....
.....
Creating root_web_1
Creating root_db_1 ... done

dlp:~ #
docker ps

CONTAINER ID    IMAGE      COMMAND                  CREATED          STATUS           PORTS                    NAMES
6193fa67dcfc    mariadb    "docker-entrypoint.s…"   18 seconds ago   Up 16 seconds    0.0.0.0:3306->3306/tcp   root_db_1
3e064442b62d    root_web   "/usr/sbin/httpd -D …"   18 seconds ago   Up 16 seconds    0.0.0.0:80->80/tcp       root_web_1

# verify accesses

dlp:~ #
mysql -h 127.0.0.1 -u root -p -e "show variables like 'hostname';"

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | 6193fa67dcfc |
+---------------+--------------+

dlp:~ #
mysql -h 127.0.0.1 -u fedora -p -e "show databases;"

Enter password:
+--------------------+
| Database           |
+--------------------+
| fedora_db          |
| information_schema |
+--------------------+

dlp:~ #
echo "Hello Docker Compose World" > /var/lib/docker/disk02/index.html

dlp:~ #
curl 127.0.0.1

Hello Docker Compose World
[3] The follows are some other basic Operation for Docker Compose.
# show status of application

dlp:~ #
docker-compose ps

   Name                 Command              State           Ports
---------------------------------------------------------------------------
root_db_1    docker-entrypoint.sh mysqld     Up      0.0.0.0:3306->3306/tcp
root_web_1   /usr/sbin/httpd -D FOREGROUND   Up      0.0.0.0:80->80/tcp

# show logs of application

dlp:~ #
docker-compose logs

.....
.....
db_1   | 2018-11-06  8:15:05 0 [Note] mysqld: ready for connections.
db_1   | Version: '10.3.10-MariaDB-1:10.3.10+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

# run commands inside a container

# for container name, it's just the name set in [docker-compose.yml]

dlp:~ #
docker-compose exec db /bin/bash

root@31ff9de8ca96:/#

# stop allications

dlp:~ #
docker-compose stop

Stopping root_web_1 ... done
Stopping root_db_1  ... done

# run an application particularly

# if it has dependency, another container also runs

dlp:~ #
docker-compose up -d web

Starting root_web_1 ... 
Starting root_web_1 ... done
dlp:~ #
docker-compose ps

   Name                 Command              State          Ports
------------------------------------------------------------------------
root_db_1    docker-entrypoint.sh mysqld     Exit 0
root_web_1   /usr/sbin/httpd -D FOREGROUND   Up       0.0.0.0:80->80/tcp

# remove applications

# running container won't be removed

dlp:~ #
docker-compose rm

Going to remove root_web_1
Going to remove root_db_1
Are you sure? [yN] y
Removing root_db_1 ... done
Removing root_web_1 ... done
Matched Content
 
Tweet