docker-compose

19/10/2019

supprimer tous les conteneurs et volumes

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker volume prune

One line:

docker stop $(docker ps -a -q) ; docker rm $(docker ps -a -q) ; docker volume prune

creer mysql

docker run --rm -v /root/docker/mysql/data:/var/lib/mysql -p 33006:3306 --name mysqlcontainer -e MYSQL_ROOT_PASSWORD=xxx -d mysql:5

tester depuis l'interieur du conteneur

docker exec -ti mysqlcontainer bash
mysql -u root -pxxx

tester depuis l'exterieur (sur l'hôte)

mysql --host 127.0.0.1 -P 33006 -u root -pxxx

Attention: localhost ne marche pas, mais 127.0.0.1 marche bien
Attention: également accessible depuis une autre machine par defaut.

créer redis

docker run --rm -p 63790:6379 -v /root/docker/redis/data:/data --name rediscontainer -d redis

tester

redis-cli -p 63790 -h 127.0.0.1

créer mongo

docker run --rm -v /root/docker/mongo/data:/data/db -p 20081:27017 --name mongocontainer -d mongo:latest

tester

mongo -p 20081 -h 127.0.0.1

créer postfix

docker run --rm --name postfixcontainer -p "2525:25" -e SMTP_SERVER=smtp.bar.com -e SMTP_USERNAME=foo@bar.com -e SMTP_PASSWORD=XXXXXXXX -e SERVER_HOSTNAME=helpdesk.mycompany.com -d juanluisbaptiste/postfix

creer node

docker run --rm --name my-running-script -p 3000:3000 -v /root/docker/node:/home/node/site -w /home/node/site -d node:8 node server.js

tester

curl localhost:3000

server.js
emacs server.js
const express = require('express')
const app = express()
var mysql = require('mysql');

app.get('/', function (req, res) {
    res.send('ok');
});

app.get('/mysql', function (req, res) {
    var connection = mysql.createConnection({
        host     : 'mysql',
        port     : 3306,
        user     : 'root',
        password : 'xxx',
        database : 'mysql'
    });
    connection.connect();
    connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
        if (error) throw error;
        res.send(results);
    });
})

app.listen(3500, function () {
    console.log('Example app listening on port 3000!')
})

start all

emacs docker-compose.yml
version: "2"
services:
  mysql:
    restart: always
    image: mysql:5
    environment:
      - MYSQL_ROOT_PASSWORD=xxx
#      - MYSQL_PASSWORD=xxx
#      - MYSQL_DATABASE=docker_sql
#      - MYSQL_USER=docker_user
#    ports:
#      - 33006:3306
    volumes:
      - /root/docker/mysql/data:/var/lib/mysql

  node:
    depends_on:
      - mysql
    user: node
    command: "node server.js"
    image: node:8
    working_dir: /home/node/site
    ports:
      - 1616:3500
    volumes:
      - /root/docker/node:/home/node/site

  mongodb:
    image: mongo:latest
    environment:
      - MONGO_DATA_DIR=/data/db
      - MONGO_LOG_DIR=/dev/null
    volumes:
      - ./data/db:/data/db
#    ports:
#      - 27017:27017
    command: mongod --smallfiles --logpath=/dev/null # --quiet