{"id":257,"date":"2022-12-24T08:53:33","date_gmt":"2022-12-24T06:53:33","guid":{"rendered":"https:\/\/amsd.go.ro\/wordpress\/?p=257"},"modified":"2022-12-24T08:53:33","modified_gmt":"2022-12-24T06:53:33","slug":"how-to-install-and-use-docker-on-centos","status":"publish","type":"post","link":"https:\/\/amsd.go.ro\/wordpress\/?p=257","title":{"rendered":"How To Install and Use Docker on CentOS"},"content":{"rendered":"\n<p>Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system.<\/p>\n\n\n\n<p><strong>Note:<\/strong> Docker requires a 64-bit version of CentOS as well as a kernel version equal to or greater than 3.10.<\/p>\n\n\n\n<p>All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by <code>sudo<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-1-installing-docker\">Step 1 \u2014 Installing Docker<\/h2>\n\n\n\n<p>The Docker installation package available in the official CentOS 7 repository may not be the latest version. To get the latest and greatest version, install Docker from the official Docker repository. This section shows you how to do just that.<\/p>\n\n\n\n<p>But first, let\u2019s update the package database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo yum check-update\n\n<\/code><\/pre>\n\n\n\n<p>Now run this command. It will add the official Docker repository, download the latest version of Docker, and install it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -fsSL https:\/\/get.docker.com\/ | sh\n\n<\/code><\/pre>\n\n\n\n<p>After installation has completed, start the Docker daemon:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start docker\n\n<\/code><\/pre>\n\n\n\n<p>Verify that it\u2019s running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status docker\n\n<\/code><\/pre>\n\n\n\n<p>The output should be similar to the following, showing that the service is active and running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output\u25cf docker.service - Docker Application Container Engine\n   Loaded: loaded (\/lib\/systemd\/system\/docker.service; enabled; vendor preset: enabled)\n   Active: <mark>active (running)<\/mark> since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago\n     Docs: https:\/\/docs.docker.com\n Main PID: 749 (docker)\n<\/code><\/pre>\n\n\n\n<p>Lastly, make sure it starts at every server reboot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable docker\n\n<\/code><\/pre>\n\n\n\n<p>Installing Docker now gives you not just the Docker service (daemon) but also the <code>docker<\/code> command line utility, or the Docker client. We\u2019ll explore how to use the <code>docker<\/code> command later in this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-2-executing-docker-command-without-sudo-optional\">Step 2 \u2014 Executing Docker Command Without Sudo (Optional)<\/h2>\n\n\n\n<p>By default, running the <code>docker<\/code> command requires root privileges \u2014 that is, you have to prefix the command with <code>sudo<\/code>. It can also be run by a user in the <strong>docker<\/strong> group, which is automatically created during the installation of Docker. If you attempt to run the <code>docker<\/code> command without prefixing it with <code>sudo<\/code> or without being in the docker group, you\u2019ll get an output like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Outputdocker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.\nSee 'docker run --help'.\n<\/code><\/pre>\n\n\n\n<p>If you want to avoid typing <code>sudo<\/code> whenever you run the <code>docker<\/code> command, add your username to the docker group:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo usermod -aG docker $(whoami)\n\n<\/code><\/pre>\n\n\n\n<p>You will need to log out of the Droplet and back in as the same user to enable this change.<\/p>\n\n\n\n<p>If you need to add a user to the <code>docker<\/code> group that you\u2019re not logged in as, declare that username explicitly using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo usermod -aG docker <mark>username<\/mark>\n\n<\/code><\/pre>\n\n\n\n<p>The rest of this article assumes you are running the <code>docker<\/code> command as a user in the docker user group. If you choose not to, please prepend the commands with <code>sudo<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-3-using-the-docker-command\">Step 3 \u2014 Using the Docker Command<\/h2>\n\n\n\n<p>With Docker installed and working, now\u2019s the time to become familiar with the command line utility. Using <code>docker<\/code> consists of passing it a chain of options and subcommands followed by arguments. The syntax takes this form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker &#91;option] &#91;command] &#91;arguments]\n\n<\/code><\/pre>\n\n\n\n<p>To view all available subcommands, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker\n\n<\/code><\/pre>\n\n\n\n<p>As of Docker 1.11.1, the complete list of available subcommands includes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output\n    attach    Attach to a running container\n    build     Build an image from a Dockerfile\n    commit    Create a new image from a container's changes\n    cp        Copy files\/folders between a container and the local filesystem\n    create    Create a new container\n    diff      Inspect changes on a container's filesystem\n    events    Get real time events from the server\n    exec      Run a command in a running container\n    export    Export a container's filesystem as a tar archive\n    history   Show the history of an image\n    images    List images\n    import    Import the contents from a tarball to create a filesystem image\n    info      Display system-wide information\n    inspect   Return low-level information on a container or image\n    kill      Kill a running container\n    load      Load an image from a tar archive or STDIN\n    login     Log in to a Docker registry\n    logout    Log out from a Docker registry\n    logs      Fetch the logs of a container\n    network   Manage Docker networks\n    pause     Pause all processes within a container\n    port      List port mappings or a specific mapping for the CONTAINER\n    ps        List containers\n    pull      Pull an image or a repository from a registry\n    push      Push an image or a repository to a registry\n    rename    Rename a container\n    restart   Restart a container\n    rm        Remove one or more containers\n    rmi       Remove one or more images\n    run       Run a command in a new container\n    save      Save one or more images to a tar archive\n    search    Search the Docker Hub for images\n    start     Start one or more stopped containers\n    stats     Display a live stream of container(s) resource usage statistics\n    stop      Stop a running container\n    tag       Tag an image into a repository\n    top       Display the running processes of a container\n    unpause   Unpause all processes within a container\n    update    Update configuration of one or more containers\n    version   Show the Docker version information\n    volume    Manage Docker volumes\n    wait      Block until a container stops, then print its exit code\n<\/code><\/pre>\n\n\n\n<p>To view the switches available to a specific command, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker <mark>docker-subcommand<\/mark> --help\n\n<\/code><\/pre>\n\n\n\n<p>To view system-wide information, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker info\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-4-working-with-docker-images\">Step 4 \u2014 Working with Docker Images<\/h2>\n\n\n\n<p>Docker containers are run from Docker images. By default, it pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anybody can build and host their Docker images on Docker Hub, so most applications and Linux distributions you\u2019ll need to run Docker containers have images that are hosted on Docker Hub.<\/p>\n\n\n\n<p>To check whether you can access and download images from Docker Hub, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run hello-world\n\n<\/code><\/pre>\n\n\n\n<p>The output, which should include the following, should indicate that Docker in working correctly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputHello from Docker.\nThis message shows that your installation appears to be working correctly.\n...\n<\/code><\/pre>\n\n\n\n<p>You can search for images available on Docker Hub by using the <code>docker<\/code> command with the <code>search<\/code> subcommand. For example, to search for the CentOS image, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker search centos\n\n<\/code><\/pre>\n\n\n\n<p>The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputNAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED\ncentos                          The official build of CentOS.                   2224      &#91;OK]       \njdeathe\/centos-ssh              CentOS-6 6.7 x86_64 \/ CentOS-7 7.2.1511 x8...   22                   &#91;OK]\njdeathe\/centos-ssh-apache-php   CentOS-6 6.7 x86_64 \/ Apache \/ PHP \/ PHP M...   17                   &#91;OK]\nmillion12\/centos-supervisor     Base CentOS-7 with supervisord launcher, h...   11                   &#91;OK]\nnimmis\/java-centos              This is docker images of CentOS 7 with dif...   10                   &#91;OK]\ntorusware\/speedus-centos        Always updated official CentOS docker imag...   8                    &#91;OK]\nnickistre\/centos-lamp           LAMP on centos setup                            3                    &#91;OK]\n\n...\n<\/code><\/pre>\n\n\n\n<p>In the <strong>OFFICIAL<\/strong> column, <strong>OK<\/strong> indicates an image built and supported by the company behind the project. Once you\u2019ve identifed the image that you would like to use, you can download it to your computer using the <code>pull<\/code> subcommand, like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker pull centos\n\n<\/code><\/pre>\n\n\n\n<p>After an image has been downloaded, you may then run a container using the downloaded image with the <code>run<\/code> subcommand. If an image has not been downloaded when <code>docker<\/code> is executed with the <code>run<\/code> subcommand, the Docker client will first download the image, then run a container using it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run centos\n\n<\/code><\/pre>\n\n\n\n<p>To see the images that have been downloaded to your computer, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker images\n\n<\/code><\/pre>\n\n\n\n<p>The output should look similar to the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;secondary_lable Output]\nREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\ncentos              latest              778a53015523        5 weeks ago         196.7 MB\nhello-world         latest              94df4f0ce8a4        2 weeks ago         967 B\n<\/code><\/pre>\n\n\n\n<p>As you\u2019ll see later in this tutorial, images that you use to run containers can be modified and used to generate new images, which may then be uploaded (<em>pushed<\/em> is the technical term) to Docker Hub or other Docker registries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-5-running-a-docker-container\">Step 5 \u2014 Running a Docker Container<\/h2>\n\n\n\n<p>The <code>hello-world<\/code> container you ran in the previous step is an example of a container that runs and exits, after emitting a test message. Containers, however, can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.<\/p>\n\n\n\n<p>As an example, let\u2019s run a container using the latest image of CentOS. The combination of the <strong>-i<\/strong> and <strong>-t<\/strong> switches gives you interactive shell access into the container:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -it centos\n\n<\/code><\/pre>\n\n\n\n<p>Your command prompt should change to reflect the fact that you\u2019re now working inside the container and should take this form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output&#91;root@59839a1b7de2 \/]#\n<\/code><\/pre>\n\n\n\n<p><strong>Important:<\/strong> Note the container id in the command prompt. In the above example, it is <code>59839a1b7de2<\/code>.<\/p>\n\n\n\n<p>Now you may run any command inside the container. For example, let\u2019s install MariaDB server in the running container. No need to prefix any command with <code>sudo<\/code>, because you\u2019re operating inside the container with root privileges:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install mariadb-server\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-6-committing-changes-in-a-container-to-a-docker-image\">Step 6 \u2014 Committing Changes in a Container to a Docker Image<\/h2>\n\n\n\n<p>When you start up a Docker image, you can create, modify, and delete files just like you can with a virtual machine. The changes that you make will only apply to that container. You can start and stop it, but once you destroy it with the <code>docker rm<\/code> command, the changes will be lost for good.<\/p>\n\n\n\n<p>This section shows you how to save the state of a container as a new Docker image.<\/p>\n\n\n\n<p>After installing MariaDB server inside the CentOS container, you now have a container running off an image, but the container is different from the image you used to create it.<\/p>\n\n\n\n<p>To save the state of the container as a new image, first exit from it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit\n\n<\/code><\/pre>\n\n\n\n<p>Then commit the changes to a new Docker image instance using the following command. The <strong>-m<\/strong> switch is for the commit message that helps you and others know what changes you made, while <strong>-a<\/strong> is used to specify the author. The container ID is the one you noted earlier in the tutorial when you started the interactive docker session. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker commit -m \"What did you do to the image\" -a \"Author Name\" <mark>container-id<\/mark> <mark>repository<\/mark>\/<mark>new_image_name<\/mark>\n\n<\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker commit -m \"added mariadb-server\" -a \"Sunday Ogwu-Chinuwa\" 59839a1b7de2 finid\/centos-mariadb\n\n<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> When you <em>commit<\/em> an image, the new image is saved locally, that is, on your computer. Later in this tutorial, you\u2019ll learn how to push an image to a Docker registry like Docker Hub so that it may be assessed and used by you and others.<\/p>\n\n\n\n<p>After that operation has completed, listing the Docker images now on your computer should show the new image, as well as the old one that it was derived from:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker images\n\n<\/code><\/pre>\n\n\n\n<p>The output should be of this sort:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputREPOSITORY             TAG                 IMAGE ID            CREATED             SIZE\nfinid\/centos-mariadb   latest              23390430ec73        6 seconds ago       424.6 MB\ncentos                 latest              778a53015523        5 weeks ago         196.7 MB\nhello-world            latest              94df4f0ce8a4        2 weeks ago         967 B\n<\/code><\/pre>\n\n\n\n<p>In the above example, <strong>centos-mariadb<\/strong> is the new image, which was derived from the existing CentOS image from Docker Hub. The size difference reflects the changes that were made. And in this example, the change was that MariaDB server was installed. So next time you need to run a container using CentOS with MariaDB server pre-installed, you can just use the new image. Images may also be built from what\u2019s called a Dockerfile. But that\u2019s a very involved process that\u2019s well outside the scope of this article. We\u2019ll explore that in a future article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-7-listing-docker-containers\">Step 7 \u2014 Listing Docker Containers<\/h2>\n\n\n\n<p>After using Docker for a while, you\u2019ll have many active (running) and inactive containers on your computer. To view the active ones, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps\n\n<\/code><\/pre>\n\n\n\n<p>You will see output similar to the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\nf7c79cc556dd        centos              \"\/bin\/bash\"         3 hours ago         Up 3 hours                              silly_spence\n<\/code><\/pre>\n\n\n\n<p>To view all containers \u2014 active and inactive, pass it the <code>-a<\/code> switch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps -a\n\n<\/code><\/pre>\n\n\n\n<p>To view the latest container you created, pass it the <code>-l<\/code> switch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps -l\n\n<\/code><\/pre>\n\n\n\n<p>Stopping a running or active container is as simple as typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stop <mark>container-id<\/mark>\n\n<\/code><\/pre>\n\n\n\n<p>The <mark><code>container-id<\/code><\/mark> can be found in the output from the <code>docker ps<\/code> command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-8-pushing-docker-images-to-a-docker-repository\">Step 8 \u2014 Pushing Docker Images to a Docker Repository<\/h2>\n\n\n\n<p>The next logical step after creating a new image from an existing image is to share it with a select few of your friends, the whole world on Docker Hub, or other Docker registry that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.<\/p>\n\n\n\n<p>This section shows you how to push a Docker image to Docker Hub.<\/p>\n\n\n\n<p>To create an account on Docker Hub, register at <a href=\"https:\/\/hub.docker.com\/\">Docker Hub<\/a>. Afterwards, to push your image, first log into Docker Hub. You\u2019ll be prompted to authenticate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker login -u <mark>docker-registry-username<\/mark>\n\n<\/code><\/pre>\n\n\n\n<p>If you specified the correct password, authentication should succeed. Then you may push your own image using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker push <mark>docker-registry-username<\/mark>\/<mark>docker-image-name<\/mark>\n\n<\/code><\/pre>\n\n\n\n<p>It will take sometime to complete, and when completed, the output will be of this sort:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputThe push refers to a repository &#91;docker.io\/finid\/centos-mariadb]\n670194edfaf5: Pushed \n5f70bf18a086: Mounted from library\/centos \n6a6c96337be1: Mounted from library\/centos\n\n...\n\n<\/code><\/pre>\n\n\n\n<p>After pushing an image to a registry, it should be listed on your account\u2019s dashboard.<\/p>\n\n\n\n<p>If a push attempt results in an error of this sort, then you likely did not log in:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputThe push refers to a repository &#91;docker.io\/finid\/centos-mariadb]\ne3fbbfb44187: Preparing\n5f70bf18a086: Preparing\na3b5c80a4eba: Preparing\n7f18b442972b: Preparing\n3ce512daaf78: Preparing\n7aae4540b42d: Waiting\nunauthorized: authentication required\n<\/code><\/pre>\n\n\n\n<p>Log in, then repeat the push attempt.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system. Note: Docker requires a 64-bit [&hellip;]<\/p>\n<div class=\"link-more\"><a href=\"https:\/\/amsd.go.ro\/wordpress\/?p=257#more-257\" class=\"more-link\">Continue reading &#10142; <span class=\"screen-reader-text\">How To Install and Use Docker on CentOS<\/span><\/a><\/div>","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":1,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}