In this post, we will be looking at the practical use of docker. In case if you have not visited by previous two post about docker, you can check these two post here Docker-Part-1 and Docker-Part-2
1.Docker installation: The installation of docker on every OS different, here we will be looking at installation of docker on Ubuntu 22.04 version. You can have a look on the following link to install docker on different OS.
Set up Docker's apt repository.
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Install the latest Docker packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify that the Docker Engine installation is successful by running the hello-world image.
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:d211f485f2dd1dee407a80973c8f129f00d54604d2c90732e8e320e5038a0348
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
2.Use the below command to check installed docker version.
docker version
Client: Docker Engine - Community
Version: 27.2.1
API version: 1.47
Go version: go1.22.7
Git commit: 9e34c9b
Built: Fri Sep 6 12:08:10 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 27.2.1
API version: 1.47 (minimum version 1.24)
Go version: go1.22.7
Git commit: 8b539b8
Built: Fri Sep 6 12:08:10 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.7.22
GitCommit: 7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
runc:
Version: 1.1.14
GitCommit: v1.1.14-0-g2c9f560
docker-init:
Version: 0.19.0
GitCommit: de40ad0
3.If you want to pull any public image from docker public registry, you can use the below command to pull a public docker image. Here i am pulling latest image of nginx docker.
root@ubuntuserver:/home/superuser# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
302e3ee49805: Pull complete
d07412f52e9d: Pull complete
9ab66c386e9c: Pull complete
4b563e5e980a: Pull complete
55af3c8febf2: Pull complete
5b8e768fb22d: Pull complete
85177e2c6f39: Pull complete
Digest: sha256:d2eb56950b84efe34f966a2b92efb1a1a2ea53e7e93b94cdf45a27cf3cd47fc0
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@ubuntuserver:/home/superuser#
4.Now you can launch the container using nginx docker image using below command, I will running the container in in detach mode using -d command and port no 80.
root@ubuntuserver:/home/superuser# docker run -d --name mynginx -p 8080:80 nginx
383cb1b644d31370a7e009fa6117d4c7117bc500290869c12f7ae444c2527528
The above command launched a nginx container name mynginx in detach mode. The -d parameter is use to run container in detach mode. -p is for port mapping. By default nginx listen on port 80 so we mapped port 8080 of host to nginx container 80. You can access the nginx web server using your host ip address and port 8080 in your browser like below.
5. You can use the following command to to stop a running container.
docker stop container-id/container-name
root@ubuntuserver:/home/superuser# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
383cb1b644d3 nginx "/docker-entrypoint.…" 6 minutes ago Up 5 minutes 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp mynginx
root@ubuntuserver:/home/superuser# docker stop mynginx
mynginx
root@ubuntuserver:/home/superuser# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6.You can remove the a container completely using below command. You can see the container is showing in exited mode by using docker ps -a
root@ubuntuserver:/home/superuser# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
383cb1b644d3 nginx "/docker-entrypoint.…" 7 minutes ago Exited (0) About a minute ago mynginx
root@ubuntuserver:/home/superuser#
To remove docker use following command.
Docker rm containerid/cotainername
root@ubuntuserver:/home/superuser# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
383cb1b644d3 nginx "/docker-entrypoint.…" 7 minutes ago Exited (0) About a minute ago mynginx
root@ubuntuserver:/home/superuser# docker rm 383cb1b644d3
383cb1b644d3
root@ubuntuserver:/home/superuser# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntuserver:/home/superuser#
7. You can take the terminal access of a container using following commands.
docker exec -it container-id/container-name
root@ubuntuserver:/home/superuser# docker exec -it mynginx bash
root@43a739212098:/#
8. Use below command to see all docker images.
root@ubuntuserver:/home/superuser# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7f553e8bbc89 12 days ago 192MB
9.Use the below command to check the container logs.
docker logs -f container-id/container name
root@ubuntuserver:/home/superuser# docker logs -f mynginx
/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: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/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
2024/10/15 14:58:02 [notice] 1#1: using the "epoll" event method
2024/10/15 14:58:02 [notice] 1#1: nginx/1.27.2
2024/10/15 14:58:02 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/10/15 14:58:02 [notice] 1#1: OS: Linux 5.15.0-122-generic
2024/10/15 14:58:02 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/10/15 14:58:02 [notice] 1#1: start worker processes
2024/10/15 14:58:02 [notice] 1#1: start worker process 29
2024/10/15 14:58:02 [notice] 1#1: start worker process 30
10.you can use the following command to remove docker image.
docker rmi images-name
These are day to day command that you can use while working with docker using docker CLI. We will be looking into docker file in more details on our next post. Kindly let me know if you have any question or suggestion in comment section. Please hit the subscribe button and stay tune for more technical article.