Handbook of commands for managing multiple containers
What we want to achieve:
Run a nginx, MySQL and an apache (httpd) server.
Run all of them --detach and give them a name with --name
nginx should listen on 80:80 httpd on 8080:80 mysql on 3306:3306
Set an --env var to mysql container to pass in MYSQL_RANDOM_ROOT_PASSWORD=yes
Use docker container logs on mysql to find the random password created on startup
Clean all
Let's start ensuring that everything is clean before creating new containers
sudo docker ps
You should see a table with only the headers and without running containers
Let's create nginx container.
sudo docker container run --publish 80:80 --detach --name MyNginx nginx
Now you should see the containers running with the sudo docker ps -a
Now it's time to create our MySQL container
sudo docker container run --publish 3306:3306 --detach --env MYSQL_RANDOM_ROOT_PASSWORD="yes" --name MyMySQL mysql
Let's read some logs to find out the db psw, first you need to know what is your container's id.
you can find it in the line after your command.
In my case it starts with 4f0
so just type
sudo docker logs 4f
and search for our password that should look like this
GENERATED ROOT PASSWORD: ox1ju5oMae6paishailoxaeGhe5em8io
Now it's time to create the last container for the httpd service
sudo docker container run --publish 8080:80 --detach --name Myhttpd httpd
It's time to clean up! Now you should have something like this
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60a26e21a411 httpd "httpd-foreground" 29 minutes ago Up 24 seconds 0.0.0.0:8080->80/tcp Myhttpd 4f053e51ff2c mysql "docker-entrypoint.s…" About an hour ago Up 16 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp MyMySQL 9361d770d456 nginx "nginx -g 'daemon of…" About an hour ago Up 3 seconds 0.0.0.0:80->80/tcp MyNginx
Now lets simply run sudo docker stop for each container
chinaski@chinaski-XPS-15-9550:~$ sudo docker stop 60 60 chinaski@chinaski-XPS-15-9550:~$ sudo docker stop 4f 4f chinaski@chinaski-XPS-15-9550:~$ sudo docker stop 93 93 chinaski@chinaski-XPS-15-9550:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Fine! Now we have to delete containers
In fact if we add -a flag to ps command we can see that we have only stop containers but they are still present on our system.
chinaski@chinaski-XPS-15-9550:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60a26e21a411 httpd "httpd-foreground" 38 minutes ago Exited (0) 5 minutes ago Myhttpd 4f053e51ff2c mysql "docker-entrypoint.s…" About an hour ago Exited (0) 5 minutes ago MyMySQL 9361d770d456 nginx "nginx -g 'daemon of…" About an hour ago Exited (0) 5 minutes ago MyNginx chinaski@chinaski-XPS-15-9550:~$ sudo docker rm 60 60 chinaski@chinaski-XPS-15-9550:~$ sudo docker rm 4f 4f chinaski@chinaski-XPS-15-9550:~$ sudo docker rm 93 93
Let's check if everything's gone fine
chinaski@chinaski-XPS-15-9550:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Thanks for your attention!
PS In this tutorial i've run each command without condensed them . But you can also run commands composed like "sudo docker rm firstid secondid ..."
Have a great day!
© Andrea Borio 2018