Tuesday 18 December 2018

Docker Compose Installation && Docker Compose simple usage guide

* Install docker && docker-compose(prewritten docker commands that did congfigs for container)
                Docker: - https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-using-the-repository
                Lateset docker-compose:


    curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" > ./docker-compose
                                                                sudo mv ./docker-compose /usr/bin/docker-compose
                                                                sudo chmod +x /usr/bin/docker-compose
                                

(https://stackoverflow.com/questions/49839028/how-to-upgrade-docker-compose-to-latest-version)
 
 
* Config docker to use some IP not in conflict with already existing one
                - vim /etc/docker/daemon.json
{
    "bip": "192.168.40.1/28", (just any non conflicting ip is fine)
    "ipv6": false,
    "dns": ["8.8.8.8"]
}
 
* restart docker -

   # sudo systemctl restart docker.service
 
 * Start docker service:
               - Instructions

* We'll start by creating a folder for this project: mkdir lamp-stack && cd lamp-stack
 
Create another subdirectory, /php which contains the following index.php file:
 
<!-- ./php/index.php -->
 
<html>
    <head>
        <title>Hello World</title>
    </head>
 
    <body>
        <?php
            echo "Hello, World!";
        ?>
    </body>
</html>
Populate docker-compose.yml with the following configuration:
# ./docker-compose.yml
 
version: '3'
 
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_pw_shh
      MYSQL_DATABASE: test_db
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    ports:
      - "9906:3306"
  web:
    image: php:7.2.2-apache
    container_name: php_web
    depends_on:
      - db
    volumes:
      - ./php/:/var/www/html/
    ports:
      - "8100:80"
    stdin_open: true
    tty: true
Our directory structure should look as follows:
$ tree
.
├── docker-compose.yml
└── php
    └── index.php
---DockerFile(use by docker to create an image(https://docs.docker.com/get-started/part2/#apppy))
 
 
DockerFile content:
 
FROM php:7.2.2-apache
MAINTAINER egidio docile
RUN docker-php-ext-install pdo pdo_mysql mysqli
---------------------(use php:7.2.2-apache image, use its built in command to install pdo, mysqli: https://linuxconfig.org/how-to-create-a-docker-based-lamp-stack-using-docker-compose-on-ubuntu-18-04-bionic-beaver-linux)
 
-----------------------------------------------
Execute docker-compose up -d in the terminal and load http://localhost(your url):8100/ in your browser.
Time to learn some PHP!
 
Notes
* image is from docker hub
* volume is the folder we want to retain when destroy and recreate same containers
 
 
We use port-forwarding to connect to the inside of containers from our local machine.
webserver: http://localhost:8100
db: mysql://devuser:devpass@localhost:9906/test_db
Our local directory, ./php, is mounted inside of the webserver container as /var/www/html/
The files within in our local folder will be served when we access the website inside of the container
 
 
 
(https://alysivji.github.io/php-mysql-docker-containers.html)
--------------------------------------------------------------------------------------------------
* show list of running containers:
                                - sudo docker-compose ps
* GO inside to edit contianer file:
                - you have two ways to edit file (config, source code, any files):
 
                * php_web is docker container name
 
1. docker exec -it php_web bash
2. if you have a volume, then you can edit the files inside the volume directly on the host machine.
 
or
vim ~/.bashrc
dexec() {
  docker exec -it -e TERM=$TERM -e LINES=$LINES -e COLUMNS=$COLUMNS "$@"
}
$ source ~/.bashrc
Then, you can login into your container by running:
 
dexec   CONTAINER_NAME  CMD`
 
 
* exit a container
-
// If you don't want to add an editor just to make a few small changes (e.g., change the Tomcat configuration), you can just use:
 
docker cp <container>:/path/to/file.ext
 
//which copies it to your local machine (to your current directory).
 
//Then edit the file locally using your favorite editor, and then do a
 
docker cp file.ext <container>:/path/to/file.ext
 
to replace the old file.
 
(https://stackoverflow.com/questions/30853247/how-do-i-edit-a-file-after-i-shell-to-a-docker-container)
 
 
This will first stop all the containers, next remove all the containers, and finally start them in the background as specified by the docker-compose.yml file.
 
First, cd to the directory where docker-compose.yml file is present, and then execute the following to restart.
 
cd /home/myapp (directory where docker-compose.yml file is )
 
docker-compose stop && docker-compose rm -f
 
docker-compose up -d
 
(https://www.thegeekstuff.com/2016/04/docker-compose-up-stop-rm/comment-page-1/)

No comments:

Post a Comment