# Usage syntax:
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Options:
-h, --hostname string Container host name
-m, --memory bytes Memory limit
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--name string Assign a name to the container
--network network Connect a container to a network
--rm Automatically remove the container when it exits
--restart string Restart policy to apply when a container exits (default "no", )
-t, --tty Allocate a pseudo-TTY
-v, --volume list Bind mount a volume
-p, --publish list Publish a container's port(s) to the host
# docker create --name "c-nginx" nginx
a1165a3bad27914b9c17a4dc6aabb23c275279999ef244b4ec975d51f9ec8d8a
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1165a3bad27 nginx "/docker-entrypoint.…" 40 hours ago Created c-nginx
- Run a command in new container,
docker run
command first creates a writeable container layer over the specified image, and then starts it using the specified command.
# Usage syntax:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Options:
-h, --hostname string Container host name
-m, --memory bytes Memory limit
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--name string Assign a name to the container
--network network Connect a container to a network
--rm Automatically remove the container when it exits
--restart string Restart policy to apply when a container exits (default "no", )
-t, --tty Allocate a pseudo-TTY
-v, --volume list Bind mount a volume
-p, --publish list Publish a container's port(s) to the host
# docker run --name test -it ubuntu
root@60d1dfd49d81:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@60d1dfd49d81:/# hostname
60d1dfd49d81
# pwd
/data/nginx_files
# ls
index.html
# cat index.html
<h1>This mount volume nginx</h1>
# docker run --name mount_nginx -it -v /data/nginx_files/:/usr/share/nginx/html nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/02/11 18:38:14 [notice] 1#1: using the "epoll" event method
2022/02/11 18:38:14 [notice] 1#1: nginx/1.21.6
2022/02/11 18:38:14 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/11 18:38:14 [notice] 1#1: OS: Linux 4.19.0-18-amd64
2022/02/11 18:38:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/11 18:38:14 [notice] 1#1: start worker processes
2022/02/11 18:38:14 [notice] 1#1: start worker process 30
# docker inspect 30d82d7be95f | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
# curl 172.17.0.2
<h1>This mount volume nginx</h1>
# docker run -p 8080:80 -it -d --name publish_port nginx
a23eb76fdd9bac210923463680ca97c908b5ba417d15f01e861bd6977d9b6ca3
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a23eb76fdd9b nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second 0.0.0.0:8080->80/tcp publish_port
# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1970/docker-proxy
# curl -I 192.168.93.146:8080
HTTP/1.1 200 OK
Server: nginx/1.21.6
Date: Fri, 11 Feb 2022 18:45:16 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 25 Jan 2022 15:03:52 GMT
Connection: keep-alive
ETag: "61f01158-267"
Accept-Ranges: bytes
# Usage syntax:
docker ps [OPTIONS]
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a23eb76fdd9b nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp publish_port
# docker ps -as
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
a23eb76fdd9b nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp publish_port 1.52kB (virtual 142MB)
a888c44f4cb4 hello-world "/hello" 4 days ago Exited (0) 4 days ago eloquent_jennings 0B (virtual 13.3kB)
- Start/Stop/Restart/CheckStats containers
# Usage syntax:
docker start/stop/restarat/stats [OPTIONS] CONTAINER [CONTAINER...]
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a23eb76fdd9b nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:8080->80/tcp publish_port
# docker stop publish_port
publish_port
# docker start publish_port
publish_port
# docker restart publish_port
publish_port
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a23eb76fdd9b nginx "/docker-entrypoint.…" 8 minutes ago Up 1 second 0.0.0.0:8080->80/tcp publish_port
# docker stats publish_port
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a23eb76fdd9b publish_port 0.00% 2.055MiB / 987.2MiB 0.21% 0B / 0B 0B / 0B 2
- Run a command in a running container
# Usage syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
# docker exec -it a23eb76fdd9b /bin/bash
root@a23eb76fdd9b:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
- Remove one or more containers
# Usage syntax:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a23eb76fdd9b nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp publish_port
a888c44f4cb4 hello-world "/hello" 4 days ago Exited (0) 4 days ago eloquent_jennings
# docker rm a23eb76fdd9b
Error response from daemon: You cannot remove a running container a23eb76fdd9bac210923463680ca97c908b5ba417d15f01e861bd6977d9b6ca3. Stop the container before attempting removal or force remove
# docker rm -f a23eb76fdd9b
a23eb76fdd9b
# docker rm a888c44f4cb4
a888c44f4cb4
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# Usage syntax:
docker images [OPTIONS] [REPOSITORY[:TAG]]
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres 13 33f7fa4a9c0f 7 weeks ago 371MB
nginx latest f652ca386ed1 2 months ago 141MB
mysql 5.7.36 938b57d64674 3 months ago 448MB
mysql 8.0.27 ecac195d15af 3 months ago 516MB
mysql 5.6.51 f3b364958c23 4 months ago 303MB
hello-world latest feb5d9fea6a5 4 months ago 13.3kB
# docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.36 938b57d64674 3 months ago 448MB
mysql 8.0.27 ecac195d15af 3 months ago 516MB
mysql 5.6.51 f3b364958c23 4 months ago 303MB
# docker images -f "reference=my*"
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.36 938b57d64674 3 months ago 448MB
mysql 8.0.27 ecac195d15af 3 months ago 516MB
mysql 5.6.51 f3b364958c23 4 months ago 303MB
# docker images -q
33f7fa4a9c0f
f652ca386ed1
938b57d64674
ecac195d15af
f3b364958c23
feb5d9fea6a5
- Search the Docker Hub for images
# Usage syntax:
docker search [OPTIONS] TERM
Options:
-f, --filter filter Filter output based on conditions provided
--limit int Max number of search results (default 25)
# docker search debian
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 13669 [OK]
...
itscaro/debian-ssh debian:jessie 28 [OK]
...
casept/debian-amd64 A debian image built from scratch. Mostly fo… 0
1and1internet/debian-9-nginx-php-7.2-wordpress-4 debian-9-nginx-php-7.2-wordpress-4 0 [OK]
# docker search -f is-official=true debian
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 13669 [OK]
debian Debian is a Linux distribution that's compos… 4169 [OK]
- Download an image or a repostory from a registry
# Usage syntax:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Options:
-a, --all-tags Download all tagged images in the repository
-q, --quiet Suppress verbose output
# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
0c6b8ff8c37e: Pull complete
Digest: sha256:fb45fd4e25abe55a656ca69a7bef70e62099b8bb42a279a5e0ea4ae1ab410e0d
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest
# docker pull debian@sha256:fb45fd4e25abe55a656ca69a7bef70e62099b8bb42a279a5e0ea4ae1ab410e0d
docker.io/library/debian@sha256:fb45fd4e25abe55a656ca69a7bef70e62099b8bb42a279a5e0ea4ae1ab410e0d: Pulling from library/debian
Digest: sha256:fb45fd4e25abe55a656ca69a7bef70e62099b8bb42a279a5e0ea4ae1ab410e0d
Status: Image is up to date for debian@sha256:fb45fd4e25abe55a656ca69a7bef70e62099b8bb42a279a5e0ea4ae1ab410e0d
docker.io/library/debian@sha256:fb45fd4e25abe55a656ca69a7bef70e62099b8bb42a279a5e0ea4ae1ab410e0d
# Usage syntax:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
# docker images debian
REPOSITORY TAG IMAGE ID CREATED SIZE
debian latest 04fbdaf87a6a 2 weeks ago 124MB
# docker tag debian wildsre.com/debian:v1.0
# docker images
debian latest 04fbdaf87a6a 2 weeks ago 124MB
wildsre.com/debian v1.0 04fbdaf87a6a 2 weeks ago 124MB
- Push an image or a repository to a registry
# Usage syntax:
docker push NAME[:TAG]
# docker push wildsre.com/debian:v1.0
- Build an image from Dockfile
# Usage syntax:
docker build [OPTIONS] PATH | URL | -
-t, --tag list Name and optionally a tag in the 'name:tag' format
# Usage syntax:
docker network ls [OPTIONS]
Options:
-f, --filter filter Provide filter values (e.g. 'driver=bridge|none|host')
--no-trunc Do not truncate the output
-q, --quiet Only display network IDs
# docker network ls
NETWORK ID NAME DRIVER SCOPE
b22dfcac0219 bridge bridge local
8afd246383f4 host host local
80155bcf6d01 none null local
# Usage syntax:
docker network create [OPTIONS] NETWORK
Options:
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipv6 Enable IPv6 networking
--subnet strings Subnet in CIDR format that represents a network segment
# docker network create --subnet 172.21.0.0/16 --ip-range 172.21.240.0/20 personal-blog
b8dd1dc5475903970a210acede3f281f7fc348b488147d6f0c0116d4c13e14bb
# docker network ls
NETWORK ID NAME DRIVER SCOPE
b22dfcac0219 bridge bridge local
8afd246383f4 host host local
80155bcf6d01 none null local
b8dd1dc54759 personal-blog bridge local
# docker inspect b8dd1dc54759
[
{
"Name": "personal-blog",
"Id": "b8dd1dc5475903970a210acede3f281f7fc348b488147d6f0c0116d4c13e14bb",
"Created": "2022-02-11T16:25:16.026921337-08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.21.0.0/16",
"IPRange": "172.21.240.0/20"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
- Remove one or more networks
# Usage syntax:
docker network rm NETWORK [NETWORK...]
# docker network ls
NETWORK ID NAME DRIVER SCOPE
b22dfcac0219 bridge bridge local
8afd246383f4 host host local
80155bcf6d01 none null local
89a301e1c310 personal-blog bridge local
# docker network rm 89a301e1c310
89a301e1c310
- Remove all unused networks
# Usage syntax:
docker network prune [OPTIONS]
Options:
-f, --force Do not prompt for confirmation
# docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
personal-blog
# docker volume ls
DRIVER VOLUME NAME
local 6ed3c573a17d371becb0151e7f661a4a2c7f15b212a8bded82c5a40c42f73aed
local 33d9775c333668270ade7eeed1ab1af402df570846868d677a1d1a5e35b3fc75
local 69c7b804734c09c68da411d2b44181293571ed95280bb5922539da4f3fd8ed7f
local 703c8583e4b5bd59745f9cc28c7fc924b5ec197ed4b1311d0300561333ad1230
local 3186da226aa38772ea95e19136a746ae152b9e1209e8c288227109752094da4c
local 526297da2e4b6f42121fb464070d78c353daa94085fcae517ac23207c2cfe1a4
# docker inspect 6ed3c573a17d371becb0151e7f661a4a2c7f15b212a8bded82c5a40c42f73aed
[
{
"CreatedAt": "2022-02-07T16:09:16-08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/6ed3c573a17d371becb0151e7f661a4a2c7f15b212a8bded82c5a40c42f73aed/_data",
"Name": "6ed3c573a17d371becb0151e7f661a4a2c7f15b212a8bded82c5a40c42f73aed",
"Options": null,
"Scope": "local"
}
]
# Usage syntax:
docker volume create [OPTIONS] [VOLUME]
Options:
-d, --driver string Specify volume driver name (default "local")
-o, --opt map Set driver specific options (default map[])
# docker volume create --driver local --opt device=tmpfs --opt type=tmpfs --opt o=size=100m bbb
bbb
# docker inspect bbb
[
{
"CreatedAt": "2022-02-11T17:00:20-08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/bbb/_data",
"Name": "bbb",
"Options": {
"device": "tmpfs",
"o": "size=100m",
"type": "tmpfs"
},
"Scope": "local"
}
]
- Display system-wide information
# docker info
# docker version
# Usage syntax:
docker system df [OPTIONS]
Options:
-v, --verbose Show detailed information on space usage
# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 10 5 4.537GB 3.399GB (74%)
Containers 7 1 17.7MB 1.546kB (0%)
Local Volumes 39 3 1.737GB 1.195GB (68%)
Build Cache 0 0 0B 0B
- Get real time events from the server
# Usage syntax:
docker system events [OPTIONS]
Options:
-f, --filter filter Filter output based on conditions provided
--format string Format the output using the given Go template
--since string Show all events created since timestamp
--until string Stream events until this timestamp
# Start a container logs
# docker system events
2022-02-11T22:21:58.275966600-08:00 network connect bf55d1075651691ff120df785cf2bcf40b3a0cce3e6181c55efc7fa0fdcf0f9d (container=326d9b5fbb1094cf18ed9f0b3664e386689bd0896563260d955bac1ad5627ee5, name=bridge, type=bridge)
2022-02-11T22:21:58.671812100-08:00 container start 326d9b5fbb1094cf18ed9f0b3664e386689bd0896563260d955bac1ad5627ee5 (image=hello-world, name=vigorous_wright)
2022-02-11T22:21:58.703083600-08:00 container die 326d9b5fbb1094cf18ed9f0b3664e386689bd0896563260d955bac1ad5627ee5 (exitCode=0, image=hello-world, name=vigorous_wright)
2022-02-11T22:21:58.765497000-08:00 network disconnect bf55d1075651691ff120df785cf2bcf40b3a0cce3e6181c55efc7fa0fdcf0f9d (container=326d9b5fbb1094cf18ed9f0b3664e386689bd0896563260d955bac1ad5627ee5, name=bridge, type=bridge)