{"id":11796,"date":"2019-03-17T10:51:00","date_gmt":"2019-03-17T10:51:00","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11796"},"modified":"2019-03-17T10:51:00","modified_gmt":"2019-03-17T10:51:00","slug":"how-to-install-docker-and-run-docker-containers-in-ubuntu","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-install-docker-and-run-docker-containers-in-ubuntu\/","title":{"rendered":"How to Install Docker and Run Docker Containers in Ubuntu"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/altis.com.au\/wp-content\/uploads\/2017\/07\/Untitled-1.png\" alt=\"Related image\" \/><\/p>\n<p><strong>Docker<\/strong>\u00a0is an open source and popular operating system-level virtualization (commonly known as \u201c<strong>containerization<\/strong>\u201d) technology that primarily runs on Linux and Windows.\u00a0<strong>Docker<\/strong>\u00a0makes it easier to create, deploy, and run applications by using\u00a0<strong>containers<\/strong>.<\/p>\n<p>With\u00a0<strong>containers<\/strong>, developers (and system administrators) can package up an application with everything needed to run the application \u2013 the code, a run-time, libraries, environment variables, and configuration files, and ship it all out as one package. Yes, it\u2019s that great!<\/p>\n<p>In this article, we will show you how to install\u00a0<strong>Docker CE (Community Edition)<\/strong>, create and run Docker containers on\u00a0<strong>Ubuntu<\/strong>\u00a0distribution.<\/p>\n<h3>Installing Docker CE (Community Edition) in Ubuntu<\/h3>\n<p><strong>1.<\/strong>\u00a0To install\u00a0<strong>Docker CE<\/strong>, first, you need to remove older versions of\u00a0<strong>Docker<\/strong>\u00a0were called\u00a0<code>docker<\/code>,\u00a0<code>docker.io<\/code>, or\u00a0<code>docker-engine<\/code>\u00a0from the system using the following command.<\/p>\n<pre>$ sudo apt-get remove docker docker-engine docker.io containerd runc\r\n<\/pre>\n<p><center><strong>2.<\/strong>\u00a0Next, you need to set up the Docker repository to install and update Docker from the repository using following commands.<\/center><\/p>\n<pre>$ sudo apt-get update\r\n$ curl -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg | sudo apt-key add -\r\n$ sudo add-apt-repository \"deb [arch=amd64] https:\/\/download.docker.com\/linux\/ubuntu $(lsb_release -cs) stable\"\r\n<\/pre>\n<p><strong>3.<\/strong>\u00a0Update the apt package index and install the latest version of\u00a0<strong>Docker CE<\/strong>\u00a0using following commands.<\/p>\n<pre>$ sudo apt-get update\r\n$ sudo apt-get install docker-ce docker-ce-cli containerd.io\r\n<\/pre>\n<div class=\"code-label\">Installing Docker CE in Ubuntu<\/div>\n<pre>Reading package lists... Done\r\nBuilding dependency tree       \r\nReading state information... Done\r\nThe following additional packages will be installed:\r\n  aufs-tools cgroupfs-mount pigz\r\nThe following NEW packages will be installed:\r\n  aufs-tools cgroupfs-mount containerd.io docker-ce docker-ce-cli pigz\r\n0 upgraded, 6 newly installed, 0 to remove and 167 not upgraded.\r\nNeed to get 50.7 MB of archives.\r\nAfter this operation, 243 MB of additional disk space will be used.\r\nDo you want to continue? [Y\/n] y\r\nGet:1 http:\/\/archive.ubuntu.com\/ubuntu bionic\/universe amd64 pigz amd64 2.4-1 [57.4 kB]\r\nGet:2 https:\/\/download.docker.com\/linux\/ubuntu bionic\/stable amd64 containerd.io amd64 1.2.4-1 [19.9 MB]\r\nGet:3 http:\/\/archive.ubuntu.com\/ubuntu bionic\/universe amd64 aufs-tools amd64 1:4.9+20170918-1ubuntu1 [104 kB]\r\nGet:4 http:\/\/archive.ubuntu.com\/ubuntu bionic\/universe amd64 cgroupfs-mount all 1.4 [6,320 B]\r\nGet:5 https:\/\/download.docker.com\/linux\/ubuntu bionic\/stable amd64 docker-ce-cli amd64 5:18.09.3~3-0~ubuntu-bionic [13.1 MB]\r\nGet:6 https:\/\/download.docker.com\/linux\/ubuntu bionic\/stable amd64 docker-ce amd64 5:18.09.3~3-0~ubuntu-bionic [17.4 MB]\r\nFetched 50.7 MB in 7s (7,779 kB\/s)                                     \r\n...\r\n<\/pre>\n<p><strong>4.<\/strong>\u00a0After successfully installing the\u00a0<strong>Docker CE<\/strong>\u00a0package, the service should be auto-started and auto-enabled to start at system boot, you can check its status using the following command.<\/p>\n<pre>$ sudo systemctl status docker \r\n<\/pre>\n<div class=\"code-label\">Checking Docker CE Status<\/div>\n<pre>\u25cf docker.service - Docker Application Container Engine\r\n   Loaded: loaded (\/lib\/systemd\/system\/docker.service; enabled; vendor preset: enabled)\r\n   Active: active (running) since Wed 2019-03-06 08:06:42 UTC; 2min 18s ago\r\n     Docs: https:\/\/docs.docker.com\r\n Main PID: 5274 (dockerd)\r\n    Tasks: 8\r\n   CGroup: \/system.slice\/docker.service\r\n           \u2514\u25005274 \/usr\/bin\/dockerd -H fd:\/\/ --containerd=\/run\/containerd\/containerd.sock\r\n\r\nMar 06 08:06:41 tecmint dockerd[5274]: time=\"2019-03-06T08:06:41.562587408Z\" level=warning msg=\"Your kernel does not support cgroup rt runtime\"\r\nMar 06 08:06:41 tecmint dockerd[5274]: time=\"2019-03-06T08:06:41.562767803Z\" level=warning msg=\"Your kernel does not support cgroup blkio weight\"\r\nMar 06 08:06:41 tecmint dockerd[5274]: time=\"2019-03-06T08:06:41.562966844Z\" level=warning msg=\"Your kernel does not support cgroup blkio weight_device\"\r\nMar 06 08:06:41 tecmint dockerd[5274]: time=\"2019-03-06T08:06:41.565298457Z\" level=info msg=\"Loading containers: start.\"\r\nMar 06 08:06:41 tecmint dockerd[5274]: time=\"2019-03-06T08:06:41.950942467Z\" 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 prefer\r\nMar 06 08:06:42 tecmint dockerd[5274]: time=\"2019-03-06T08:06:42.036964493Z\" level=info msg=\"Loading containers: done.\"\r\nMar 06 08:06:42 tecmint dockerd[5274]: time=\"2019-03-06T08:06:42.156279378Z\" level=info msg=\"Docker daemon\" commit=774a1f4 graphdriver(s)=overlay2 version=18.09.3\r\nMar 06 08:06:42 tecmint dockerd[5274]: time=\"2019-03-06T08:06:42.157145045Z\" level=info msg=\"Daemon has completed initialization\"\r\nMar 06 08:06:42 tecmint systemd[1]: Started Docker Application Container Engine.\r\nMar 06 08:06:42 tecmint dockerd[5274]: time=\"2019-03-06T08:06:42.224229999Z\" level=info msg=\"API listen on \/var\/run\/docker.sock\"\r\n<\/pre>\n<p><strong>5.<\/strong>\u00a0Finally, verify that\u00a0<strong>Docker CE<\/strong>\u00a0is installed properly by running the\u00a0<code>hello-world<\/code>\u00a0image.<\/p>\n<pre>$ sudo docker run hello-world\r\n<\/pre>\n<div class=\"code-label\">Verify Docker CE Installation<\/div>\n<pre>Hello from Docker!\r\nThis message shows that your installation appears to be working correctly.\r\n\r\nTo generate this message, Docker took the following steps:\r\n 1. The Docker client contacted the Docker daemon.\r\n 2. The Docker daemon pulled the \"hello-world\" image from the Docker Hub.\r\n    (amd64)\r\n 3. The Docker daemon created a new container from that image which runs the\r\n    executable that produces the output you are currently reading.\r\n 4. The Docker daemon streamed that output to the Docker client, which sent it\r\n    to your terminal.\r\n\r\nTo try something more ambitious, you can run an Ubuntu container with:\r\n $ docker run -it ubuntu bash\r\n\r\nShare images, automate workflows, and more with a free Docker ID:\r\n https:\/\/hub.docker.com\/\r\n\r\nFor more examples and ideas, visit:\r\n https:\/\/docs.docker.com\/get-started\/\r\n<\/pre>\n<h3>Learn Basic Docker Commands in Ubuntu<\/h3>\n<p><strong>6.<\/strong>\u00a0To get information about\u00a0<strong>Docker<\/strong>, run the following command.<\/p>\n<pre><strong>$ sudo docker info<\/strong>\r\n\r\nKernel Version: 5.0.0-050000-generic\r\nOperating System: Ubuntu 18.04.1 LTS\r\nOSType: linux\r\nArchitecture: x86_64\r\nCPUs: 1\r\nTotal Memory: 1.452GiB\r\nName: tecmint\r\nID: FWSB:IRIF:DYL7:PRB5:Y66E:37MY:ISPO:HZVY:6YJO:2IYL:TO6Y:GNB7\r\nDocker Root Dir: \/var\/lib\/docker\r\nDebug Mode (client): false\r\nDebug Mode (server): false\r\nRegistry: https:\/\/index.docker.io\/v1\/\r\nLabels:\r\nExperimental: false\r\nInsecure Registries:\r\n 127.0.0.0\/8\r\nLive Restore Enabled: false\r\nProduct License: Community Engine\r\n<\/pre>\n<p><strong>7.<\/strong>\u00a0To get information about\u00a0<strong>Docker<\/strong>\u00a0version, run the following command.<\/p>\n<pre><strong>$ sudo docker version<\/strong>\r\n\r\nClient:\r\n Version:           18.09.3\r\n API version:       1.39\r\n Go version:        go1.10.8\r\n Git commit:        774a1f4\r\n Built:             Thu Feb 28 06:53:11 2019\r\n OS\/Arch:           linux\/amd64\r\n Experimental:      false\r\n\r\nServer: Docker Engine - Community\r\n Engine:\r\n  Version:          18.09.3\r\n  API version:      1.39 (minimum version 1.12)\r\n  Go version:       go1.10.8\r\n  Git commit:       774a1f4\r\n  Built:            Thu Feb 28 05:59:55 2019\r\n  OS\/Arch:          linux\/amd64\r\n  Experimental:     false\r\n<\/pre>\n<p><strong>8.<\/strong>\u00a0To get a list of all available Docker commands run\u00a0<code>docker<\/code>\u00a0on your terminal.<\/p>\n<pre><strong>$ docker<\/strong>\r\n\r\nUsage:\tdocker [OPTIONS] COMMAND\r\n\r\nA self-sufficient runtime for containers\r\n\r\nOptions:\r\n      --config string      Location of client config files (default \"\/home\/tecmint\/.docker\")\r\n  -D, --debug              Enable debug mode\r\n  -H, --host list          Daemon socket(s) to connect to\r\n  -l, --log-level string   Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\") (default \"info\")\r\n      --tls                Use TLS; implied by --tlsverify\r\n      --tlscacert string   Trust certs signed only by this CA (default \"\/home\/tecmint\/.docker\/ca.pem\")\r\n      --tlscert string     Path to TLS certificate file (default \"\/home\/tecmint\/.docker\/cert.pem\")\r\n      --tlskey string      Path to TLS key file (default \"\/home\/tecmint\/.docker\/key.pem\")\r\n      --tlsverify          Use TLS and verify the remote\r\n  -v, --version            Print version information and quit\r\n\r\nManagement Commands:\r\n  builder     Manage builds\r\n  config      Manage Docker configs\r\n  container   Manage containers\r\n  engine      Manage the docker engine\r\n  image       Manage images\r\n  network     Manage networks\r\n  node        Manage Swarm nodes\r\n  plugin      Manage plugins\r\n  secret      Manage Docker secrets\r\n  service     Manage services\r\n  stack       Manage Docker stacks\r\n  swarm       Manage Swarm\r\n  system      Manage Docker\r\n  trust       Manage trust on Docker images\r\n  volume      Manage volumes\r\n....\r\n<\/pre>\n<h3>Download a Docker Image in Ubuntu<\/h3>\n<p><strong>9.<\/strong>\u00a0To run a Docker container, first, you need to download an image from\u00a0<a href=\"https:\/\/hub.docker.com\/\" target=\"_blank\" rel=\"nofollow noopener\">Docker Hub<\/a>\u00a0\u2013 provides free images from its repositories.<\/p>\n<p>For example, to download a Docker image called\u00a0<code>CentOS 7<\/code>, issue the following command.<\/p>\n<pre><strong>$ sudo docker search centos<\/strong>\r\n\r\nNAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED\r\ncentos                             The official build of CentOS.                   5227                [OK]                \r\nansible\/centos7-ansible            Ansible on Centos7                              120                                     [OK]\r\njdeathe\/centos-ssh                 CentOS-6 6.10 x86_64 \/ CentOS-7 7.5.1804 x86\u2026   107                                     [OK]\r\nconsol\/centos-xfce-vnc             Centos container with \"headless\" VNC session\u2026   81                                      [OK]\r\nimagine10255\/centos6-lnmp-php56    centos6-lnmp-php56                              50                                      [OK]\r\ncentos\/mysql-57-centos7            MySQL 5.7 SQL database server                   47                                      \r\ntutum\/centos                       Simple CentOS docker image with SSH access      43                                      \r\ngluster\/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust\u2026   40                                      [OK]\r\nopenshift\/base-centos7             A Centos7 derived base image for Source-To-I\u2026   39                                      \r\ncentos\/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational \u2026   37                                      \r\ncentos\/python-35-centos7           Platform for building and running Python 3.5\u2026   33                                      \r\nkinogmt\/centos-ssh                 CentOS with SSH                                 26                                      [OK]\r\nopenshift\/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w\u2026   20                                      \r\ncentos\/php-56-centos7              Platform for building and running PHP 5.6 ap\u2026   19                                      \r\npivotaldata\/centos-gpdb-dev        CentOS image for GPDB development. Tag names\u2026   10                                      \r\nopenshift\/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use \u2026   6                                       \r\nopenshift\/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima\u2026   4                                       \r\ndarksheer\/centos                   Base Centos Image -- Updated hourly             3                                       [OK]\r\npivotaldata\/centos                 Base centos, freshened up a little with a Do\u2026   2                                       \r\npivotaldata\/centos-mingw           Using the mingw toolchain to cross-compile t\u2026   2                                       \r\npivotaldata\/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi\u2026   1                                       \r\nopenshift\/wildfly-81-centos7       A Centos7 based WildFly v8.1 image for use w\u2026   1                                       \r\nblacklabelops\/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]\r\nsmartentry\/centos                  centos with smartentry                          0                                       [OK]\r\njameseckersall\/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]\r\n<\/pre>\n<p><strong>10.<\/strong>\u00a0After you determined on what image you want to run based on your requirements, download it locally by running the below command (in this case a\u00a0<strong>CentOS<\/strong>\u00a0image is downloaded and used).<\/p>\n<pre><strong>$ docker pull centos<\/strong>\r\n\r\nUsing default tag: latest\r\nlatest: Pulling from library\/centos\r\na02a4930cb5d: Pull complete \r\nDigest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426\r\nStatus: Downloaded newer image for centos:latest\r\n<\/pre>\n<p><strong>11.<\/strong>\u00a0To list all the available Docker images on your host run the following command.<\/p>\n<pre><strong>$ sudo docker images<\/strong>\r\n\r\nREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\r\nhello-world         latest              fce289e99eb9        2 months ago        1.84kB\r\ncentos              latest              1e1148e4cc2c        3 months ago        202MB\r\n<\/pre>\n<p><strong>12.<\/strong>\u00a0If you don\u2019t want a\u00a0<strong>Docker<\/strong>\u00a0image anymore and you can remove it using the following command.<\/p>\n<pre><strong>$ sudo docker rmi centos<\/strong>\r\n\r\nUntagged: centos:latest\r\nUntagged: centos@sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426\r\nDeleted: sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb\r\nDeleted: sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956\r\n<\/pre>\n<h3>Run a Docker Container in Ubuntu<\/h3>\n<p><strong>13.<\/strong>\u00a0In order to create and run a Docker container, first you need to run a command into a downloaded\u00a0<code>CentOS<\/code>image, so a basic command would be to check the distribution version file inside the container using\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>, as shown.<\/p>\n<pre>$ docker run centos cat \/etc\/issue\r\n<\/pre>\n<p><strong>14.<\/strong>\u00a0To run the containers again, first you need to get the\u00a0<strong>Container ID or Name<\/strong>\u00a0by running the following command, which will display a list of the running and stopped containers:<\/p>\n<pre><strong>$ sudo docker ps -l<\/strong>\r\n\r\nCONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES\r\n<strong>0ddfa81c5779<\/strong>        centos              \"cat \/etc\/issue\"    About a minute ago   Exited (0) About a minute ago                       <strong>elastic_shirley<\/strong>\r\n<\/pre>\n<p><strong>15.<\/strong>\u00a0Once the\u00a0<strong>Container ID or Name<\/strong>\u00a0has been acquired, you can start the container using the following command:<\/p>\n<pre>$ sudo docker start <strong>0ddfa81c5779<\/strong>\r\nOR\r\n$ sudo docker start <strong>elastic_shirley<\/strong>\r\n<\/pre>\n<p>Here, the string\u00a0<code>0ddfa81c5779<\/code>\u00a0represents the container ID and\u00a0<code>elastic_shirley<\/code>\u00a0represents the container Name.<\/p>\n<p><strong>16.<\/strong>\u00a0To stop the running container run\u00a0<code>docker stop<\/code>\u00a0command by specifying the\u00a0<strong>Container ID or Name<\/strong>.<\/p>\n<pre>$ sudo docker stop <strong>0ddfa81c5779<\/strong>\r\nOR\r\n$ sudo docker stop <strong>elastic_shirley<\/strong>\r\n<\/pre>\n<p><strong>17.<\/strong>\u00a0A more better way, so that you don\u2019t have to remember the container ID would be to define a unique name for each container you create by using the\u00a0<code>--name<\/code>\u00a0option as shown.<\/p>\n<pre>$ docker run --name myname centos cat \/etc\/issue\r\n<\/pre>\n<p><strong>18.<\/strong>\u00a0In order to connect and run Linux commands into a container issue the following command.<\/p>\n<pre><strong>$ docker run -it centos bash<\/strong>\r\n\r\n[root@6213ec547863 \/]# uname -a\r\nLinux 6213ec547863 5.0.0-050000-generic #201903032031 SMP Mon Mar 4 01:33:18 UTC 2019 x86_64 x86_64 x86_64 GNU\/Linux\r\n[root@6213ec547863 \/]# cat \/etc\/redhat-release \r\nCentOS Linux release 7.6.1810 (Core) \r\n<\/pre>\n<p><strong>19.<\/strong>\u00a0To quit and back to host from the running container session you must type\u00a0<code>exit<\/code>\u00a0command as shown.<\/p>\n<pre>$ exit\r\n<\/pre>\n<p>That\u2019s all for basic container manipulation. If you have any questions or comments about this article, use the feedback form below to reach us.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/install-docker-and-run-docker-containers-in-ubuntu\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker\u00a0is an open source and popular operating system-level virtualization (commonly known as \u201ccontainerization\u201d) technology that primarily runs on Linux and Windows.\u00a0Docker\u00a0makes it easier to create, deploy, and run applications by using\u00a0containers. With\u00a0containers, developers (and system administrators) can package up an application with everything needed to run the application \u2013 the code, a run-time, libraries, environment &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-install-docker-and-run-docker-containers-in-ubuntu\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Install Docker and Run Docker Containers in Ubuntu&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11796","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11796"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11796\/revisions"}],"predecessor-version":[{"id":11797,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11796\/revisions\/11797"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}