Docker, put simply, is a container platform – which solves the issue of running code that is OS agnostic (to an extent) and allows for the application to be freed from it’s underlying OS/kernel dependencies. Simply put, I can run multiple versions of the same application in a container, without having to worry about dependencies and conflicts! In DevOps environment, that is just pure gold! I can have ready to ship code which can be essentially packaged into VM or OS agnostic bits that just work!
Unlike older versions the newer docker Desktop versions need you to register to the https://hub.docker.com portal and also gives you a desktop tray icon. For a MAC, this looks like this
Installation is again pretty straightforward. You register at https://hub.docker.com and download the install package. Then doubleclick the dmg file and once that opens with the Docker package, just click and drag the package to the Applications folder and sign in. As simple as that!
Now, to run and ensure Docker has installed fine, you can use the hello-world that is the hello world image from Docker. But before that, just run docker without any arguments to ensure docker fires up correctly
Ayans-MacBook-Pro:~ ayan$ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default
"/Users/ayan/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/Users/ayan/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/Users/ayan/.docker/cert.pem")
--tlskey string Path to TLS key file (default
"/Users/ayan/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
Now that you know docker works, check the hello-world and see what comes up
Ayans-MacBook-Pro:~ ayan$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
So that was fun! There’s an additional example that is provided which can be run to check out further functionality
Ayans-MacBook-Pro:~ ayan$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
84ed7d2f608f: Pull complete
be2bf1c4a48d: Pull complete
a5bdc6303093: Pull complete
e9055237d68d: Pull complete
Digest: sha256:868fd30a0e47b8d8ac485df174795b5e2fe8a6c8f056cc707b232d65b8a1ab68
Status: Downloaded newer image for ubuntu:latest
root@1459d58895b0:/#
root@1459d58895b0:/# uname -rn
1459d58895b0 4.9.125-linuxkit
Sweet! So we just created an Ubuntu container (basic) and logged in using the root credentials and the bash shell. Let’s run a top to check out the other processes running
#top
top - 16:03:13 up 27 min, 0 users, load average: 0.03, 0.05, 0.01
Tasks: 2 total, 1 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2047036 total, 344688 free, 267540 used, 1434808 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 1620344 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
17 root 20 0 36612 3108 2624 R 0.3 0.2 0:00.01 top
1 root 20 0 18504 3472 3052 S 0.0 0.2 0:00.05 bash
Nice! Now let’s check out the container properties. Open another terminal and check out the docker processes
Ayans-MacBook-Pro:~ ayan$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1459d58895b0 ubuntu "bash" 5 minutes ago Up 5 minutes angry_mccarthy
Not bad at all. Now deleting the container. Let’s first list out the total containers in use
Ayans-MacBook-Pro:~ ayan$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1459d58895b0 ubuntu "bash" 6 minutes ago Up 6 minutes angry_mccarthy
8147c23f59a1 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago stoic_shockley
29bf5f2277e9 hello-world "/hello" 26 minutes ago Exited (0) 26 minutes ago dreamy_payne
You can delete off the stopped containers using the rm command
Ayans-MacBook-Pro:~ ayan$ docker rm 8147c23f59a1 29bf5f2277e9
8147c23f59a1
29bf5f2277e9
Check the list of containers again
Ayans-MacBook-Pro:~ ayan$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1459d58895b0 ubuntu "bash" 9 minutes ago Up 9 minutes angry_mccarthy
Stop all running containers
Ayans-MacBook-Pro:~ ayan$ docker container stop $(docker container ls -aq)
1459d58895b0
Remove/delete all containers
Ayans-MacBook-Pro:~ ayan$ docker container rm $(docker container ls -aq)
1459d58895b0
Ayans-MacBook-Pro:~ ayan$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Ayans-MacBook-Pro:~ ayan$
Now, though the containers are gone, the images will still be there, right?
Ayans-MacBook-Pro:~ ayan$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 5 days ago 1.84kB
ubuntu latest 1d9c17228a9e 8 days ago 86.7MB
To remove the images, we can run the rm command
Ayans-MacBook-Pro:~ ayan$ docker image rm fce289e99eb9 1d9c17228a9e
Untagged: hello-world:latest
Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:868fd30a0e47b8d8ac485df174795b5e2fe8a6c8f056cc707b232d65b8a1ab68
Deleted: sha256:1d9c17228a9e80a0a23927f24f3cf17d012cf0bb3eae5e3541a8c6987ab9bd5a
Deleted: sha256:3288cd6e6e7d42bcb4a74121b412c42a11f96da52685e42dbf9de6a747a55c6b
Deleted: sha256:b1636589630239bdb9153f95ac564bcd2afd9202aaf8511cbf5a9a37e03daf35
Deleted: sha256:043f492f40c539cfe7cee4cb8aae00ed1d5b19e864fbe6ea35ec92a2333bacc4
Deleted: sha256:2fb7bfc6145d0ad40334f1802707c2e2390bdcfc16ca636d9ed8a56c1101f5b9
Ayans-MacBook-Pro:~ ayan$
Check for volumes
Ayans-MacBook-Pro:~ ayan$ docker volume ls
DRIVER VOLUME NAME
Check for networks
Ayans-MacBook-Pro:~ ayan$ docker network ls
NETWORK ID NAME DRIVER SCOPE
361746435c82 bridge bridge local
38babddf71cc host host local
cbaafcdbb22d none null local
For network, I find the prune option easier
Ayans-MacBook-Pro:~ ayan$ docker network prune
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Stay tuned for more!