In my earlier articles we have seen how to install docker and docker compose. If you missed the part, please check the links for installing docker and docker compose.
In this article we will see how to work with docker in brief.
The first basic command to check your docker installation by running a simple docker image on your system.
$ docker run hello-world
jhony@ljlinux:~$ sudo docker run hello-world
ERRO[0428] Handler for POST /v1.28/containers/create returned error: No such image: hello-world:latest
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
WARN[0466] Couldn't run auplink before unmount /var/lib/docker/aufs/mnt/32a11b389bd894b1e127cc8f4a344612649e7053b5e7c8c89c52a3e7347e459e-init: exec: "auplink": executable file not found in $PATH
WARN[0467] Couldn't run auplink before unmount /var/lib/docker/aufs/mnt/32a11b389bd894b1e127cc8f4a344612649e7053b5e7c8c89c52a3e7347e459e: exec: "auplink": executable file not found in $PATH
INFO[0468] No non-localhost DNS nameservers are left in resolv.conf. Using default external servers: [nameserver 8.8.8.8 nameserver 8.8.4.4]
INFO[0468] IPv6 enabled; Adding default IPv6 external servers: [nameserver 2001:4860:4860::8888 nameserver 2001:4860:4860::8844]
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/
What is a docker image:
Docker image is an application available on docker cloud. We can run the image directly or we can download and execute it. Now we will download an image called Busybox from docker cloud and run it.
The command used to download the docker image is docker pull image_name.
$ docker pull busybox
If you face permission denied issue try executing with sudo.
$ sudo docker pull busybox
jhony@ljlinux:~$ sudo docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
76df9210b28c: Pull complete
Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Status: Downloaded newer image for busybox:latest
To list the downloaded images use docker images command. It will give you the details of the image size,ID and image created day.
$ sudo docker images
jhony@ljlinux:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 10 days ago 1.22MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
Running the docker image:`
We can directly run the docker image by run command but anyway the image will be downloaded to the system. These images runs in a virtual environment called as docker container. Each container has an unique ID.
$ sudo docker run busybox echo hello from Linux Tutorial Spot
jhony@ljlinux:~$ sudo docker run busybox echo hello from Linux Tutorial Spot
hello from Linux Tutorial Spot
Checking the status of your containers:
The command ps with the flag -a will list all the containers either it is running or in exited state.
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46494ebaff77 busybox "echo hello from L..." 7 seconds ago Exited (0) 3 seconds ago pedantic_bassi
4e1cc2f0e3ea hello-world "/hello" 20 seconds ago Exited (0) 16 seconds ago zen_easley
The above commands will give the
Container ID
Image Name
Command Used
Container created time
Status of the container
Port number
Names
Getting into a container:
Since we are not getting into OS level, the busybox and hello-world are just a simple application and it just executes the command we pass. So the status are showing as Exited.
If we are executing the commands by logging into the container then the container status will be shown as UP.
So to get into a container we need to add i and t flag while executing run command with shell.
$ sudo docker run -it busybox sh
jhony@ljlinux:~$ sudo docker run -it busybox sh
/ # echo Hello From LTS!!!
Hello From LTS!!!
Accessing a Running container:
The run command with -it flag will create a container and gets log into it. If we want to access a running or existing container we need to use exec command following with the Container_ID.
$ sudo docker exec -it c9baa86f6b0a sh
jhony@ljlinux:~$ sudo docker exec -it c9baa86f6b0a sh
/ # echo Hello From LTS!!!
Hello From LTS!!!
Run ps command on different tab so you can check the status of a running container.
$ sudo docker ps
jhony@ljlinux:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9baa86f6b0a busybox "sh" 14 seconds ago Up 11 seconds cranky_mirzakhani
Stopping a container:
The command docker stop Container_ID will stop the running container.
If we are having n number of containers in our system , we can filter it by querying its status. So it would be easy for us to delete them.
To list the stopped container:
$ sudo docker ps -a -q -f status=exited
To list the running container:
$ sudo docker ps -a -q -f status=running
Deleting all stopped containers:
After listing the stopped containers we can delete the one we are not going to use. If we want to delete all the stopped containers we can use prune command.
$ sudo docker container prune
jhony@ljlinux:~$ sudo docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue?[y/N] y
Deleted Containers:
c9baa86f6b0a287e44fc3ca1363c0b4796d542229671a4720df11b4c16448f2d
Total reclaimed space: 28B
Deleting a docker image:
The command rmi used to delete a docker image. To delete a image the below things should be checked.
The image should not be used by any running container.
Image can be deleted only by its ID and not by its name.
jhony@ljlinux:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 11 days ago 1.22MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
Comments