Docker-1: Installation and basic operations

Docker memo

This memo contains the following items:

  1. Docker installation from Repository
  2. Post operation after docker installation
  3. Basic docker operation commands

Docker installation from Repository in Ubuntu 18.04 LTS

There are several ways to install docker. I followed its official one here:

https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-using-the-repository

Install using the repository

Before you install Docker CE for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

SET UP THE REPOSITORY
  1. Update the apt package index:
    $ sudo apt-get update
    
  2. Install packages to allow apt to use a repository over HTTPS:
    $ sudo apt-get install apt-transport-https ca-certificates curl \
        software-properties-common
    
  3. Add Docker’s official GPG key:
    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    

    Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

    $ sudo apt-key fingerprint 0EBFCD88
    
    pub   4096R/0EBFCD88 2017-02-22
          Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
    uid                  Docker Release (CE deb) <docker@docker.com>
    sub   4096R/F273FCD8 2017-02-22
    
  4. Use the following command to set up the stable repository. You always need the stable repository, even if you want to install builds from the edge or test repositories as well. To add the edge or test repository, add the word edge or test (or both) after the word stable in the commands below.

    Note: The lsb_release -cs sub-command below returns the name of your Ubuntu distribution, such as xenial. Sometimes, in a distribution like Linux Mint, you might need to change $(lsb_release -cs) to your parent Ubuntu distribution. For example, if you are using Linux Mint Rafaela, you could use trusty.

    $ sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    

    Note: Starting with Docker 17.06, stable releases are also pushed to the edge and test repositories.

INSTALL DOCKER CE
  1. Update the apt package index.
    $ sudo apt-get update
    
  2. Install the latest version of Docker CE, or go to the next step to install a specific version:
    $ sudo apt-get install docker-ce

Post operation after docker installation

This follows the official guide on "https://docs.docker.com/install/linux/linux-postinstall/" with some modification.

To create the docker group and add your user:

  1. Create the docker group. (I have skipped this step and it also ok finally).
    $ sudo groupadd docker
    
  2. Add your user to the docker group.
    $ sudo usermod -aG docker $USER
    
  3. Log out and log back in so that your group membership is re-evaluated. Important.On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.
  4. Verify that you can run docker commands without sudo.
    $ docker run hello-world

Basic docker operation commands

  1. docker images
    -List all local available docker images. The list will contain the images' repository, tag, image_id, created date, and file size information.

    chuantao@chuantao-IdeaPad:~$ docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    nginx-fun latest 766e49a9bec5 6 hours ago 109MB
    nginx latest 8b89e48b5f15 18 hours ago 109MB
    hello-world latest 2cb0d9787c4d 6 days ago 1.85kB
    ubuntu latest 113a43faa138 6 weeks ago 81.2MB
  2. docker run imageName/imageId
    -start running a docker container from a docker image.

    • docker run hello-world OR
    • docker run 2cb0d9787c4d will both launch the hello-world image.
    • docker run ubuntu echo hello-world  (This will launch an ubuntu instance and run echo command to print msg "hello-world").
    • docker run -p 8080:80 -d nginx
      This will launch an nginx server and forward its port 80 to host's 8080.
      -p: Publish a container’s port(s) to the host
      -d: Run container in background and print container ID.

      chuantao@chuantao-IdeaPad:~$ docker run -p 8080:80 -d nginx
      12f2ad4e6ae0acb5537e98107b4aa5160b2e75aebea5b95b78aed0a1f5ca3392
  3. docker ps
    -List currently running docker containers.

    chuantao@chuantao-IdeaPad:~$ docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    12f2ad4e6ae0 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp stupefied_joliot
  4. docker ps -a
    -List all docker containers including those have been stopped.

    chuantao@chuantao-IdeaPad:~$ docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    12f2ad4e6ae0 nginx "nginx -g 'daemon of…" 12 minutes ago Exited (0) 8 minutes ago stupefied_joliot
    af75c2adb46f ubuntu "echo hello-world" 18 minutes ago Exited (0) 18 minutes ago admiring_bell
    552b643f243e hello-world "/hello" 20 minutes ago Exited (0) 20 minutes ago festive_brattain
    79c7b54f9111 2cb0d9787c4d "/hello" 20 minutes ago Exited (0) 20 minutes ago awesome_kilby
    deacf334c88b nginx "nginx -g 'daemon of…" 6 hours ago Exited (0) 6 hours ago agitated_proskuriakova

    Note above, docker run containerId and docker run containerImage return different container id.

  5. docker stop containerId
    -This will stop a running docker container matching the given container id.
    -Stopped containerId can be re run by docker start command.
  6. docker rm containerId
    -Remove those have been stopped.
  7. docker rmi imageId
    -Remove local docker image.
  8. docker pull imageName
  9. docker commit -m 'commit comment' currentContainerId newDockerImageName
    -Create a new docker image locally.
  10. docker start containerId
    -Start one or more stopped containers.
  11. docker cp hostFiles containerId://container path
    docker cp index.html 90ddb2f0e986://usr/share/nginx/html

    copy index.html to container's html folder.

Leave a Reply

Your email address will not be published.