Fedora 30
Sponsored Link

Docker : Use Docker Compose2019/05/10

 
Install Docker Compose to create or run multiple Containers easily.
[1] Install Docker Compose.
[root@dlp ~]#
dnf -y install docker-compose
[2] For example, create a config to create 2 containers, one has Apache httpd, one has MariaDB.
# create Dockerfile with httpd

[root@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

[root@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

[root@dlp ~]#
docker-compose up -d

Creating network "root_default" with the default driver
Pulling db (mariadb:)...
Trying to pull repository docker.io/library/mariadb ...
Digest: sha256:182b47379bf7c746ad990e48ef2496e2c84477bd3a74dcb164b4244bec934926
Status: Downloaded newer image for docker.io/mariadb:latest
Building web
Step 1/6 : FROM fedora
 ---> d09302f77cfc
Step 2/6 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Using cache
 ---> f59ff4ea81ea
Step 3/6 : RUN dnf -y upgrade
 ---> Running in 00e8b044adac
.....
.....
Creating root_web_1 ... done
Creating root_db_1  ... done

[root@dlp ~]#
docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a6ced9967672        mariadb             "docker-entrypoint..."   32 seconds ago      Up 29 seconds       0.0.0.0:3306->3306/tcp   root_db_1
ad280000e64c        root_web            "/usr/sbin/httpd -..."   32 seconds ago      Up 29 seconds       0.0.0.0:80->80/tcp       root_web_1

# verify accesses

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

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | a6ced9967672 |
+---------------+--------------+

[root@dlp ~]#
mysql -h 127.0.0.1 -u fedora -p -e "show databases;"

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

[root@dlp ~]#
echo "Hello Docker Compose World" > /var/lib/docker/disk02/index.html

[root@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

[root@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

[root@dlp ~]#
docker-compose logs

.....
.....
db_1   | 2019-05-10  7:35:00 0 [Note] mysqld: ready for connections.
db_1   | Version: '10.3.14-MariaDB-1:10.3.14+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]

[root@dlp ~]#
docker-compose exec db /bin/bash

root@31ff9de8ca96:/#

# stop allications

[root@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

[root@dlp ~]#
docker-compose up -d web

Starting root_web_1 ... 
Starting root_web_1 ... done
[root@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

[root@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