Installing Docker under Linux (Ubuntu / Mint)

package manager installation
sudo apt-get install
manual installation
remove sudo requirement
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
hello world
sudo docker run hello-world
starting / stopping the daemon
service docker status
service docker start
service docker stop
service docker restart
viewing the docker daemon log
journalctl -u docker
enabling debug mode
sudo vi /etc/docker/daemon.json
{ "debug": true }
uninstall docker
sudo apt remove docker docker-engine containerd runc

using docker

docker help
docker help
docker [command] --help
show general info
sudo docker info
show container details
docker inspect [container]
create and start new container
sudo docker run [image]
start an existing container
docker start [id]
stop container (find container_id using docker ps)
sudo docker stop [container_id]
remove container
docker rm -f [name]
remove all containers
docker container rm -f $(docker container ps -aq)
show running containers
sudo docker ps
show cpu and memory usage of all containers
sudo docker stats
show processes running in a specific container
sudo docker top [container_id]
detach from active container
ctrl-p, q
attach to container running in background
docker attach [container_id]
update docker image
sudo docker pull [image]
run a command in a container
sudo docker exec [container_id] [command]
open linux shell to container
sudo docker exec -it [container_id] bash
show logs
docker logs [container]
copy from host to container
docker cp [file] [container]:[path]
copy from container to $pwd on host
docker cp [container]:[path] .
copying files between containers is not supportedshow localhost ports used (-P)
docker port [container]
show filesystem changes for container (compared to image)
docker container diff [container_id]

docker ps (processes)

show running containers
sudo docker ps
show ALL containers
sudo docker ps -a
sudo docker ps --all
show ids only (for scripts)
sudo docker ps -q
sudo docker ps -aq

docker run (new container)

run interactive linux docker
sudo docker run -it ubuntu bash
interactive (pseudo-TTY)
automatically remove container and associated file system on exit
name a container
-- name [name]
assign an environment variable
-e [name]=[value]
launch in background (detached)
expose bound ports
bind image port to host
-p [host_port]:[container_port]
make port available to localhost only
mount persistent volume on image
-v [host_dir]:[image_dir]
mount all persistent volumes used in another container
--volumes-from [container_id]
set work directory
-w [dir]
--workdir [dir]
start container every time docker starts
disable networking (communicate through volumes)
run container in host network
run containers with shared network
run container with extended privileges (can fix problems in some cases)
run desktop application in docker
docker run --privileged -v /tmp/.X11-unix/:/tmp/.X11-unix/ -e DISPLAY=$DISPLAY chromium


list volumes
docker volume ls
create volume
docker volume create [name]
get info on volume
docker volume inspect [volume_name]
remove volume
docker volume rm [volume_name]
remove all unused volumes
docker volume prune
example: create and mount shared volume
docker volume create myvol
docker run --name terminal1 -v myvol:/myvol -it ubuntu
docker run --name terminal2 -v myvol:/myvol -it ubuntu
example: anonymous volume
docker run -v /tmp/anonymous -it ubuntu
example: mount local directory
docker run -v /tmp/mountme:/data --rm --name volume -it ubuntu

docker network

list networks
docker network ls
create a new network
docker network create [network]
delete network (disconnect containers first)
docker network rm [network]
connect container to network
docker network connect [network] [container]
disconnect container from network
docker network disconnect [network] [container]
examine network details
docker inspect [network]

docker images

search for images
docker search nosql
list local images
docker image ls
docker image list
inspect image
docker inspect [image]
list image development history
docker image history [container_id]
assign additional name tag to image
docker image tag [image] [tag]
remove image
docker image rm [id]
remove unused images
docker image prune -a
remove all local images
docker image rm $(docker image ls -q)
save local docker image (from docker repo) to file
docker image save [tag] > [tar_file]
docker image load < [tar_file]
save running container (without layers)
docker container export [container_id] > [tar_file]
docker image import < [tar_file]
common images
image hub

Postgres image

pull latest stable
docker pull postgres
pull specific version
docker pull postgres:[postgres_version]
run image
docker run --name postgres -e POSTGRES_PASSWORD=[password] -e POSTGRES_DB=[database_name] -p -v [persistent_dir]:/var/lib/postgresql/data  postgres
environment variables
POSTGRES_PASSWORD=super user password
POSTGRES_USER=super user name
POSTGRES_DB=default database, defaults to [POSTGRES_USER]

mysql image

run latest
docker run --rm -d --name mysql --network=host -e MYSQL_ROOT_PASSWORD=secret -d -p 3306:3306 mysql

Java image

java hello world
sudo docker run --rm openjdk java -version
mounting and running an application
sudo docker run --rm -v "$PWD":/app -w /app openjdk java -jar [jar]
sudo docker run --rm -v "$PWD":/app openjdk java -jar app/[jar]

Apache image

Apache hello world
sudo docker run --rm -P httpd
sudo docker run --rm -p 80:80 httpd
mount document root
sudo docker run --rm -p 80:80 -v [dir]:/usr/local/apache2/htdocs/ httpd:2.4
reload config
docker cp httpd.conf [container]:/usr/local/apache2/conf/httpd.conf
docker exec -it [container] apachectl graceful

Jenkins image

Jenkins hello world
docker run --network bridge --publish 8080:8080 --rm jenkins/jenkins
<3>ubuntu imagerun ubuntu
docker run --rm -it ubuntu
connecting to container ip
docker run --rm -it ubuntu
apt update
apt install net-tools python
python -m SimpleHTTPServer
curl [ip]:8000 # on host