A list of my commonly used Docker commands.
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
When you install Docker on a linux host you're actually installing three different competence.
DOCKER ENGINE
- DOCKER CLI
- REST API
- DOCKER DEAMON
-
The DOCKER CLI is nothing but the command line interface to perform actions such as running a container stopping containers destroying images etc. It uses the REST API to interact with the DOCKER DEAMON.
-
The DOCKER REST API server is the API interface that programs can use to talk to the DEAMON and provide instructions.
-
The DOCKER DEAMON is a background process that manages Docker objects such as the images containers volumes and networks.
Dockers CLI need not necessarily be on the same host.
Simply use the -h option on the docker command and specify the remote Docker engine address and the port.
docker -H={{remote-docker-engine}}:{{port}} run {{image}}
docker version
Before Going Deeper, We Are Going To Update Some Configuration In Our Bash File By Running Few Command:
sudo apt install net-tools
sudo apt install jq
sudo vi ~/.bashrc
Paste The Below Code At The Bottom Of The bashrc File
dps() {
docker ps $@ --format "table{{ .ID}}\\t{{ .Image }}\\t{{ .Names }}\\t{{ .Status }}\\t{{ .Command }}\\t{{ .Status }}\\t{{ .Ports }}" | awk '
NR % 2 == 0 {
printf "\033[0;32m";
}
NR % 2 == 1 {
printf "\033[0;34m";
}
NR == 1 {
printf "\033[1;31m";
PORTSPOS = index($0, "PORTS");
PORTS = "PORTS";
PORTSPADDING = "\n";
for(n = 1; n < PORTSPOS; n++)
PORTSPADDING = PORTSPADDING " ";
}
NR > 1 {
PORTS = substr($0, PORTSPOS);
gsub(/, /, PORTSPADDING, PORTS);
}
{
printf "%s%s\n\n", substr($0, 0, PORTSPOS - 1), PORTS;
}
END {
printf "\033[0m";
}
'
}
dpsa() { dps -a $@; }
dpsjson() {
docker ps --format "{{json .}}" --no-trunc | jq
}
source ~/.bashrc
dps
dpsjson
Run an instance of the image on the docker host, if image is not on the host it will go to docker hub and pull
| PATTERN | DESCRIPTION |
|---|---|
docker run image |
RUN A CONTAINER FROM AN IMAGE (By default the container is running in attach mode) |
docker run image:tag |
RUN A CONTAINER FROM AN IMAGE FOR A SPECIFIC VERSION |
docker run -d image |
RUN A CONTAINER IN DETACH MODE (If you would like to attach back to the running container) docker attach conatiner |
docker run -i image |
RUN CONTAINER IN INTERACTIVE MODE |
docker run -it image |
RUN CONTAINER IN INTERACTIVE MODE AND ATTACHED TO TERMINAL |
when you run the docker run Ubuntu command, it runs an instance of ubuntu image and exits immediately.
If you list all containers using docker ps -a including those that are stopped you will see that the new container you ran is in an exited stage.
Because containers are not meant to host an operating system.
Containers are meant to run a specific task or process, Once the task is complete the container exits a container only lives as long as the process inside it is alive.
If the web service inside the container is stopped or a crash then the container exits.
This is why when you run a container from an ubuntu image it stops immediately because Ubuntu is just an image of an operating system that is used as the base image for other applications. There is no process or application running in it by default.
docker run nginxis same asdocker run docker.io/nginx/nginx
image:docker.io/nginx/nginx
wheredocker.iois the REGISTRY (Default)nginxis the USER ACCOUNT (if not given use the same name as the IMAGE REPOSITORY)nginxis the IMAGE REPOSITORY
DOCKER REGISTRY (docker.io, google's registry gcr.io, aws, azure, dcp)
docker pull `image`
docker ps
docker ps -a
docker ps | less -S
docker ps --format 'table {{ .ID }}\t{{.Image}}\t{{ .Names }}\t{{ .Ports }}' -s
docker ps --format 'table {{ .ID }}\t{{.Image}}\t{{ .Names }}\t{{ .Ports }}' | less -S
docker ps --format "{{json .}}" --no-trunc
docker ps --format "{{json .}}" --no-trunc | jq
docker stop `conatiner`
docker images
docker rm `container` `container` `container`
Must ensure no conatiner running off of that image, must stop and delete all container first
docker rmi `image`
docker exec -it CONTAINER_ID /bin/bash
docker exec -it CONTAINER_ID bash
docker exec -it CONTAINER_ID sh
docker exec -it `container` ps -eaf
Default IP of a docker container is 172.X.0.X, this is internal IP and only accessible within docker host.
Default IP of the docker host is 192.168.1.X / host.docker.internal, this IP is accessible outside the docker host.
MAP port to docker container
-p 80:5000
docker run -v /opt/datadir:/var/lib/mysql mysql
By running this command we are going to save mysql data in our local storage so that even if the container is remove we have the data.
docker inspect `container`
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' `container_id`
docker inspect `container_id` | grep IPAddress
docker inspect `container_id` | jq .[0].NetworkSettings.Networks[].IPAddress
docker logs `container`
docker history `container`
| PATTERN | DESCRIPTION |
|---|---|
| docker network ls docker network ls | less -S |
LIST NETWORK |
docker network rm network |
REMOVE NETWORK |
docker network inspect network |
SHOW NETWORK INFORMATION |
docker network connect network container |
CONNECT A CONTAINER TO A NETWORK |
docker network disconnect network container |
DISCONNECT A CONTAINER TO A NETWORK |