Working with Docker Images

by XDK 25. August 2019 13:42

Command to run a busybox docker image

ubuntu@ip-172-31-59-138:~$ docker run --rm busybox:latest /bin/echo "Hello world"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
ee153a04d683: Pull complete
Digest: sha256:9f1003c480699be56815db0f8146ad2e22efea85129b5b5983d0e0fb52d9ab70
Status: Downloaded newer image for busybox:latest
Hello world

Note:
clean up (--rm) By default a container’s file system persists even after the container exits. This makes debugging a lot easier (since you can inspect the final state) and you retain all your data by default. But if you are running short-term foreground processes, these container file systems can really pile up. If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the --rm flag:

Command to run a tomcat:jre8 docker image and change the container to listen to port 80 instead of tomcat's default port 8080 at runtime.

ubuntu@ip-172-31-59-138:~$ docker run -d -p 80:8080 tomcat:jre8
Unable to find image 'tomcat:jre8' locally
jre8: Pulling from library/tomcat
c5e155d5a1d1: Pull complete
221d80d00ae9: Pull complete
4250b3117dca: Pull complete
d1370422ab93: Pull complete
deb6b03222ca: Pull complete
9cdea8d70cc3: Pull complete
968505be14db: Pull complete
04b5c270ac81: Pull complete
301d76fcab1f: Pull complete
57ca7a0b9e79: Pull complete
3c1d6826d7a3: Pull complete
Digest: sha256:7cdf9dca1472da80e7384403c57b0632753a3a5cdf4f310fc39462e08af8ef39
Status: Downloaded newer image for tomcat:jre8
59cb47cef38dd64376188c66e6dbe7288840e6e68703548c12d1cdf7018f7f9d

Note: In the above command, -d will detach our terminal, -P will publish all exposed ports to random ports

 

Command to verify the java version in the container.

ubuntu@ip-172-31-59-138:~$ docker exec -it 59cb47cef38d java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

Note: -i Keep STDIN open even if not attached.-t Allocate a pseudo-TTY (Text Only Console).


Command to stop the container

ubuntu@ip-172-31-59-138:~$ docker stop 59cb47cef38d
59cb47cef38d
ubuntu@ip-172-31-59-138:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Command to remove docker container

ubuntu@ip-172-31-59-138:~$ docker rm 59cb47cef38d
59cb47cef38d
ubuntu@ip-172-31-59-138:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Command to list the docker images

ubuntu@ip-172-31-59-138:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest db8ee88ad75f 5 weeks ago 1.22MB
tomcat jre8 3639174793ba 3 months ago 463MB

Command to remove docker image

ubuntu@ip-172-31-59-138:~$ docker rmi 3639174793ba
Untagged: tomcat:jre8
Untagged: tomcat@sha256:7cdf9dca1472da80e7384403c57b0632753a3a5cdf4f310fc39462 e08af8ef39
Deleted: sha256:3639174793bafbf0f22aeaf6096842f563de3154fe7c756d04966eca1fe4e5 5d
Deleted: sha256:ad683ea82dd8cc2ea298e08626031a01b3bf94a35714572774405f04daa0fe a8
Deleted: sha256:c43fe4301f09c132eefa0707afd2995ade93975a3a7cd11c7ba61b616b5c59 e2
Deleted: sha256:4abee72d794e2aedaa1fd461440964ec8306780c9e22b7b363e953730613e1 45
Deleted: sha256:5f01074ca9f442239d769dffd326dc5004d8bda0365a4767fff8af8e5f58ee 41
Deleted: sha256:fc82c5f00299a2e87402e3090f53e161801b9c6aa6c86121c36e7bb277c68c d0
Deleted: sha256:067619b0ac688892f0e9f086135168006bdc58bc40d902c570772250ea264b 3c
Deleted: sha256:6b6450559d31f9c4e78c7ca449410807188a4d5f88fecb4db7c0f5efbbd9bc 2c
Deleted: sha256:fa14a85b824e4d22bebf8a3787f80f7b13a87e63636efc630416350d3cb09c 8d
Deleted: sha256:a9a9c8853295275070975beba78ec0f573172e6e41b30232a00d8af0d49b8e f3
Deleted: sha256:ddf0293e8e23246803d265b158ffbb9453d925fe392b43515984815853e912 1b
Deleted: sha256:f94641f1fe1f5c42c325652bf55f0513c881c86b620b912b15460e0bca07cc 12

Verify!

ubuntu@ip-172-31-59-138:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest db8ee88ad75f 5 weeks ago 1.22MB

Tags:

Amazon Web Service | Azure Native DevOps | Docker | Microsoft Azure Services

Docker Command

by XDK 25. August 2019 13:39

ubuntu@ip-172-31-59-138:~$ docker

Management Commands:

builder Manage builds
config Manage Docker configs
container Manage containers
engine Manage the docker engine
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

Tags:

Amazon Web Service | Azure Native DevOps | Docker | Microsoft Azure Services

How to install Docker on Ubuntu 18.04?

by XDK 25. August 2019 13:28

Explanation:

Update the existing list of packages

ubuntu@ip-172-31-59-138:~$ sudo apt update

Install Docker

************************************************************************************************

Note: execute the following commands to install docker-ce [Docker Community Edition (CE)] or docker-ee [Docker Enterprise Edition (EE)]

#Install packages to allow apt to use a repository over https
sudo apt install apt-transport-https ca-certificates curl software-properties-common
#Add docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Command to setup the stable repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update

#Docker Community Edition (CE)
apt-cache policy docker-ce
sudo apt install docker.ce
#Docker Enterprise Edition (EE)
apt-cache policy docker-ee
sudo apt install docker.ee

*************************************************************************************************

ubuntu@ip-172-31-59-138:~$ sudo apt install docker.io

Verify Docker installation status

ubuntu@ip-172-31-59-138:~$ sudo systemctl status docker

docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2019-08-24 03:13:13 UTC; 8min ago
Docs: https://docs.docker.com
Main PID: 15011 (dockerd)
Tasks: 8
CGroup: /system.slice/docker.service
└─15011 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.484355658Z" level=warning msg="Your kernel does not support cgroup rt period"
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.484498272Z" level=warning msg="Your kernel does not support cgroup rt runtime"
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.489353437Z" level=info msg="Loading containers: start."
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.663568387Z" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.754217392Z" level=info msg="Loading containers: done."
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.758501298Z" level=warning msg="failed to retrieve runc version: unknown output format: runc version spec: 1.0.1-dev\n"
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.806114376Z" level=info msg="Docker daemon" commit=2d0083d graphdriver(s)=overlay2 version=18.09.7
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.806534853Z" level=info msg="Daemon has completed initialization"
Aug 24 03:13:13 ip-172-31-59-138 systemd[1]: Started Docker Application Container Engine.
Aug 24 03:13:13 ip-172-31-59-138 dockerd[15011]: time="2019-08-24T03:13:13.877186101Z" level=info msg="API listen on /var/run/docker.sock"

Add username to the docker group, to avoid using sudo to execute the docker command.

To add user (ubuntu) to any one of the supplementary group (docker), use the options '-a' and '-G'.

ubuntu@ip-172-31-59-138:~$ groups
ubuntu adm dialout cdrom floppy sudo audio dip video plugdev lxd netdev

ubuntu@ip-172-31-59-138:~$ sudo usermod -aG docker ubuntu

ubuntu@ip-172-31-59-138:~$ groups
ubuntu adm dialout cdrom floppy sudo audio dip video plugdev lxd netdev

Restart the SSH to see the changes.

ubuntu@ip-172-31-59-138:~$ groups
ubuntu adm dialout cdrom floppy sudo audio dip video plugdev lxd netdev docker

Run Docker command without sudo

ubuntu@ip-172-31-59-138:~$ docker version
Client:
Version: 18.09.7
API version: 1.39
Go version: go1.10.1
Git commit: 2d0083d
Built: Fri Aug 16 14:20:06 2019
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.09.7
API version: 1.39 (minimum version 1.12)
Go version: go1.10.1
Git commit: 2d0083d
Built: Wed Aug 14 19:41:23 2019
OS/Arch: linux/amd64
Experimental: false

ubuntu@ip-172-31-59-138:~$ docker info

Tags:

Amazon Web Service | Azure Native DevOps | Docker | Microsoft Azure Services

About the author

My name is Xavier Dilip Kumar Jayaraj having 16+ years of IT experience which includes solid experience and depth Knowledge in Application Life Cycle Management, Configuration Management, Implementation and Support using TFS on-premises and Azure DevOps. I have invested in gaining DevOps knowledege to expertise with Cloud Computing providers namely Microsoft Azure and Amazon Web Services in recent years. I am very positive to learn and adapt emerging technologies to client’s environment.

Microsoft Certified: Azure Administrator Associate

Microsoft Certified: Azure DevOps Engineer Expert

DevOps Engineer Certificate Program | Transcript 

OTP-AWSD5: AWS IoT: Developing and Deploying an Internet of Things

[PCEP-30-01] PCEP – Certified Entry-Level Python Programmer

Quotes I Like

"Failure will never overtake me if my determination to succeed is strong enough."  - Dr. APJ. Abdul Kalam

"Always be yourself, express yourself, have faith in yourself, do not go out and look for a successful personality and duplicate it." - Bruce Lee

"Technology is just a tool. In terms of getting the kids working together and motivating them, the teacher is the most important." - Bill Gates

"Innovation distinguishes between a leader and a follower." - Steve Jobs

CategoryList

Disclaimer

The information provided here is based on my expreriences, troubleshooting and online/offline findings. It can be used as is on your own risk without any warranties and I impose no rights.