Thursday, 5 July 2018

001 - Getting started with docker

Following is based on "Getting Started with Kubernetes" course provided by Sander van Vugt



Introuction:



having fresh Fedora 27 to be acted as "Containers Host":

[ab@fedora28 ~]$ hostname
fedora28.ab.lab
[root@fedora28 ~]# cat /etc/redhat-release 
Fedora release 27 (Twenty Seven)
[root@fedora28 ~]# ip a show ens3
2: ens3:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:c0:37:c2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.39/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fec0:37c2/64 scope link 
       valid_lft forever preferred_lft forever


Remove any docker RPMs provided by Fedora:
[root@fedora28 ~]# dnf remove docker docker-common container-selinux docker-selinux docker-engine
No match for argument: docker
No match for argument: docker-common
No match for argument: container-selinux
No match for argument: docker-selinux
No match for argument: docker-engine
Error: No packages marked for removal.
[root@fedora28 ~]# rpm -qa | egrep -i 'docker|container'


Install Docker Repos:

[root@fedora28 ~]# dnf repolist
Last metadata expiration check: 0:22:23 ago on Thu 05 Jul 2018 01:34:10 PM CEST.
repo id                                                            repo name                                                                               status
*fedora                                                            Fedora 27 - x86_64                                                                      54,801
*updates                                                           Fedora 27 - x86_64 - Updates                                                            15,360
[root@fedora28 ~]# curl -o /etc/yum.repos.d/docker-ce.repo  https://download.docker.com/linux/fedora/docker-ce.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2544  100  2544    0     0   2544      0  0:00:01 --:--:--  0:00:01 10779
[root@fedora28 ~]# dnf repolist
Last metadata expiration check: 0:00:31 ago on Thu 05 Jul 2018 01:56:51 PM CEST.
repo id                                                                repo name                                                                           status
docker-ce-stable                                                       Docker CE Stable - x86_64                                                                8
*fedora                                                                Fedora 27 - x86_64                                                                  54,801
*updates                                                               Fedora 27 - x86_64 - Updates                                                        15,360


Install Docker Community Edition 'docker-ce' to your machine:

[root@fedora28 ~]# dnf install docker-ce
[root@fedora28 ~]# rpm -qa | grep docker
docker-ce-18.03.1.ce-1.fc27.x86_64


Start and enable docker engine:

[root@fedora28 ~]# systemctl enable docker --now
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.


Run 'docker' command as non-root-user

[root@fedora28 ~]# grep docker /etc/group
docker:x:989:

[root@fedora28 ~]# usermod -aG docker ab

[root@fedora28 ~]# grep docker /etc/group
docker:x:989:ab


Verify the docker engine installation

[ab@fedora28 ~]$ hostname
fedora28.ab.lab

[ab@fedora28 ~]$ id
uid=1000(ab) gid=1000(ab) groups=1000(ab),10(wheel),989(docker) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[ab@fedora28 ~]$ docker --help | grep info
  info        Display system-wide information
[ab@fedora28 ~]$ docker info

[ab@fedora28 ~]$ docker help version

Usage: docker version [OPTIONS]

Show the Docker version information

Options:
  -f, --format string   Format the output using the given Go template
[ab@fedora28 ~]$ docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:19:31 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:21 2018
  OS/Arch:      linux/amd64
  Experimental: false
[ab@fedora28 ~]$ docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.03.1-ce
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.17.3-100.fc27.x86_64
Operating System: Fedora 27 (Server Edition)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.852GiB
Name: fedora28.ab.lab
ID: BT7F:YVLB:TDYO:FBWX:RPMK:62H7:EWRQ:GSYB:XZMX:3XM2:ISKU:XED6
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false


Getting help with any docker sub-command:

[ab@fedora28 ~]$ docker help info

Usage: docker info [OPTIONS]

Display system-wide information

Options:
  -f, --format string   Format the output using the given Go template


Search for official Fedora image:

[ab@fedora28 ~]$ docker help search 

Usage: docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
[ab@fedora28 ~]$ docker search fedora
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
fedora                               Official Docker builds of Fedora                669                 [OK]                
[...]


Pull the official Fedora image from docker hub (docker registry):

[ab@fedora28 ~]$ docker help pull

Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
[ab@fedora28 ~]$ docker pull fedora
Using default tag: latest
latest: Pulling from library/fedora
e71c36a80ba9: Pull complete 
Digest: sha256:7ae08e5637170eb47c01e315b6e64e0d48c6200d2942c695d0bee61b38c65b39
Status: Downloaded newer image for fedora:latest


List the current download docker images:

[ab@fedora28 ~]$ docker help image

Usage: docker image COMMAND

Manage images

Options:


Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
[ab@fedora28 ~]$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
fedora              latest              cc510acfcd70        2 months ago        253MB


Show how many docker containers are running now:

[ab@fedora28 ~]$ docker help ps

Usage: docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


Show How many containers you have even the offline ones:

[ab@fedora28 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


Execute "echo hello world" in the new container 'Fedora'. Container will be stopped after that as nothing to do:

[ab@fedora28 ~]$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
fedora              latest              cc510acfcd70        2 months ago        253MB
[ab@fedora28 ~]$ docker help run

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container
[..]
[ab@fedora28 ~]$ docker run fedora echo hello world
hello world
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[ab@fedora28 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
901f42ca4f10        fedora              "echo hello world"   24 seconds ago      Exited (0) 23 seconds ago                       eloquent_elbakyan     --> Note "CONTAINER ID"


Run command in Container and attach to its console:


Attach to container console:
[ab@fedora28 ~]$ docker help run | grep -E -e "[[:space:]]-i" -e "[[:space:]]-t" -e "--name"
  -i, --interactive                    Keep STDIN open even if not attached
      --name string                    Assign a name to the container
  -t, --tty                            Allocate a pseudo-TTY
[ab@fedora28 ~]$ docker run -it fedora /bin/bash

[root@3ed4e70d4c73 /]# uname -n    
3ed4e70d4c73

Check from another window if container still up and running
[ab@fedora28 ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3ed4e70d4c73        fedora              "/bin/bash"         4 minutes ago       Up 4 minutes                            competent_villani

De-attach from the above container
[root@3ed4e70d4c73 /]#  

[ab@fedora28 ~]$ hostname
fedora28.ab.lab

[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3ed4e70d4c73        fedora              "/bin/bash"         9 minutes ago       Up 9 minutes                            competent_villani


Attach to current working container:

[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3ed4e70d4c73        fedora              "/bin/bash"         13 minutes ago      Up 13 minutes                           competent_villani
[ab@fedora28 ~]$ docker help attach

Usage: docker attach [OPTIONS] CONTAINER

Attach local standard input, output, and error streams to a running container

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)
[ab@fedora28 ~]$ docker attach 3ed4e70d4c73
[root@3ed4e70d4c73 /]# uname -n
3ed4e70d4c73


Stop current working container:

Way #1 - from the console
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3ed4e70d4c73        fedora              "/bin/bash"         17 minutes ago      Up 17 minutes                           competent_villani

[ab@fedora28 ~]$ docker attach 3ed4e70d4c73


[root@3ed4e70d4c73 /]# uname -n
3ed4e70d4c73

[root@3ed4e70d4c73 /]# exit
exit
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Way #2 - from the containers host
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3ed4e70d4c73        fedora              "/bin/bash"         19 minutes ago      Up 5 seconds                            competent_villani

[ab@fedora28 ~]$ docker help stop

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)
[ab@fedora28 ~]$ docker stop 3ed4e70d4c73
3ed4e70d4c73

[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


Execute a command inside current working container without attaching to the console:

[ab@fedora28 ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3ed4e70d4c73        fedora              "/bin/bash"         22 minutes ago      Up 16 seconds                           competent_villani
[ab@fedora28 ~]$ docker help exec

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: [:])
  -w, --workdir string       Working directory inside the container
[ab@fedora28 ~]$ docker exec 3ed4e70d4c73 uname -a 
Linux 3ed4e70d4c73 4.17.3-100.fc27.x86_64 #1 SMP Tue Jun 26 14:19:03 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux



No comments:

Post a Comment