Docker
These are notes of doing the cybrary Intro do Docker course.
Installation
Install docker on Debian based Linux machine (VM or physical)
or
and give your user the rights to run docker so that you don't need to sudo the docker commands.
Docker images
Create a docker image and run it
Build a new image of ubuntu with nginx webserver capabilities. We'll name it ubuntu-nginx.
TODO
Automate docker images
Create a directory, create a dockerfile and describe your file with the commands used in the previous section.
mkdir ubuntu-nginx | ls
cat dockerfile >
Create the webserver file
cat default >
Create the files that will be copied to the web server file path
Build the image
You'll see your image in the list with docker image ls
Rebuild with docker build . -t ubuntu-nginx
Remove your image with docker rmi ubuntu-nginx
Layers and rebuilding images
Show the layers of the container image with
If you change only one line in the dockerfile and rebuild the image, then the build is very fast because the image was built using the existing layers from the previous docker image builds. If some of these layers are being used in other containers, they can just use the existing layer instead of recreating it from scratch
Run your image in a container
Get the id of your running container and search for the IPAddress.
Insert the address into your browser.
Make your image available to everyone on dockerhub
push the project to github
sign-in to hub.docker.com and connect the git account
create new reposiory with an automated build on your github project
Docker Network
modes of networking: bridge=bridge containers together, host=directly plug the container to the hosts network adapter hosts port is directly mapped to the container, none, overlay=run over another network like VPNS used for swarms, MACVLAN=assignes a MAC address to a container making it appear as a physical device
Creating networks
list the networks
create networks
assign networks. Container should be stopped.
Mode: bridge (default)
containers are bridged together in a network
containers within a bridge see their names
containers within the different networks can ping each other by IP address
Mode: host
container is directly connected to the host machine
no port mapping
only one host network on one host
only one container instance per port
If you start a second container with --net host then it will exit immediately as you can see in docker container ls -a
Mode: none
no network
only one instance with network=none per host
Docker subnets
You can modify a static configuration file in /etc/docker/daemon.json and specify your bridge networks ip range:
/lib/systemd/system/docker.service
Docker port mapping
If you want a container communicate with an external system you have to map a porton the host machine with
docker container run -P image_name
docker container run -P hostport:containerport imagename
Show port mappings for container: docker port container_id/name
Dockerfile: EXPOSE port and run the container wit the -P flag without specifying a port (it is already specified in the dockerfile).
Example
Docker Storage handling
Volumes
/var/lib/docker/volumes, managed by docker, persistent
best for: cloud options, for backup and restore
Bind mounts
managed by the host system, absolut path has to be used, can reference to non existing data, less performant, security risks
best for: sharing files between containers and host system, sharing of files that need to be shared with the host, files and directories with a consistent structure
tmpfs
non persistant, in the RAM
best for: sensitive information such as credentials
Example with volumes
Create a volume
Assign the volume
Use the volume, create a file
Create a container and assign the volume as read-only
Test that it's readonly
Example with bind mount
Mount a volume /home/username/vol-h to vol-1 of the container
Verify that the volume has been mounted
Now on the host machine I create a file
Which is accessible in the container
Another way is to map a non existing volume, docker creates it, but it will not be accessible without root permissions by the host system
Example with tmpfs
No src in the create command. Destination is the mount point on the container. Create a container with a
Map volumes within dockerfile
Create dockerfile:
build the image
run a container with the image, --mount must be specified
now we see that everything from the destination volumes gets copied onto the source volume.
you can remove the files on the host machine.
Docker orchestration: compose
Is a tool for managing multiple containers. Uses a YAML file for configuration. Start/stop multiple containers with one command line. It also is a single documentation of an entire environment in a single file. It's easier to move services between environments.
Installation
Get your services running
run it with docker-compose up then check if the web app is running using localhost:5000 and localhost 5001 in your browser.
Docker UI: portainer
Is a UI for managing docker containers.
Installation
Get Ipaddress and log in. I use grep to get the ip address, instead of format
Ok, I open a browser using 172.105.0.2:9000
Docker local registry
Docker Reference
Basic Docker commands
docker container run <containerID or name>
docker ls -a (for all running and non running containers)
docker stats --no-stream (single output of the stats)
docker stats --no-stream --no-trunc (output with IDs and other info without truncating)
Modes
-i interacive mode
-d deamon
-t terminal allocation
docker container run -idt --name=<containername> <imagename>
Useful inspects
Getting IPAddress
json format of containers in the network network1
or use grep:
inspect a specific container
Helpful docker resources
More labs
Last updated