Docker Flashcards
What are challenges that docker resolves?
1) Compatibility
2) Dependencies
How to check docker version?
docker version
Run hub.docker.com/whalesay
run docker.com/whalesay cowsay Boo
List all running containers
ps
Show all containers
ps -a
Stop running container
stop
Remove container permanently
rm
List all container images
images
Remove docker image
rmi (all dependent containers are removed)
Pull image without running a container
pull
Why is container in ‘exited’ state?
Container is run when underlying process is run, otherwise is not
Run sleep cmd on ubuntu
docker run ubuntu sleep 5
Execute a cmd on a running container
exec name-or-id cat /etc/hosts
Run in attached mode
run domain\centos
Run in detached mode
1) -d
2) run -d domain/centos
Attach container
attach name-id
What is difference between attached & detached container?
1) Attached: currently loaded to shell prompt
2) Detached: running in the background, not currently loaded to shell
Specify which version of a container to run with a TAG
docker run Ubuntu:17.04
Flag to run c7s in interactive mode
1) -i
2) docker run -i domain/centos
Flag to map host port to c7s port & run
1) -p host-port:c7s-port
2) docker run -p 80:5000 domain\centos
How to map a volume
1) -v host-dir:c7s-dir
2) docker run -v /opt/datadir:/var/lib/mysql mysql
Flags to run interactive & pseudo-TTY
docker run -it domain/centos
Map local port to port of container
docker run -p 80:5000 domain/centos
Get container info in JSON
docker inspect
Access container log
docker log name-or-id
Run c7 selected version with additional cmd
docker run ubuntu:17.10 cat /etc/release
What is a limiation of running container with specified cmd?
Lack of control of current terminal session until cmd stops
How to run background cmd in a container? (detached)
docker run -d ubuntu sleep 15
How to go back (attach) to currently run container?
docker attach
Forward port & map a volume to a container
run -p 8080:8080 -v /root/dir:var/dir centos
Map a volume to a container with credentials (& port map)
run -p 8080:8080 -v /root/dir:var/dir -u root centos
Create local image based on Dockerfile with tag
docker build Dockerfile -t domain/centos
Make image available on docker-hub
docker push domain/centos
How is Dockerfile structured?
INSTRUCTION argument
What is first required Dockerfile instruction?
FROM
Get access to container history
docker history domain/centos
Build docker image from Dockerfile located in current dir
docker build .
Build docker image with specified tag-name
docker build . -t my-simple-webapp
Tag for push to docker-hub purposes
docker build . -t domain/app-name
Login to docker account in terminal
docker login
Run simple docker web-app with environment variable
1) -e
docker run -e APP_color=blue simple-web-app-color
How to find ENV:VAR on a running docker
1) docker inspect
2) Config
Example of creating ENV:VAR in Python
color = os.environ.get(‘APP_COLOR’)
Examples of CMD usage
1) CMD command param1 = CMD sleep5
2) CMD [“command”, “param1”] or CMD [“sleep”, “5”]
Entrypoint instruction
ENTRYPOINT [“sleep”]
How to use default instruction for entrypoint? It can be override
1) ENTRYPOINT [“sleep”]
2) CMD [“5”]
What are components of docker engine?
1) Daemon
2) REST API
3) CLI
Access docker by remote commands
1) -H=name:port
2) docker -H=remote-docker-engine:2375
What are components of docker namespace?
1) InterProcess 2) Mount 3) Network 4) Process ID 5) Unix Timesharing
What tools restricts amount of HW resources allocated to a container?
cgroups
Cmd to limit cpu usage to 50%
docker run –cpus=.5 centos
Cmd to limit memory usage to 100mb
run –memory=100m centos
Where docker stores data in linux FS?
/var/lib/docker
What architecture is docker based on?
Layered architecture
Components of layered architecture
1) OS
2) APT / YUM
3) PIP
4) Source code
5) Entrypoint +
6) Container layer (R/W)
Create docker volume dir
docker volume create data_volume
Mount volume inside a container
run -v /source-dir:/var/lib/mysql mysql
Where are docker volumes created?
/var/lib/docker/volumes
Examples of two syntaxes for volume mounting (1)
docker run -v /data/mysql:/var/lib/mysql mysql
How docker chooses storage drivers?
Automatically based on OS type
What networks are created automatically in docker?
1) Bridge (default)
2) none
3) host
List all networks
docker network ls
What network feature comes by default with docker? Runs at 127.0.0.11
Embedded DNS
What is docker registry?
Central repository of all docker images
What is default docker registry?
docker.io
What is more secure registry option to use?
Private registry
Deploy private registry
docker run -d -p 5000:5000 –name registry regsitry:2
Examples of two syntaxes for volume mounting (2)
1) \ –mount
2) run \ –mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql