Skip to content

Instantly share code, notes, and snippets.

@chirag773
Last active December 21, 2019 09:07
Show Gist options
  • Save chirag773/32c961e33ace885a3a9e4711b01537d2 to your computer and use it in GitHub Desktop.
Save chirag773/32c961e33ace885a3a9e4711b01537d2 to your computer and use it in GitHub Desktop.

How to Install Docker

  • First, update your existing list of packages:

    ->sudo apt update

  • Next, install a few prerequisite packages which let apt use packages over HTTPS:

    ->sudo apt install apt-transport-https ca-certificates curl software-properties-common

  • Then add the GPG key for the official Docker repository to your system:

    ->curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • Add the Docker repository to APT sources:

    ->sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

  • Next, update the package database with the Docker packages from the newly added repo:

    ->sudo apt-get update

  • Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:

    ->apt-cache policy docker-ce

  • You’ll see output like this, although the version number for Docker may be different:

docker-ce:
  Installed: (none)
  Candidate: 18.03.1~ce~3-0~ubuntu
  Version table:
     18.03.1~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages

Finally, install Docker:

->sudo apt install docker-ce

  • Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
Output
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-07-05 15:08:39 UTC; 2min 55s ago
     Docs: https://docs.docker.com
 Main PID: 10096 (dockerd)
    Tasks: 16
   CGroup: /system.slice/docker.service
           ├─10096 /usr/bin/dockerd -H fd://
           └─10113 docker-containerd --config /var/run/docker/containerd/containerd.toml

Executing the Docker Command Without Sudo (Optional)

  • If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

    ->sudo usermod -aG docker ${USER}

  • To apply the new group membership, log out of the server and back in, or type the following:

    ->su - ${USER}

  • You will be prompted to enter your user’s password to continue.

  • Confirm that your user is now added to the docker group by typing:

    ->id -nG

Output
<Your Username> sudo docker
  • If you need to add a user to the docker group that you’re not logged in as, declare that username explicitly using:

    ->sudo usermod -aG docker username

How to use Docker

  • To build Docker image

    -> docker build -t <application_name> .

  • To Check Docker Images

    -> docker images

  • To create and run docker at once

    -> docker run -i -t <Image_ID || Image_name> /bin/bash

  • Another method to create container

    -> docker create --name <give_name_of_container> <Image_Name>

  • To start Docker

    ->docker start <container_name || container_ID>

  • To run conatiner if crteated container using above command

    ->docker exec -it <container_name> bash

  • Remove a Docker image

    -> docker rmi <docker_image_id>

  • Remove a Docker container

    -> docker rm <container_name || container ID>

  • Show Running Containers

    -> docker ps

  • Show all Running Containers

    -> docker ps -a

  • Stop Container

    -> docker stop <container_id>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment