- Head on to https://docs.docker.com/get-docker and select the appropriate OS
- Since I'll be doing it on Ubuntu 20.04 - https://docs.docker.com/engine/install/ubuntu/
- Now there are 3 options to install -
- I'll be using the 3rd option, so only 3 commands here -
curl -fsSL https://get.docker.com -o get-docker.sh
chmod +x get-docker.sh
sudo sh get-docker.sh
- For running docker in rootless mode, refer to this link
- Check the docker version:
sudo docker version
- Do a simple test - head on to https://hub.docker.com/r/docker/whalesay
- We'll use this image for a test run
sudo docker pull docker/whalesay Save the whales!
- pull the image and run it
- Next time it will run from the local image
Command | Description |
---|---|
docker version |
to check the version of docker installed |
docker run nginx |
to start a container of nginx from image (if already exist, else it will pull from hub) |
docker run --name myserver nginx |
to start a container of nginx with a name |
docker ps |
list all running containers |
docker ps -a |
to list all containers - currently running as well stopped ones |
docker stop containerID/name |
to stop a running container provide either name or ID (first few letter works as long as it unique) |
docker rm containerID/name |
to remove a container permanently |
docker rm -f containerID/name |
to remove a running container permanently |
docker images |
to see a list of available images |
docker rmi name |
to delete a docker image |
docker pull nginx |
to only pull the docker image and not run the container |
docker exec ubuntu cat /etc/passwd |
to execute command in a container |
docker run -d ubuntu:latest |
to run and detach a container |
docker attach ID/name |
to attach back to the running container |
docker cp nginx:file.txt data/ |
to copy a file from container to host [cp container:source target ] (just like scp ) |
docker cp data.txt nginx:file.txt |
to copy a file from host to container [cp target container:source ] |
Command | Description |
---|---|
docker run redis:4.0 |
to run a container with a different version, specify with name:tag format (will take latest tag by default) |
docker run -i name |
to run in interactive mode (by default won't take input from STDIN) |
docker run -it name |
to attach a psuedo terminal to the container |
docker run -p 69:3306 mysql |
to map the port 69 of localhost/dockerhost to port 3306 of the container running mysql (-p HOST:CONTAINER) |
docker run -P mysql |
to map all ports |
docker run -v /opt/datadir:/var/lib/mysql |
to persist the data : map a dir outside the container on docker host to a dir inside the container (-v HOST:CONTAINER) |
docker inspect name/ID |
to inspect a container (output JSON) |
docker logs name/ID |
to view logs of a container |
docker stats |
to view stats of running containers |
docker top name/ID |
to view processes of a container |
Command | Description |
---|---|
docker run -e BG_COLOR=blue simple-webapp-color |
to set the environment variable for our container |
docker inspect name/ID |
to find the env var in a running container, refer to config section in output |
Command | Description |
---|---|
docker build Dockerfile -t webserver/release-one |
to build a docker image with a Dockerfile and give it a tag |
docker tag Ubuntu Ubuntu:20.04 |
to tag an image |
docker build . |
build an image from Dockerfile in current directory |
docker save nginx > nginx.tar |
save an image to tar file |
docker load -i nginx.tar |
load an image from tar file |
docker push webserver/release-one |
to host it on docker hub community (docker registry) |
- In case of
CMD
, when we pass a cmd line argument,docker run ubuntu sleep 10
, it will replace theCMD
in the Dockerfile - In case of
ENTRYPOINT
, when we pass a cmd line argument,docker run ubuntu 10
, it will get appended toENTRYPOINT
in the Dockerfile - If user doesn't pass an arg, use both of them instead (the cmds in Dockerfile should be in Json format)-
FROM Ubuntu
ENTRYPOINT ["sleep"]
CMD ["5"]
The CMD
will get appended to ENTRYPOINT
, else with the cmd line arg, if specified.
docker run --entrypoint blah ubuntu 10
: to override default entrypoint
-
When installed docker, it creates 3 networks automatically -
- Bridge: the default network a container gets attached to (private + internal). Usually in range of
172.17.x.y
- None: container not attached to any network; no connection to external host or other containers, they'll run in completely isolated network
- Host: connect directly to the host network
- Bridge: the default network a container gets attached to (private + internal). Usually in range of
-
docker run Ubuntu --network=none
: Can specify a different network
To access the containers externally -
- Either port map from container to host
- OR run the container on host network:
docker run Ubuntu --network=host
- No network isolation left
- Now we can access it on same port directly
- BUT can't use the same port for same another container - unlike before
-
By default docker only creates 1 internal bridge network. We can create more and group containers -
Command Description docker network create --driver bridge --subnet 182.18.0.0/16 custom-iso-net
to create a new bridge network docker network ls
to list all networks docker inspect name/ID
to check a container's IP, mac, subnet, etc. -
Docker has a built-in DNS server that helps the containers to resolve each other using the container name
- This DNS server always runs at -
127.0.0.11
- This DNS server always runs at -
-
Docker uses network namespaces that creates a separate namespace for each container. It then uses virtual ethernet to connect containers together.
Command | Description |
---|---|
/var/lib/docker |
location in file system where docker store its data |
docker volume create data_vol |
create a volume for persistence storage in /var/lib/docker/data_vol |
docker run -v data_vol:/var/lib/mysql mysql |
to mount the vol in the container |
docker run --mount type=bind,source=~/Desktop/data,target=/var/lib/mysql mysql |
Using -v is old style, use this instead |
volume mount
mounts from volume dir in/var/lib/docker
whilebind mount
mounts from any dir
Topic name | Cheatsheet Link |
---|---|
Docker-Compose | link |
Docker-Swarm | link |