Running the first docker container with image :
hello-world. This is nothing but just to check your docker setup.
-bash-4.2$
sudo docker run hello-world
Unable to
find image 'hello-world:latest' locally
latest:
Pulling from library/hello-world
1b930d010525:
Pull complete
Digest:
sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
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/
-bash-4.2$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 11 months ago 1.84kB
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Once your setup gets completed, please add the newly
created docker user(in this case it is dockmgr) to docker group.
-bash-4.2$
sudo usermod -aG docker dockmgr
-bash-4.2$
id dockmgr
uid=1000(dockmgr)
gid=1000(manager) groups=1000(manager),10(wheel),994(docker)
-bash-4.2$
docker run comamnd will check locally for the
mentioned image if available it will run a new container upon it, else it will
pull the image from docker hub. In this example, We are running a container on
ubuntu image and requested docker to provide bash shell.
-bash-4.2$
sudo docker run -it ubuntu /bin/bash
[sudo]
password for dockmgr:
Unable to
find image 'ubuntu:latest' locally
latest:
Pulling from library/ubuntu
7ddbc47eeb70:
Pull complete
c1bbdc448b72:
Pull complete
8c3b70e39044:
Pull complete
45d437916d57:
Pull complete
Digest:
sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status:
Downloaded newer image for ubuntu:latest
root@19f979a29591:/#
ls -lrt
total 4
drwxr-xr-x. 8 root root
96 May 23 2017 lib
drwxr-xr-x. 2 root root 6 Apr 24
2018 home
drwxr-xr-x. 2 root root 6 Apr 24
2018 boot
drwxr-xr-x. 1 root root
18 Oct 29 21:25 usr
drwxr-xr-x. 2 root root 6 Oct 29 21:25 srv
drwxr-xr-x. 2 root root 6 Oct 29 21:25 opt
drwxr-xr-x. 2 root root 6 Oct 29 21:25 mnt
drwxr-xr-x. 2 root root 6 Oct 29 21:25 media
drwxr-xr-x. 2 root root
34 Oct 29 21:25 lib64
drwxr-xr-x. 1 root root
17 Oct 29 21:25 var
drwx------. 2 root root
37 Oct 29 21:25 root
drwxr-xr-x. 2 root root 4096 Oct 29 21:25 bin
drwxrwxrwt. 2 root root 6 Oct 29 21:25 tmp
drwxr-xr-x. 1 root root
21 Oct 31 22:20 sbin
drwxr-xr-x. 1 root root
21 Oct 31 22:20 run
dr-xr-xr-x. 13 root root 0 Nov 29 03:30 sys
drwxr-xr-x. 1 root root
66 Nov 29 05:02 etc
dr-xr-xr-x.
122 root root 0 Nov 29 05:02 proc
drwxr-xr-x. 5 root root
360 Nov 29 05:02 dev
To find the docker images available/downloaded
locally:
-bash-4.2$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 4 weeks ago 64.2MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
Below command will create a container of ubuntu image
as daemon and will print "Hello World" every second.
-bash-4.2$
sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello World;
sleep 1; done"
ad7625e2a5cb886bc40b9e784d0848c2a13c305c27217525405aa6aad06f1959
-bash-4.2$
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad7625e2a5cb ubuntu "/bin/sh -c 'while
t…" 9 seconds ago Up 8 seconds objective_diffie
When we create a container a fancy name got allocated
to it by docker. Here it is "objective_diffie". If we need to check
the logs of the above container running as daemon, we can run:
-bash-4.2$
sudo docker logs objective_diffie
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
To stop a container:
-bash-4.2$
sudo docker stop objective_diffie
objective_diffie
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-bash-4.2$
Below command is reponsible to download the
"training/webapp" image and execute the python code
"app.py". As it is a web app, we need to do host-to-container port
mapping. So, that we can hit the container port TCP 5000 through host machine
web browser at port TCP 80.
-bash-4.2$
sudo docker run -d -p 80:5000 training/webapp python app.py
Unable to
find image 'training/webapp:latest' locally
latest:
Pulling from training/webapp
Image
docker.io/training/webapp:latest uses outdated schema1 manifest format. Please
upgrade to a schema2 image for better future compatibility. More information at
https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8:
Pull complete
909cd34c6fd7:
Pull complete
0b9bfabab7c1:
Pull complete
a3ed95caeb02:
Pull complete
10bbbc0fc0ff:
Pull complete
fca59b508e9f:
Pull complete
e7ae2541b15b:
Pull complete
9dd97ef58ce9:
Pull complete
a4c1b0cb7af7:
Pull complete
Digest:
sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status:
Downloaded newer image for training/webapp:latest
d78aba119faea05d5feecf2b9a047a36f442c361c832e0311f7770cf7c7bbcf4
-bash-4.2$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 4 weeks ago 64.2MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d78aba119fae training/webapp "python app.py" 27 seconds ago Up 25 seconds 0.0.0.0:80->5000/tcp inspiring_thompson
To view the webpage:
-bash-4.2$
curl http://0.0.0.0:80/
Hello
world!-bash-4.2$
From base
machine:
http://192.168.29.10:80
To view the container-to-host mapped ports:
-bash-4.2$
sudo docker port d78aba119fae
[sudo]
password for dockmgr:
5000/tcp
-> 0.0.0.0:80
-bash-4.2$
id
uid=1000(dockmgr)
gid=1000(manager) groups=1000(manager),10(wheel)
context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
-bash-4.2$
sudo docker port d78aba119fae 5000
0.0.0.0:80
-bash-4.2$
sudo docker port inspiring_thompson 5000
0.0.0.0:80
-bash-4.2$
-bash-4.2$
sudo docker stop inspiring_thompson
inspiring_thompson
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-bash-4.2$
-bash-4.2$
sudo docker run ubuntu /bin/echo "Hello World"
Hello
World
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-bash-4.2$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 4 weeks ago 64.2MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
--name keyword is used to name a container without
using the docker defined names:
-bash-4.2$
sudo docker run --name Shantanu ubuntu /bin/echo "Hello World"
Hello
World
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
To view the container attributes, docker inspect
command has been used. you can get the information about driver, network, etc.
-bash-4.2$
sudo docker inspect Shantanu
[
{
"Id":
"3dd11a45ea66efd0e31b7b6495f365b71847262e4b4aea393d8831ff0d88aa91",
"Created":
"2019-11-29T05:33:04.274948048Z",
"Path":
"/bin/echo",
"Args": [
"Hello World"
],
"State": {
"Status":
"exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt":
"2019-11-29T05:33:04.903288236Z",
"FinishedAt":
"2019-11-29T05:33:04.845752218Z"
},
"Image":
"sha256:775349758637aff77bf85e2ff0597e86e3e859183ef0baba8b3e8fc8d3cba51c",
"ResolvConfPath":
"/var/lib/docker/containers/3dd11a45ea66efd0e31b7b6495f365b71847262e4b4aea393d8831ff0d88aa91/resolv.conf",
"HostnamePath":
"/var/lib/docker/containers/3dd11a45ea66efd0e31b7b6495f365b71847262e4b4aea393d8831ff0d88aa91/hostname",
"HostsPath":
"/var/lib/docker/containers/3dd11a45ea66efd0e31b7b6495f365b71847262e4b4aea393d8831ff0d88aa91/hosts",
"LogPath":
"/var/lib/docker/containers/3dd11a45ea66efd0e31b7b6495f365b71847262e4b4aea393d8831ff0d88aa91/3dd11a45ea66efd0e31b7b6495f365b71847262e4b4aea393d8831ff0d88aa91-json.log",
"Name":
"/Shantanu",
"RestartCount": 0,
"Driver":
"overlay2",
"Platform":
"linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile":
"",
"ExecIDs": null,
.
.
.
.
"Networks": {
"bridge": {
"IPAMConfig":
null,
"Links": null,
"Aliases": null,
"NetworkID":
"967757ddd2efc9649725da1708c4c24631d5dc2e9486fcebded5b392d14d3b77",
"EndpointID":
"",
"Gateway":
"",
"IPAddress":
"",
"IPPrefixLen": 0,
"IPv6Gateway":
"",
"GlobalIPv6Address":
"",
"GlobalIPv6PrefixLen": 0,
"MacAddress":
"",
"DriverOpts":
null
}
}
}
}
]
docker pull command is used to pull a image from
docker hub to your local machine without starting/creating a container:
-bash-4.2$
sudo docker pull centos
Using
default tag: latest
latest:
Pulling from library/centos
729ec3a6ada3:
Pull complete
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status:
Downloaded newer image for centos:latest
docker.io/library/centos:latest
-bash-4.2$
sudo docker ps
CONTAINER
ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-bash-4.2$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 775349758637 4 weeks ago 64.2MB
centos latest 0f3e07c0138f 8 weeks ago 220MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
-bash-4.2$
The same image can be used to run a container. Here,
the centos image which was pulled in the previous step has been used to run a
container with /bin/bash shell.
-bash-4.2$
sudo docker run -it centos:latest /bin/bash
[root@2b3c4f44d979
/]# ls -lrt
total 0
drwxr-xr-x. 2 root root
6 May 11 2019 srv
lrwxrwxrwx. 1 root root
8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root
6 May 11 2019 opt
drwxr-xr-x. 2 root root
6 May 11 2019 mnt
drwxr-xr-x. 2 root root
6 May 11 2019 media
lrwxrwxrwx. 1 root root
9 May 11 2019 lib64 ->
usr/lib64
lrwxrwxrwx. 1 root root
7 May 11 2019 lib -> usr/lib
drwxr-xr-x. 2 root root
6 May 11 2019 home
lrwxrwxrwx. 1 root root
7 May 11 2019 bin -> usr/bin
drwx------. 2 root root
6 Sep 27 17:13 lost+found
drwxr-xr-x. 12 root root 144 Sep 27 17:13 usr
drwxr-xr-x. 20 root root 262 Sep 27 17:13 var
drwxrwxrwt. 7 root root 145 Sep 27 17:13 tmp
drwxr-xr-x. 11 root root 163 Sep 27 17:13 run
dr-xr-x---. 2 root root 162 Sep 27 17:13 root
dr-xr-xr-x. 13 root root
0 Nov 29 03:30 sys
drwxr-xr-x. 1 root root
66 Nov 29 05:38 etc
dr-xr-xr-x.
122 root root 0 Nov 29 05:38 proc
drwxr-xr-x. 5 root root 360 Nov 29 05:38 dev
[root@2b3c4f44d979
/]# hostname
2b3c4f44d979
[root@2b3c4f44d979
/]# uptime
05:39:02 up
2:08, 0 users, load average: 0.56, 0.22, 0.12
[root@2b3c4f44d979
/]# exit
exit
docker search command is used to search for an image
locally & in docker hub.
-bash-4.2$
sudo docker search sinatra
NAME
DESCRIPTION STARS OFFICIAL AUTOMATED
training/sinatra
18
tongueroo/sinatra
1
mcansky/sinatra_hello A simple, hello worldly
sinatra app in a con… 0
gruntwork/sinatra-backend
0
pranay/sinatra FROM ubuntu:14.04 RUN apt-get
update && apt-… 0
marceldegraaf/sinatra Sinatra test app 0
convox/sinatra Convox base image for Sinatra 0 [OK]
franklinyu/sinatra-hello This is a hello-world server
built with Sina… 0 [OK]
thoom/sinatra-mock Used to build a Sinatra-backed
mock web serv… 0 [OK]
capttofu/sinatra-with-logstash
0
glazziq/sinatra-ok A simple Sinatra application
that will respo… 0
gitresolve/sinatra Simple sinatra app for
testing 0
arshiagg/sinatra Sinatra web server running a ruby
exporter 0
pmblanco/sinatra
0
torico/sinatra-async port 3001. for AWS 0 [OK]
amireh/sinatra Ruby Sinatra with
live-reload for developmen… 0 [OK]
eventingmonkey/sinatra 0
dancodes/sinatra-heroku-docker An image used to speed up CI builds of
Sinat… 0 [OK]
esteveavi/sinatra Sinatra example 0
gwjjeff/sinatra
0
[OK]
zoomix/sinatra-galleria
0
[OK]
carlad/sinatra
0
welldoer/sinatra Sinatra webapp 0 [OK]
nilsc/sinatra
0
marceldegraaf/sinatra-with-logstash Sinatra test app with logstash-forwarder to
… 0
-bash-4.2$
To login to your Docker account and to push and pull
images from Docker Hub use your Docker ID.
-bash-4.2$
sudo docker login
Login with
your Docker ID to push and pull images from Docker Hub. If you don't have a
Docker ID, head over to https://hub.docker.com to create one.
Username:
shaan2212
Password:
WARNING!
Your password will be stored unencrypted in /root/.docker/config.json.
Configure
a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login
Succeeded
Your password will be stored unencrypted in
/root/.docker/config.json
-bash-4.2$
sudo cat /root/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth":
"c2hhYW4yMjEyOlNoYWFuQDIyMTI="
}
},
"HttpHeaders": {
"User-Agent":
"Docker-Client/19.03.5 (linux)"
}
}-bash-4.2$
Pull an image, run a container using it, modify and
push it to dockerhub, so that the updated image can be used directly later on.
It will save us the overhead to do the repeated things. Here in a centos image
we are adding webserver - httpd and binding it as new image while pushing it to
dockerhub.
-bash-4.2$
sudo docker run -ti centos /bin/bash
[root@f3bc073ecc85
/]# yum install httpd
Failed to
set locale, defaulting to C
CentOS-8 -
AppStream
256 kB/s | 6.3 MB 00:25
CentOS-8 -
Base
937 kB/s | 7.9 MB 00:08
CentOS-8 -
Extras
392 B/s | 2.1 kB 00:05
Dependencies
resolved.
==========================================================================================================================
Package Arch Version
Repository Size
==========================================================================================================================
Installing:
httpd x86_64
2.4.37-12.module_el8.0.0+185+5908b0db AppStream 1.7 M
Installing
dependencies:
apr x86_64 1.6.3-9.el8 AppStream 125 k
apr-util x86_64 1.6.1-6.el8 AppStream 105 k
centos-logos-httpd noarch 80.5-2.el8 AppStream 24 k
httpd-filesystem noarch
2.4.37-12.module_el8.0.0+185+5908b0db AppStream 35 k
httpd-tools x86_64
2.4.37-12.module_el8.0.0+185+5908b0db AppStream 102 k
mod_http2 x86_64
1.11.3-3.module_el8.0.0+185+5908b0db AppStream 158 k
brotli
x86_64 1.0.6-1.el8
BaseOS 323 k
mailcap noarch 2.1.48-3.el8 BaseOS 39 k
Installing
weak dependencies:
apr-util-bdb x86_64 1.6.1-6.el8 AppStream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 AppStream 27 k
Enabling
module streams:
httpd 2.4
Transaction
Summary
==========================================================================================================================
Install 11 Packages
Total
download size: 2.6 M
Installed
size: 7.6 M
Is this ok
[y/N]: y
Downloading
Packages:
CentOS-8 -
AppStream 195%
[===========================================================================(1/11):
apr-util-bdb-1.6.1-6.el8.x86_64.rpm 66 kB/s | 25 kB
00:00
(2/11):
apr-util-1.6.1-6.el8.x86_64.rpm
118 kB/s | 105 kB 00:00
(3/11):
apr-1.6.3-9.el8.x86_64.rpm
140 kB/s | 125 kB 00:00
(4/11):
apr-util-openssl-1.6.1-6.el8.x86_64.rpm 38
kB/s | 27 kB 00:00
(5/11):
centos-logos-httpd-80.5-2.el8.noarch.rpm 87
kB/s | 24 kB 00:00
(6/11):
httpd-filesystem-2.4.37-12.module_el8.0.0+185+5908b0db.noarch.rpm 88 kB/s | 35 kB
00:00
(7/11):
httpd-tools-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64.rpm 101 kB/s | 102 kB 00:01
(8/11):
mod_http2-1.11.3-3.module_el8.0.0+185+5908b0db.x86_64.rpm 121 kB/s | 158 kB 00:01
(9/11):
mailcap-2.1.48-3.el8.noarch.rpm
102 kB/s | 39 kB 00:00
(10/11):
brotli-1.0.6-1.el8.x86_64.rpm
271 kB/s | 323 kB 00:01
(11/11):
httpd-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64.rpm 132 kB/s | 1.7
MB 00:12
--------------------------------------------------------------------------------------------------------------------------
Total 155
kB/s | 2.6 MB 00:17
warning:
/var/cache/dnf/AppStream-02e86d1c976ab532/packages/apr-1.6.3-9.el8.x86_64.rpm:
Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 -
AppStream
1.1 MB/s | 1.6 kB 00:00
Importing
GPG key 0x8483C65D:
Userid
: "CentOS (CentOS Official Signing Key)
<security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882
05B5 55B3 8483 C65D
From
: /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Is this ok
[y/N]: y
Key
imported successfully
Running
transaction check
Transaction
check succeeded.
Running
transaction test
Transaction
test succeeded.
Running
transaction
Preparing :
1/1
Installing : apr-1.6.3-9.el8.x86_64
1/11
Running scriptlet:
apr-1.6.3-9.el8.x86_64
1/11
Installing : apr-util-bdb-1.6.1-6.el8.x86_64
2/11
Installing : apr-util-openssl-1.6.1-6.el8.x86_64
3/11
Installing : apr-util-1.6.1-6.el8.x86_64
4/11
Running scriptlet: apr-util-1.6.1-6.el8.x86_64
4/11
Installing :
httpd-tools-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64 5/11
Installing : mailcap-2.1.48-3.el8.noarch 6/11
Installing : brotli-1.0.6-1.el8.x86_64
7/11
Running scriptlet:
httpd-filesystem-2.4.37-12.module_el8.0.0+185+5908b0db.noarch 8/11
Installing :
httpd-filesystem-2.4.37-12.module_el8.0.0+185+5908b0db.noarch 8/11
Installing :
centos-logos-httpd-80.5-2.el8.noarch
9/11
Installing :
mod_http2-1.11.3-3.module_el8.0.0+185+5908b0db.x86_64 10/11
Installing :
httpd-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64
11/11
Running scriptlet:
httpd-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64
11/11
Verifying : apr-1.6.3-9.el8.x86_64
1/11
Verifying : apr-util-1.6.1-6.el8.x86_64
2/11
Verifying : apr-util-bdb-1.6.1-6.el8.x86_64
3/11
Verifying :
apr-util-openssl-1.6.1-6.el8.x86_64
4/11
Verifying : centos-logos-httpd-80.5-2.el8.noarch
5/11
Verifying :
httpd-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64
6/11
Verifying :
httpd-filesystem-2.4.37-12.module_el8.0.0+185+5908b0db.noarch 7/11
Verifying :
httpd-tools-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64 8/11
Verifying :
mod_http2-1.11.3-3.module_el8.0.0+185+5908b0db.x86_64 9/11
Verifying : brotli-1.0.6-1.el8.x86_64
10/11
Verifying : mailcap-2.1.48-3.el8.noarch 11/11
Installed:
httpd-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64 apr-util-bdb-1.6.1-6.el8.x86_64
apr-util-openssl-1.6.1-6.el8.x86_64
apr-1.6.3-9.el8.x86_64
apr-util-1.6.1-6.el8.x86_64
centos-logos-httpd-80.5-2.el8.noarch
httpd-filesystem-2.4.37-12.module_el8.0.0+185+5908b0db.noarch
httpd-tools-2.4.37-12.module_el8.0.0+185+5908b0db.x86_64
mod_http2-1.11.3-3.module_el8.0.0+185+5908b0db.x86_64 brotli-1.0.6-1.el8.x86_64
mailcap-2.1.48-3.el8.noarch
Complete!
[root@f3bc073ecc85
/]#
No comments:
Post a Comment