# Docker Container Learning > [time=Fri, Mar 20, 2020 7:27 PM] ![](https://i.imgur.com/1UKv3Wy.png) ## What is Docker ? Docker is basically a container engine which uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system and automates application deployment on the container. Docker uses Copy-on-write union file system for its backend storage. ## What is container ? A Docker container is an open source software development platform. Its main benefit is to package applications in containers, allowing them to be portable to any system running a Linux or Windows operating system (OS). A Windows machine can run Linux containers by using a virtual machine (VM). ## How to Docker? Here are some basic things like how to install docker and how to we can pull images,build custom image, run containers,remove containers and see the logs, attach volume,inspect the config, etc... ## Install Docker on fedora 31 ###### Add Docker repository: `sudo dnf config-manager --add-repo=https://download.docker.com/linux/fedora/docker-ce.repo` ###### Install docker `sudo dnf install docker-ce` ###### Enable and Start Docker deamon. `sudo systemctl enable docker` `sudo systemctl start docker` ## Search for Docker images > Search images from registry ``` [snandago@sureshn ~]$ docker search http NAME DESCRIPTION STARS OFFICIAL AUTOMATED httpd The Apache HTTP Server Project 2913 [OK] haproxy HAProxy - The Reliable, High Performance TCP… 1383 [OK] steveltn/https-portal A fully automated HTTPS server powered by Ng… 88 [OK] kennethreitz/httpbin A simple HTTP service. 50 [OK] centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 31 hashicorp/http-echo http-echo is an in-memory web server that ec… 26 geldim/https-redirect Very small http to https redirector based on… 17 [OK] citizenstig/httpbin Docker container for httpbin: HTTP Request &… 14 [OK] clue/httpie HTTPie is a cURL-like tool for humans. Usefu… 13 [OK] alpine/httpie Auto-trigger docker build for `httpie` when … 10 [OK] mendhak/http-https-echo Listens on http/https, echoes details about … 8 [OK] mainflux/http HTTP adapter service for Mainflux IoT platfo… 3 ``` ## Pull docker image from registry > using `docker pull` command we can pull the images locally ``` [snandago@sureshn ~]$ docker pull httpd Using default tag: latest latest: Pulling from library/httpd 68ced04f60ab: Pull complete 35d35f1e0dc9: Pull complete 8a918bf0ae55: Pull complete d7b9f2dbc195: Pull complete d56c468bde81: Pull complete Digest: sha256:946c54069130dbf136903fe658fe7d113bd8db8004de31282e20b262a3e106fb Status: Downloaded newer image for httpd:latest docker.io/library/httpd:latest [snandago@sureshn ~]$ ``` ## List locally Downloaded Docker images > `docker images` command shows the locally builded/downloaed images. ``` [snandago@sureshn ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE myutil/httpd latest c8811902f987 57 minutes ago 381MB httpd latest c5a012f9cf45 3 weeks ago 165MB centos latest 470671670cac 2 months ago 237MB centos latest 49f7960eb7e4 21 months ago 200MB fedora/firefox latest 7fa9c2fade45 3 years ago 880MB [snandago@sureshn ~]$ ``` ## Remove the Docker images > Remove docker images from local cab be done by `docker rmi imagename` ``` [snandago@sureshn ~]$ docker rmi httpd Untagged: httpd:latest Untagged: httpd@sha256:946c54069130dbf136903fe658fe7d113bd8db8004de31282e20b262a3e106fb Deleted: sha256:c5a012f9cf45ce0634f5686cfb91009113199589bd39b683242952f82cf1cec1 Deleted: sha256:0f29a08770415263e178a4fd0114fe05e6dcc7d0c7922d5ee5430ad29dde9aef Deleted: sha256:7e07c23416eb19df1444ae11062dc553d9e8eb8fd91f866b2ad2aa22954597b9 Deleted: sha256:997f97a68088ee2a31925e6deefcc690d8b45f2d795a5ce540e4d540d838fca7 Deleted: sha256:c61f156b49aa9f766f67b79ee6d7df6e83a4a2a0bda8da0c5ff19b3ea480cbd3 Deleted: sha256:f2cb0ecef392f2a630fa1205b874ab2e2aedf96de04d0b8838e4e728e28142da [snandago@sureshn ~]$ ``` ## Run container > Run container by `docker run` commands with Arguments required. ``` [snandago@sureshn ~]$ docker run --name test_http -d -p 9090:80 httpd cc80d4595d4c566af398b0ca145f7e9f82661e1751e397b59bdca3fd4a2b499e [snandago@sureshn ~]$ ``` > Above `docker` command ran the test_http apcahe container with argument -d -p where -d make sure its run on backgroud and -p to expose port 80,we can verify it using curl localhost:9090 and Good news is it works! :+1: ``` [snandago@sureshn ~]$ curl 127.0.0.1:9090 <html><body><h1>It works!</h1></body></html> [snandago@sureshn ~]$ ``` ## Copy the file to running container > `docker cp ` command can be used to copy the file to running container. > this below example show command to copy the index.html to running container path /var/www/html ``` [snandago@sureshn my_utils]$ docker cp index.html test-myutil:/var/www/html ``` ## List Running & Stopped Containers > Running containers can be listed using `docker ps` > `docker ps` only so the running container we can use -a argument to see all the containers [running|stopped] ``` [snandago@sureshn ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cc80d4595d4c httpd "httpd-foreground" 6 minutes ago Up 6 minutes 0.0.0.0:9090->80/tcp test_http 2e8264b4050c myutil/httpd "/usr/sbin/httpd -D …" 27 minutes ago Up 27 minutes 0.0.0.0:8081->80/tcp test-myutil [snandago@sureshn ~]$ ``` ``` [snandago@sureshn ~]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1da62939c4a4 httpd "httpd-foreground" 15 seconds ago Exited (0) 8 seconds ago stopped 6664c9d1bcf7 httpd "httpd-foreground" About a minute ago Created test_http2 cc80d4595d4c httpd "httpd-foreground" 9 minutes ago Up 9 minutes 0.0.0.0:9090->80/tcp test_http 2e8264b4050c myutil/httpd "/usr/sbin/httpd -D …" 30 minutes ago Up 30 minutes 0.0.0.0:8081->80/tcp test-myutil [snandago@sureshn ~]$ ``` ## Kill the Running Containers > Kill the running container using `docker kill` command ``` [snandago@sureshn ~]$ docker kill test_http test_http [snandago@sureshn ~]$ ``` ## Remove the Containers > `docker rm` command can be used to remove container > If you would like to remove multiple containers at on shot just run `docker rm $(docker ps -a -q)` ``` [snandago@sureshn ~]$ docker rm test_http test_http [snandago@sureshn ~]$ ``` ## Run commands on Running Container > Using `docker exec` command we can Run a command in a running container ``` [snandago@sureshn ~]$ docker exec httpd_test ls /var/log alternatives.log apt btmp dpkg.log faillog lastlog wtmp [snandago@sureshn ~]$ docker exec httpd_test date Fri Mar 20 12:42:06 UTC 2020 [snandago@sureshn ~]$ [snandago@sureshn ~]$ docker exec httpd_test env PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=622b2f999cd2 HTTPD_PREFIX=/usr/local/apache2 HTTPD_VERSION=2.4.41 HTTPD_SHA256=133d48298fe5315ae9366a0ec66282fa4040efa5d566174481077ade7d18ea40 HTTPD_PATCHES= HOME=/root [snandago@sureshn ~]$ ``` ## ssh container > Using docker exec -it and /bin/bash conainer name we can ssh into container. ``` [snandago@sureshn search_index]$ docker exec -it grafana /bin/bash bash-5.0$ grafana-cli -v Grafana CLI version 6.7.1 bash-5.0$ ``` ## Add storage to the Container > by adding VOLUME in docker file we can attach the storage `docker -v path:containerpath` will attach the persistance storage to the container. ``` [snandago@sureshn docker]$ docker run --name con_volume_test -v /home/snandago/learning/docker:/var/www/html -d -p 9010:80 centos/httpd dd126bb53c2f5f427b92997a6a51181f638d0aa05bf0d76c6fd9a3d1a47911ba [snandago@sureshn docker]$ docker exec con_volume_test df Filesystem 1K-blocks Used Available Use% Mounted on overlay 103077688 52877616 44940812 55% / tmpfs 65536 0 65536 0% /dev shm 65536 0 65536 0% /dev/shm /dev/mapper/luks-66ddcfec-d738-46c9-ab1d-3954bf254999 103077688 52877616 44940812 55% /etc/hosts /dev/mapper/luks-841e521e-de10-4ae5-9e7d-d76dac326fa7 367219400 204250500 144292064 59% /var/www/html tmpfs 9943936 0 9943936 0% /proc/asound tmpfs 9943936 0 9943936 0% /proc/acpi tmpfs 9943936 0 9943936 0% /proc/scsi tmpfs 9943936 0 9943936 0% /sys/firmware [snandago@sureshn docker]$ docker exec con_volume_test ls -l /var/www/html total 4 -rw-rw-r--. 1 1000 1000 84 Mar 20 12:57 index.html [snandago@sureshn docker]$ curl 127.0.0.1:9010 <html> <body> <b> Test file volume attached to container. </b> </body> </html> [snandago@sureshn docker]$ ``` ## Inspect the Container configs > `docker inspect` command show provides detailed information on constructs controlled by Docker. ``` [snandago@sureshn ~]$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' test-myutil 172.17.0.2 [snandago@sureshn ~]$ [snandago@sureshn ~]$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' test-myutil 02:42:ac:11:00:02 [snandago@sureshn ~]$ ``` > Above docker inspect filter show container ip address and next filter show mac address. We can can filter multiple informations. ## Build Custom image by writing the Dockerfile we can build the custome image by our wish. > below is the sample Dockerfile ``` [snandago@sureshn docker]$ cat Dockerfile #sample Dockerfile FROM centos MAINTAINER sureshcbe5@gmail.com RUN yum install -y httpd; yum clean all COPY index.html /var/www/html EXPOSE 80 CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"] [snandago@sureshn docker]$ ``` ##### Small explaination on Dockerfile writing. How this file wrting is done. > Dockerfile file must start with `FROM` instruction we can add the base image name there. > The `MAINTAINER` instruction allows you to set the Author field of the generated images. > `RUN <command>` (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows) > `COPY` Copies new files or directories from <src> and adds them to the filesystem of the image at the path <dest>. > `EXPOSE` Informs Docker that the container listens on the specified network port(s) at runtime. > `CMD` The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well. > create a Dockerfile and run docker build command to build the custom image. ``` [snandago@sureshn docker]$ docker build -t custombuild_image . Sending build context to Docker daemon 3.072kB Step 1/6 : FROM centos ---> 470671670cac Step 2/6 : MAINTAINER sureshcbe5@gmail.com ---> Running in dfb3416849c6 Removing intermediate container dfb3416849c6 ---> 03af3fb70aad Step 3/6 : RUN yum install -y httpd; yum clean all ---> Running in ae9595d0dede CentOS-8 - AppStream 1.9 MB/s | 6.5 MB 00:03 CentOS-8 - Base 2.6 MB/s | 5.0 MB 00:01 CentOS-8 - Extras 5.7 kB/s | 4.2 kB 00:00 Dependencies resolved. ================================================================================ Package Arch Version Repo Size ================================================================================ Installing: httpd x86_64 2.4.37-16.module_el8.1.0+256+ae790463 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-16.module_el8.1.0+256+ae790463 AppStream 35 k httpd-tools x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 103 k mod_http2 x86_64 1.11.3-3.module_el8.1.0+213+acce2796 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 ________trimmed some content to make it smaller----------------------------- Complete! 20 files removed Removing intermediate container ae9595d0dede ---> 1e797d9d4344 Step 4/6 : COPY index.html /var/www/html ---> f6b8f7331d0e Step 5/6 : EXPOSE 80 ---> Running in 1c0f40e48936 Removing intermediate container 1c0f40e48936 ---> 878e8361d5ce Step 6/6 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"] ---> Running in 5d8785e85068 Removing intermediate container 5d8785e85068 ---> d0ee1706467b Successfully built d0ee1706467b Successfully tagged custombuild_image:latest [snandago@sureshn docker]$ ``` > `docker images` show up the custom buid image. Now we can try running this container. ``` [snandago@sureshn docker]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE custombuild_image latest d0ee1706467b 5 minutes ago 259MB myutil/httpd latest c8811902f987 2 hours ago 381MB centos latest 470671670cac 2 months ago 237MB centos/httpd latest 2cc07fbb5000 14 months ago 258MB centos latest 49f7960eb7e4 21 months ago 200MB fedora/firefox latest 7fa9c2fade45 3 years ago 880MB [snandago@sureshn docker]$ docker run --name custom_build -d -p 9098:80 custombuild_image 39c1ac7a24067e5030a214c96c0cc58cca0ca11dfd22646b78d29e2daabb1dce [snandago@sureshn docker]$ curl 127.0.0.1:9098 Cutom build tested! [snandago@sureshn docker]$ ``` > yes! we can see the container running and the index.html file we copied while build shows up. :+1: ## Running mysql container Run the mysql container mysql-57-centos7 docker registry image. ``` docker run -d --name mysql \ -v /mydata/data:/var/lib/mysql/init \ -e MYSQL_DATABASE=lab \ -e MYSQL_USER=user \ -e MYSQL_PASSWORD=password \ -e MYSQL_ROOT_PASSWORD=password! \ centos/mysql-57-centos7 ``` find the network details of the container and connect the db and verify. ``` docker inspect -f '{{ .NetworkSettings.IPAddress }}' mysql ``` ``` [snandago@sureshn data]$ mysql -u root -p -h 172.17.0.2 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 38 Server version: 5.7.24 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | lab | | mysql | | new_schema | | performance_schema | | sys | +--------------------+ 6 rows in set (0.002 sec) MySQL [(none)]> use lab; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MySQL [lab]> show tables; +---------------+ | Tables_in_lab | +---------------+ | data | | lab_inventory | +---------------+ 2 rows in set (0.001 sec) MySQL [lab]> ``` ###### tags: `container` `docker` `Learning`