GithubHelp home page GithubHelp logo

dockerdev's Introduction

DockerDev

A few scripts to quickly setup a local environment for web development based on Docker.

Plus, some miscellaneous notes and links - since I'm not familiar with Docker, yet.

Cheat sheet

Command Purpose
docker container ls -a List all containers
docker container ls -a -q List all containers in "quiet" mode
docker rm $(docker container ls -a -q) Remove all containsers
docker image ls -a List images
docker rmi Remove image
docker exec -it /bin/bash Get a bash shell in the container
docker exec -it '' Execute whatever command you specify in the container.

Running an istantaneous Ubuntu Machine

We will run a Ubuntu machine working with an interactive docker container.

This is the very ground level knowledge on how using Docker as a user environment, rather than a standalone container with an app running in it.

    docker run --interactive --tty ubuntu /bin/bash

What we did:

  • we created a ubuntu machine with a random name

or:

    docker run --name ubuntu -v /Users/morlandi/tmp/sources:/src -t -i ubuntu /bin/bash

What we did:

  • we created a ubuntu machine named "ubuntu"
  • with a volumes attached to it, directly from our machine
  • we mapped the volume using the "-v" flag [local]:[destination],

The lifetime of a container is the life time of its single main process.

Time to check docker, see what happened with it:

    docker ps -a

    CONTAINER ID        IMAGE               COMMAND             NAMES
    e41423f7856b        ubuntu              "/bin/bash"         ubuntu
    ed5887209364        ubuntu              "/bin/bash"         cool_proskuriakova

Finally, we cleanup the images:

    docker rm -f cool_proskuriakova
    docker rm -f ubuntu

Mac docker volume mount using osxfs

Docker Desktop for Mac started using osxfs for supporting volume mounting on MacOS.

The following command mounts the ~/Desktop directory to the docker container:

    docker run -it -v ~/Desktop:/Desktop ubuntu bash

Proof:

from the container:

    root@71f6e65abc6e:/# ls /
    bin  boot  Desktop  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

    root@71f6e65abc6e:/# ls -l /Desktop/

        -rw-r--r-- 1 root root 127138 Jul 17 14:52 '/Desktop/Screenshot 2020-07-17 at 16.51.58.png'
        -rw-r--r-- 1 root root 648435 Jul 17 15:47 '/Desktop/Screenshot 2020-07-17 at 17.47.21.png'
        -rw-r--r-- 1 root root 249486 Jul 17 16:15 '/Desktop/Screenshot 2020-07-17 at 18.15.15.png'
        -rw-r--r-- 1 root root 821404 Jul 18 08:57 '/Desktop/Screenshot 2020-07-18 at 10.57.38.png'

from the host:

    $ ls -l ~/Desktop

        -rw-r--r--@ 1 morlandi  staff  127138 Jul 17 16:52 Desktop/Screenshot 2020-07-17 at 16.51.58.png
        -rw-r--r--@ 1 morlandi  staff  648435 Jul 17 17:47 Desktop/Screenshot 2020-07-17 at 17.47.21.png
        -rw-r--r--@ 1 morlandi  staff  249486 Jul 17 18:15 Desktop/Screenshot 2020-07-17 at 18.15.15.png
        -rw-r--r--@ 1 morlandi  staff  821404 Jul 18 10:57 Desktop/Screenshot 2020-07-18 at 10.57.38.png

Links

Exercise with mysql/mysql-server

References

(1) Creazione di un container da immagine "mysql/mysql-server"

docker run --name=mysql1 mysql/mysql-server
  • In questo caso ci siamo limitati ad assegnare un nome al container ("mysql1") e abbiamo evitato il flag -d che provoca l'esecuzione del container in background
  • In questo modo, vediamo tranquillamente i messaggi del container durante il processo di bootstrap, che sono abbastanza istruttruttivi:
❯ docker run --name=mysql1 mysql/mysql-server
[Entrypoint] MySQL Docker Image 8.0.27-1.2.6-server
[Entrypoint] No password option specified for new database.
[Entrypoint]   A random onetime password will be generated.
[Entrypoint] Initializing database
...
[Entrypoint] GENERATED ROOT PASSWORD: Wry1GTT28?v8ujwX_9+3.J0g2:AY:B,;
...
[Entrypoint] MySQL init process done. Ready for start up.
...

In particolare, notare la riga GENERATED ROOT PASSWORD: Wry1GTT28?v8ujwX_9+3.J0g2:AY:B,; che come spiegato dalla descrizione dell'immagine e' una password random generata per l'utente root.

(2) Connessione da mysql client e sostituzione password

Avendo assegnato al container un nome noto, possiamo collegarci da un terminale con il client mysql inserendo la password di cui sopra:

$ docker exec -it mysql1 mysql -uroot -p

e magari sfruttare la connessione per impostare una nuova password per root:

> alter user root@localhost identified by 'password';

(3) Distruzione del container

Verifica dei containers is esecuzione:

$ docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS                    PORTS                       NAMES
2d988632ff2b   mysql/mysql-server   "/entrypoint.sh mysq…"   10 minutes ago   Up 10 minutes (healthy)   3306/tcp, 33060-33061/tcp   mysql1

rm non funziona:

$ docker rm mysql1
Error response from daemon: You cannot remove a running container 2d988632ff2b1fec008261838c7330733a7af98387778f48b6dbb7cd5f532e96. Stop the container before attempting removal or force remove

Bisogna prima stoppare il container oppure usare il flag -force:

$ docker stop mysql1
$ docker rm mysql1
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Poiche' i docker containers sono stateless, ricostruendoli si perdono tutti i dati precedentemente memorizzati (a meno di non attaccarli a un Volume)

(4) Nuova esecuzione (questa volta in background)

Basta aggiungere l'opzione -d:

$ docker run -d --name=mysql1 mysql/mysql-server
b4f683a1d7f7655ebbc2c505f0d307d8558d362db47b18c6d00ac1112b8f7517

Possiamo comunque sbirciare i messaggi prodotti durante l'avvio del container come segue:

$ docker logs mysql1
...
[Entrypoint] GENERATED ROOT PASSWORD: 3F9h#V.2.PW6_e:6+kd3aP?A5YK2^Jdg
...

(5) Soluzione definitiva

Per convenienza pubblichiamo la porta 3306 per poter accedere in localhost dall'host; se la porta fosse gia' in uso, potremmo usarne un'altra (sull'host; quella del container "mysql1" e' necessariamente 3306 cioe' la porta di default di Mysql)

docker run -d --name=mysql1 --env MYSQL_ROOT_PASSWORD=password --env MYSQL_ROOT_HOST=% --publish 0.0.0.0:3306:3306 mysql/mysql-server

MYSQL_ROOT_PASSWORD: This variable specifies a password that is set for the MySQL root account.

MYSQL_ROOT_HOST: By default, MySQL creates the 'root'@'localhost' account. This account can only be connected to from inside the container as described in Connecting to MySQL Server from within the Container. To allow root connections from other hosts, set this environment variable. For example, the value 172.17.0.1, which is the default Docker gateway IP, allows connections from the host machine that runs the container. The option accepts only one entry, but wildcards are allowed (for example, MYSQL_ROOT_HOST=172...* or MYSQL_ROOT_HOST=%).

Inoltre possiamo utilizzare un docker volume per garantire la persistenza in caso di ricostruzione del container; in questo modo i dati creati non verranno persi e potranno essere riutilizzati.

$ docker run -d --name mysql1 --env MYSQL_ROOT_PASSWORD=password --env MYSQL_ROOT_HOST=% --volume ~/DockerDev/tmp/mysql1:/var/lib/mysql --publish 0.0.0.0:3306:3306 mysql/mysql-server

dockerdev's People

Contributors

morlandi avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.