Docker
Contents
Docker
Containers
List running containers
docker ps
List all containers
docker ps -a
Run a container
docker run [image] [command] -d detached -i interactive -t terminal (usually -it) -e VAR=value (environment variables) -v VOLUME_NAME:/container/mount/point --entrypoint=sleep [image] [attribute] --name name --network network_name --link real_container_name:target_name (adds entries to /etc/hosts) <-- Depreciated
run:version (using a tag)
Port mapping
-p (host/external):(container/internal)
Directory mapping
-v /host/dir:/container/dir
Inspect (returns json)
docker inspect [container]
Images
List images
docker image ls
Remove image (must have no containers attached)
docker rmi [image]
Dockerfile
Contains Instructions or Arguments
CMD can be as per cli or in json format
CMD command param1 CMD ["command","param1"]
FROM ubuntu:18.04 COPY . /app ADD root/bashrc /root/.bashrc ENV HOME /root WORKDIR /root RUN make /app CMD python /app/app.py
Build
docker build [path/Dockerfilefolder] -t name:tag
Create a Ubuntu container that sleeps for 5 seconds before exiting Dockerfile
From Ubuntu CMD sleep 5
docker build -t ubuntu-sleeper . docker run ubuntu-sleeper
Dockerfile
From Ubuntu ENTRYPOINT ["sleep"] CMD ["5"] (becomes the default)
docker build -t ubuntu-sleeper . docker run ubuntu-sleeper 10
Networks
docker network ls
Containers can resolve containers by name in the same network (internal dns server 127.0.0.11)
Create a network
docker network create --driver bridge --subnet 182.18.0.0/16 custom-isolated-network
Bridge
Internal network common to all docker containers in 172.17.0.0/24 network
None
Containers are isolated. Containers are not connected to any network
Host=
Container runs directly on the host network.
Storage
Default storage
/var/lib/docker
Create volume
docker volume create data_volume (generated /var/lib/docker/volumes/data_volume
Use Volume
docker run --mount data_volume:/var/lib/mysql mysql
Bind Mounting (with new syntax)
docker run --mount type=bind,source=/host/data/mysql,target=/var/lib/mysql mysql
Compose
Yaml - run multiple containers together on a single Docker Host.
docker compose up