How to install and use Docker on Ubuntu 16.04

Introduction

Docker is an application that makes it easy to run application processes in a container, just like a virtual machine, but more portable, more resource-friendly, and more dependent on the host operating system.

There are two ways to install Docker on Ubuntu 16.04. One way is to install it on an existing installation of the operating system. The other involves using a tool to start the server, which automatically installs Docker on it.

In this tutorial, you will learn how to install and use it on an existing Ubuntu 16.04 installation.

Preparation

To follow this tutorial, you need to have the following conditions:

**Note: ** Docker requires a 64-bit version and Ubuntu with a kernel version equal to or greater than 3.10. The default 64-bit Ubuntu 16.04 server meets these requirements.

All commands in this tutorial should be run as a non-root user. If the command requires root access, it will have sudo in front. [Ubuntu 16.04 initial installation tutorial] (https://cloud.tencent.com/developer/article/1007167?from=10680) describes how to add users and provide them with sudo access.

Step 1-Install Docker

The Docker installation package provided in the official Ubuntu 16.04 repository may not be the latest version. To get the latest and greatest version, install Docker from the official Docker repository. This section will show you how to do this.

First, add the GPG key of the official Docker repository to the system:

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

Add the Docker repository to the APT source:

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

Next, update the package database with the Docker package in the newly added repo:

sudo apt-get update

Make sure you want to install from the Docker repo instead of the default Ubuntu 16.04 repo:

apt-cache policy docker-ce

You should see output similar to the following:

docker-ce:
 Installed:(none)
 Candidate:17.03.1~ce-0~ubuntu-xenial
 Version table:17.03.1~ce-0~ubuntu-xenial 500500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
  17.03.0~ ce-0~ubuntu-xenial 500500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages

Note that docker-ce is not installed, but the candidate for installation is from the Docker repository of Ubuntu 16.04. The docker-ce version number may be different.

Finally, install Docker:

sudo apt-get install -y docker-ce

Docker should now be installed, the daemon started, and the process startup process should be enabled. Check if it is running:

sudo systemctl status docker

The output should be similar to the following, indicating that the service is active and running:

● docker.service - Docker Application Container Engine
 Loaded:loaded(/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 Active:active(running) since Sun 2016-05-0106:53:52 CDT;1 weeks 3 days ago
  Docs: https://docs.docker.com
 Main PID:749(docker)

Installing Docker now can not only provide you with Docker service (daemon), but also provide you with a docker command line utility or Docker client. We will discuss how to use the docker command later in this tutorial.

Step 2-Execute Docker commands without Sudo (optional)

By default, running the docker command requires root privileges-that is, you must add sudo before the command. It can also be run by a user in the docker group, which is automatically created during Docker installation. If you try to run the docker command without using sudo or without prefixing it in the docker group, you will get the following output:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

If you want sudo to avoid typing when running the docker command, please add the user name to the docker group:

sudo usermod -aG docker ${USER}

To apply the new group membership, you can log out of the server and log in again, or you can enter the following:

su - ${USER}

You will be prompted to enter the user password to continue. After that, you can confirm that your user is now added to the docker group by typing the following:

id -nG

The output should look like this:

sammy sudo docker

If you need to add a user to the docker group that you are not logged in, please use the following method to clearly declare the user name:

sudo usermod -aG docker username

The rest of this article assumes that you are running the docker command as a user in the docker user group. If you do not choose to do this, please add the command sudo in front.

Step 3-Use Docker commands

With Docker installed and working, it's time to get familiar with the command line utilities. Use docker and pass a series of options and commands, followed by parameters. The syntax takes the following form:

docker [option][command][arguments]

To view all available subcommands, enter:

docker

Starting with Docker 1.11.1, the complete list of available subcommands includes:

 attach    Attach to a running container
 build     Build an image from a Dockerfile
 commit    Create a newimagefrom a container's changes
 cp        Copy files/folders between a container and the local filesystem
 create    Create a newcontainer
 diff      Inspect changes on a container's filesystem
 events    Get real time events from the server
 exec      Run a command in a running container
 export    Export a container's filesystem as a tar archive
 history   Show the history of an image
 images    List images
 import    Import the contents from a tarball to create a filesystem image
 info      Display system-wide information
 inspect   Return low-level information on a container or image
 kill      Kill a running container
 load      Load an image from a tar archive or STDIN
 login     Log in to a Docker registry
 logout    Log out from a Docker registry
 logs      Fetch the logs of a container
 network   Manage Docker networks
 pause     Pause all processes within a container
 port      List port mappings or a specific mapping for the CONTAINER
 ps        List containers
 pull      Pull an image or a repository from a registry
 push      Push an image or a repository to a registry
 rename    Rename a container
 restart   Restart a container
 rm        Remove one or more containers
 rmi       Remove one or more images
 run       Run a command in a newcontainer
 save      Save one or more images to a tar archive
 search    Search the Docker Hub for images
 start     Start one or more stopped containers
 stats     Display a live stream ofcontainer(s) resource usage statistics
 stop      Stop a running container
 tag       Tag an image into a repository
 top       Display the running processes of a container
 unpause   Unpause all processes within a container
 update    Update configuration of one or more containers
 version   Show the Docker version information
 volume    Manage Docker volumes
 wait      Block until a container stops, then print its exit code

To view the switches available for a specific command, enter:

docker docker-subcommand --help

To view system-wide information about Docker, use:

docker info

Step 4-Use Docker image

The Docker container runs from the Docker image. By default, it fetches these images from Docker Hub, which is a Docker registry managed by Docker, the company behind the Docker project. Anyone can build and host their Docker images on Docker Hub, so most of the applications and Linux distributions required to run Docker containers have images hosted on Docker Hub.

To check if you can access and download images from Docker Hub, enter:

docker run hello-world

The output should include the following and should indicate that Docker is working properly:

Hello from Docker.
This message shows that your installation appears to be working correctly....

You can use the search command with the docker command to search for images available on Docker Hub. For example, to search for Ubuntu images, enter:

docker search ubuntu

The script will crawl Docker Hub and return a list of all images whose names match the search string. In this case, the output will be similar to:

NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                            Ubuntu is a Debian-based Linux operating s...3808[OK]       
ubuntu-upstart                    Upstart is an event-based replacement for...61[OK]       
torusware/speedus-ubuntu          Always updated official Ubuntu docker imag...25[OK]
rastasheep/ubuntu-sshd            Dockerized SSH service, built on top ofof...24[OK]
ubuntu-debootstrap                debootstrap --variant=minbase --components...23[OK]       
nickistre/ubuntu-lamp             LAMP server on Ubuntu                           6[OK]
nickistre/ubuntu-lamp-wordpress   LAMP on Ubuntu with wp-cli installed            5[OK]
nuagebec/ubuntu                   Simple always updated Ubuntu docker images...4[OK]
nimmis/ubuntu                     This is a docker images different LTS vers...4[OK]
maxexcloo/ubuntu                  Docker base image built on Ubuntu with Sup...2[OK]
admiringworm/ubuntu               Base ubuntu images based on the official u...1[OK]
​
...

In the OFFICIAL column, OK represents an image built and supported by the company behind the project. Once you have determined the image you want to use, you can use the pull subcommand to download it to your computer, as shown below:

docker pull ubuntu

After downloading the image, you can run the container using the downloaded image with the run subcommand. If the image is not downloaded when docker is executed with the run subcommand, the Docker client will first download the image and then use it to run the container:

docker run ubuntu

To view the images that have been downloaded to the computer, enter:

docker images

The output should be similar to the following:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              c5f1cf30c96b        7 days ago          120.8 MB
hello-world         latest              94df4f0ce8a4        2 weeks ago         967 B

As you will see later in this tutorial, the image used to run the container can be modified and used to generate a new image, which can then be uploaded (push is the technical term) to Docker Hub or other Docker registry.

Step 5-Run the Docker container

Before you run the hello-world container to run and exit, an example of the container after the test message is illuminated. However, containers are more useful than this, they can be interactive. After all, they are similar to virtual machines, but more resource-friendly.

As an example, let's run a container using the latest image of Ubuntu. The combination of -i and -t switches gives you interactive shell access to the container:

docker run -it ubuntu

Your command prompt should change to reflect the fact that you are now working in the container, and should take the following form:

root@d9b100f2f636:/#

**Important: **Please note the container ID in the command prompt. In the example above, it is d9b100f2f636.

Now you can run any command inside the container. For example, let's update the package database inside the container. There is no need to prefix any commands with sudo, because you are operating in a container with root privileges:

apt-get update

Then install any applications in it. For example, let's install NodeJS.

apt-get install -y nodejs

Step 6-Submit the changes in the container to the Docker image

When you start the Docker image, you can create, modify and delete files just like using a virtual machine. The changes you made only apply to this container. You can start and stop it, but once you destroy it with the docker rm command, the changes will be lost forever.

This section describes how to save the state of the container as a new Docker image.

After installing nodejs in an Ubuntu container, you now have a container running an image, but the container is different from the image you used to create it.

To save the state of the container as a new image, exit it first:

exit

Then use the following command to commit the changes to the new Docker image instance. The -m switch is the submission information, which can help you and others know the changes you made, while the -a is used to specify the author. When you start an interactive session, the container ID is the container ID you mentioned earlier in this tutorial. Unless you have created other repositories on Docker Hub, the repository is usually your Docker Hub username:

docker commit -m "What did you do to the image"-a "Author Name" container-id repository/new_image_name

E.g:

docker commit -m "added node.js"-a "Sunday Ogwu-Chinuwa" d9b100f2f636 finid/ubuntu-nodejs

**Note: **When you submit the new image is saved locally, that is, on your computer. Later in this tutorial, you will learn how to push an image to a Docker registry such as Docker Hub so that you and others can evaluate and use it.

After completing the operation, immediately listing the Docker image on the computer should show the new image and the old image derived from it:

docker images

The output should be similar to:

finid/ubuntu-nodejs       latest              62359544c9ba        50 seconds ago      206.6 MB
ubuntu              latest              c5f1cf30c96b        7 days ago          120.8 MB
hello-world         latest              94df4f0ce8a4        2 weeks ago         967 B

In the above example, ubuntu-nodejs is the new image, which is derived from the existing ubuntu image of Docker Hub. The size difference reflects the changes made. In this example, the change is that NodeJS is already installed. Therefore, next time you need to run a container with Ubuntu with NodeJS pre-installed, you can use the new image. The image can also be built from a so-called Dockerfile. But this is a very complicated process that is beyond the scope of this article.

Step 7-List Docker containers

After using Docker for a while, you will have many active (running) and inactive containers on your computer. To view active, use:

docker ps

You will see output similar to the following:

CONTAINER ID   IMAGE     COMMAND       CREATED      STATUS      PORTS         NAMES
f7c79cc556dd  ubuntu    "/bin/bash"3 hours ago  Up 3 hours               silly_spence

To see all containers-active and inactive, pass it to the -a switch:

docker ps -a

To see the latest container you created, pass it to the -l switch:

docker ps -l

Stopping a running or active container is as simple as typing:

docker stop container-id

The container-id can be found in the output of the docker ps command.

Step 8-Push the Docker image to the Docker repository

The next logical step after creating a new image from an existing image is to share it with a few of your choices, the entire area on Docker Hub, or other Docker registries you can access. To push an image to Docker Hub or any other Docker registry, you must have an account there.

This section describes how to push Docker images to Docker Hub.

To create an account with Docker Hub, register at Docker Hub. Then, in order to push your image, first log in to Docker Hub. You will be prompted for identity verification:

docker login -u docker-registry-username

If you specified the correct password, authentication should be successful. Then you can use the following methods to push your own images

docker push docker-registry-username/docker-image-name

It will take some time to complete, and when complete, the output will be similar to the following:

The push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: Pushed
5 f70bf18a086: Pushed
a3b5c80a4eba: Pushed
7 f18b442972b: Pushed
3 ce512daaf78: Pushed
7 aae4540b42d: Pushed

...

After pushing the image to the registry, it should be listed on the dashboard of your account as shown in the image below.

If the push attempt results in this type of error, you may not be logged in:

The push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: Preparing
5 f70bf18a086: Preparing
a3b5c80a4eba: Preparing
7 f18b442972b: Preparing
3 ce512daaf78: Preparing
7 aae4540b42d: Waiting
unauthorized: authentication required

Log in and repeat the push attempt.

in conclusion

Docker is much more than what is given in this article, but it is enough to get you started using it on Ubuntu 16.04. Like most open source projects, Docker is built from a rapidly developing code base, so visit the project blog page for the latest information.

See also Other Docker tutorials in the DO community.

To learn more about Linux open source information tutorial, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.

Reference: "How To Install and Use Docker on Ubuntu 16.04"

Recommended Posts

How to install and use Docker on Ubuntu 20.04
How to install and use Docker on Ubuntu 16.04
How to install and use Docker on CentOS 7
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install and use MySQL Workbench on Ubuntu 18.04
How to install Docker Compose on Ubuntu 18.04
How to install Prometheus with Docker on Ubuntu 14.04
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
How to install and configure NATS on Ubuntu 16.04
How to install and configure Gogs on Ubuntu 18.04
How to install and configure Cyberpanel on Ubuntu 18.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure Ansible on Ubuntu 18.04
How to install and use Composer on CentOS 8
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure Elasticsearch on Ubuntu 16.04
How to install and configure PostGIS on Ubuntu 14.04
How to install and configure Sphinx on Ubuntu 16.04
How to install and use Curl on CentOS 8
How to install and configure PostGIS on Ubuntu 14.04
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04