Skip to content

Instantly share code, notes, and snippets.

@onlurking
Last active Nov 19, 2018
Embed
What would you like to do?
Docker Mastery Notes

Docker

First lecture

Verify if cli can talk to Docker engine.

docker version

View Docker engine config values.

docker info

Docker command line structure:

docker <command> (options)
docker <command> <sub-command> (options)

Example:

docker run
docker container run

Run nginx, and access in localhost

docker run --publish 80:80 nginx

--publish exposes the local port 80 and route/bind all the traffic to the process inside the container running in port 80, the ip binds read like <host>:<container>: 8080:80 maps the local port 8080 to port 80 on the container.

--detach tells docker to run the container in background.

--name will give the container a name.

List containers:

docker ls

Just ls will list only running containers, with -a flag for list all containers.

Stopping a container:

docker stop <id_hash>

Run container in background with a name:

docker run --publish 80:80 --detach --name webhost nginx

To see logs from a container:

docker logs webhost

To see the process running inside a container:

docker top webhost

To remove containers:

docker rm <container_hash>

This will not remove running containers, if you want to delete you can use the -f flag which stands for force

Second Lecture

Example for a command which uses a specific version, -d stands for --detach:

docker container run --publish 8080:80 --name webhost -d nginx:1.11 nginx -T

Let's run a mongodb container:

docker run --name mongo -d mongo

To check if the container is running use ps:

docker ps

Containers are just process running on the host machine!
List the process running inside the container:

docker top mongo

UID      PID        PPID       C     STIME      TTY       TIME           CMD
999      20396      20378      4     03:37      ?         00:00:00       mongod --bind_ip_all

See the process running in the host machine:

ps aux | grep mongo

999     20396   1.4  0.4 1086084 74516   ?   Ssl  03:37   0:01   mongod --bind_ip_all

Assignment: Manage Multiple Containers

Check the docker docs!

Run a nginx, a mysql and a httpd (apache) server, running them in backgroud (--detach, -d) and with a name (--name). Ports should be 80:80 (nginx), 8080:80 (httpd), and 3306:3306 (mysql)

When running the mysql container, pass the --env (or -e) flag with MYSQL_RANDOM_ROOT_PASSWORD=yes

And then use the docker logs to see the random passwword.

After clean it all up with docker stop, and docker rm and ensure everything is cleaned up with docker container ls -a.

Solution

docker run -d --name nginx -p 80:80 nginx
docker run -d --name mysql -p 3306:3306 -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
docker run --name httpd -d -p 8080:80 httpd:alpine

docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                               NAMES
ccc570f545ba   httpd:alpine   "httpd-foreground"       About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp                httpd
3ea33b0ca977   mysql          "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
433f43b3967b   nginx          "nginx -g 'daemon of…"   5 minutes ago        Up 5 minutes        0.0.0.0:80->80/tcp                  nginx

docker logs mysql
[...]
GENERATED ROOT PASSWORD: Vieve8fe...
[...]

docker stop mysql nginx httpd
docker rm mysql nginx httpd

Third Lecture: Process Monitoring

Show container metadata:

docker inspect mysql

Get live streaming of performances stats on container:

docker stats mysql

Fourth Lecture: Getting a Shell Inside Containers

To run a shell inside a container:

docker run -it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment