07 January 2019

Docker Installation

Detailed instructions are available at https://docs.docker.com/install/.

For installation on different OS, click on the corresponding link for details in the navigation bar on the left.

The highlighted instructions of Docker CE installation here are for host machines with one of following Ubuntu versions:

  • Bionic 18.04 (LTS),
  • Xenial 16.04 (LTS).

Uninstall old versions

Older versions of Docker were called docker or docker-engine. If these are installed, uninstall them

$ sudo apt remove docker docker-engine docker.io

It’s OK if apt reports that none of these packages are installed.

Install using the repository

Before installing Docker CE on a new host machine for the very first time, you need to set up the Docker repository. You may then install and update Docker from the repository

Set up the Repository

1.Update the apt package index:

$ sudo apt update

2.Install packages to allow apt to use a repository over HTTPS:

$ sudo apt 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.

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

Install Docker CE

1.Update the apt package index:

$ sudo apt update

2.Install the latest version of Docker CE:

$ sudo apt install docker-ce

3.Run the hello-world image to verify that Docker CE is installed correctly:

$ sudo docker run hello-world

Clone the code from git repository to a local directory, then mount into the Docker container

Clone the code from git repository to local

After Docker CE is successfully installed, clone the code in your own git repository to a local directory. (Follow the instruction on Assignment 1 and you should have a link of your own github repository)

$ mkdir git_repo_a1
$ cd git_repo_a1
$ git clone https://github.com/uw-stqam-w19/uw-stqam-w19-a1.git

Credentials for GitHub account is required to clone the code. After the cloning is finished, exit the current directory using cd ..

Pull the docker image

$ sudo docker pull uwstqam/uw-stqam:w19

Make sure to include the tag :w19 when pulling, since the newest version of docker would otherwise attempt to pull the image with the :latest tag as default.

Run $ sudo docker images to ensure the image is pulled successfully.

Mount the local directory into the docker container

Detailed information about managing data in Docker is available at https://docs.docker.com/storage/.

Now it is ready to mount the code cloned in your local directory into the docker container.

$ sudo docker run -d \
  -it \
  --name dev_a1 \
  -v "$(pwd)"/git_repo_a1:/home/stqam \
  uwstqam/uw-stqam:w19

In the command above, dev_a1 is the name defined for the running container (you may name it anything), whereas /git_repo_a1 is the source directory where you build the source code. You want the artifacts to be available to the container at /home/stqam, and the container would get access to a new build each time you build the source on the development host. The $(pwd) sub-command expands to the current working directory on Linux or macOS hosts.

Now you may use the following command to ensure that the mount was created correctly:

$ sudo docker inspect dev_a1

Search for the Mounts section, which would look like the following:

"Mounts": [
            {
                "Type": "bind",
                "Source": "/home/dliang2000/ECE453-STQAM/assign1/git_repo_a1",
                "Destination": "/home/stqam",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

Double check to ensure the mount shows the correct source and destination, RW is set to true, and the propagation is set to rprivate.

It is recommended to keep the container running when you are changing the code or having a new build in the source directory on the development host.

Execute inside the docker container

If you ever want to test the code inside the docker container, you may run the following command:

$ sudo docker exec -it dev_a1 sh
$ cd /home/stqam/uw-stqam-w19-a1

Then you could execute the tests using the commands provided in the assignment. Type in exit to exit the running container.

Commit the changes

After you think the work is all done in your local directory, save the work and do the following;

$ sudo docker commit dev_a1 NEW_IMAGE_NAME:NEW_TAG

You could then push this new image onto the Docker Hub if you want it to be stored on web (you need to create an account in https://hub.docker.com/ first).

Stop and remove the container

The commands below are provided when you decide to stop and remove the container. It is recommended to NOT use these commands when you still want to modify the code on your host.

$ sudo docker container stop dev_a1
$ sudo docker container rm dev_a1