×
Community Blog How to Install and Use Docker on Ubuntu 16.04

How to Install and Use Docker on Ubuntu 16.04

In this tutorial, we will explore how to install Docker on Alibaba Cloud and learn some important Docker commands to help you deploy and manage your container services.

By Hitesh Jethva, Alibaba Cloud Tech Share Author

Introduction

Docker is an Open Source project (container management service) for developers and system administrators to build, ship, and run distributed applications on laptops, data center VMs, cloud and ship them into containers which can then be deployed anywhere. Docker provides an automation of operating system level virtualization on Windows and Linux based operating system.
You can easily building blocks for deploying and scaling web apps, databases, and back-end services without depending on a particular stack using Docker.

Docker is made up from several components:
1) Docker for Linux: Allows us to run Docker containers on the Linux OS.
2) Docker Engine: Used for building Docker images and creating Docker containers.
3) Docker Hub: Used to store various Docker images.
4) Docker Compose: Used to define applications using multiple Docker containers.

Features:

• Docker Swarm provides a clustering solution for Docker containers that turns a group of Docker engines into a single, virtual Docker engine.
• Docker has the ability to reduce the size of development by providing a small footprint operating system.
• Docker containers are very lightweight and easily scalable.
• Provides easy and faster configuration and increase productivity.
• Provides containers that allow us to execute any kind of application in isolation environment.

In this tutorial, I will explain how to install Docker and explain some important Docker commands. I will also share some hands-on experience on how the commands are used and what they do.

Prerequisites

• A fresh Alibaba Cloud ECS instance with Ubuntu 16.04 installed.
• A root password is set up on the instance.

Installing Docker

Before starting, you will need to install the latest version of the Docker to your server. By default, the latest version of the Docker is not available in Ubuntu 16.04 repository. So you will need to add the official Docker repository to your server.

First, download and add the GPG key for the official Docker repository to the system with the following command:

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

Next, add the Docker repository to APT sources with the following command:

echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" | sudo tee -a /etc/apt/sources.list.d/docker.list

Next, update the repository and install Docker using the following command:

apt-get update -y
apt-get install  docker-ce -y

Once the Docker is installed, you can check the status of the Docker with the following command:

systemctl status docker

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2017-12-08 20:46:13 IST; 3min 2s ago
     Docs: https://docs.docker.com
 Main PID: 926 (dockerd)
    Tasks: 45
   Memory: 79.2M
      CPU: 7.463s
   CGroup: /system.slice/docker.service
           ├─ 926 /usr/bin/dockerd -H fd://
           ├─1453 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --st
           ├─2285 docker-containerd-shim e4202324477ae1870a77e12863ab3c79661cf8b7bfa617bb5709a816ea85194e /var/run/docker/libcontainerd/e420232
           └─2290 docker-containerd-shim 97bf4d277e0563906318bb276c3c65ebc274f28b6bb86cce80def795ecba76e2 /var/run/docker/libcontainerd/97bf4d2

Downloading Docker Image

First of all, you will need to pull a Docker image because containers are built using Docker image. There are many images already available on Docker website. You can find any image through a simple search command.

For example, to search Ubuntu 16.04 image, run the following command:

docker search ubuntu:16.04

You should see the following images available on Docker's website:

NAME                                           DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                         Ubuntu is a Debian-based Linux operating s...   6917                [OK]                
ubuntu-upstart                                 Upstart is an event-based replacement for ...   80                  [OK]                
ubuntu-debootstrap                             debootstrap --variant=minbase --components...   32                  [OK]                
mlaccetti/docker-oracle-java8-ubuntu-16.04     Oracle Java 8 on Ubuntu 16.04 LTS               4                                       [OK]
gocd/gocd-agent-ubuntu-16.04                   Docker GoCD agent for Ubuntu 16.04              2                                       
seresearch/opendavinci-ubuntu-16.04            Docker image with all Ubuntu 16.04 depende...   2                                       [OK]
proudmail/ubuntu-16.04                         Create ubuntu 16.04                             1                                       [OK]
superkumkum/ubuntu-16.04                       base image of ubuntu 16.04                      0                                       [OK]
gocdexperimental/gocd-agent-ubuntu-16.04       Experimental GoCD Agent - Ubuntu 16.04          0                                       
dokken/ubuntu-16.04                            For use with kitchen-dokken, Base image pl...   0                                       
keithf/ubuntu-16.04-armhf                      Ubuntu 16.04 for ARM-based devices.             0                                       
neoncluster/ubuntu-16.04                       Ubuntu 16.04 image with recent package upg...   0                                       
cdbishop89/docker-ubuntu-16.04                 Base Ubuntu 16.04 Image                         0                                       [OK]
vlex/ubuntu-16.04-node                         Ubuntu 16.04 with node 6.1.0                    0                                       
seresearch/opendavinci-ubuntu-16.04-complete                                                   0                                       
itsspeed/ubuntu-16.04-python                   Base Ubuntu 16.04 image with python and pi...   0                                       
syseleven/ubuntu-16.04-puppet4                 ubuntu-16.04-puppet4                            0                                       [OK]
opencpu/ubuntu-16.04                           OpenCPU Server builds for Ubuntu 16.04 (Xe...   0                                       [OK]
lgong/mml-ubuntu-16.04                                                                         0                                       
stafory/ubuntu-16.04-ci                                                                        0                                       
kensenshi/ubuntu-16.04-oracle-java8-ant        Oracle Java 8 and ANT on Ubuntu 16.04.          0                                       
naritadev/narita-ubuntu-16.04                  Narita Ubuntu 16.04 Distro                      0                                       
addle/ubuntu-16.04                             Ubuntu 16.04 LTS (Xenial Xerus)                 0                                       
uberr2000/ink-ubuntu-16.04                     ubuntu 16.04 with nginx ,phalcon ,mysql,ph...   0                                       
syseleven/ubuntu-16.04-puppet5                 ubuntu-16.04-puppet5                            0                                       [OK]

Next, Download the Ubuntu-16.04 base image from above listed images:

docker pull ubuntu

The output looks something like this:

Using default tag: latest
latest: Pulling from library/ubuntu
660c48dd555d: Pull complete 
4c7380416e78: Pull complete 
421e436b5f80: Pull complete 
e4ce6c3651b3: Pull complete 
be588e74bd34: Pull complete 
Digest: sha256:7c67a2206d3c04703e5c23518707bdd4916c057562dd51c74b99b2ba26af0f79
Status: Downloaded newer image for ubuntu:latest

Once download has been finished, you can list all available images on your system by running the following command:

docker images

Output:

REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
ubuntu                       latest              20c44cd7596f        3 weeks ago         123MB

Launching Docker Container

Now, to setup a basic ubuntu-16.04 container with a bash shell, you just need to run one command.

docker run -i -t ubuntu /bin/bash

You should see the following output:

root@0b775c3f606d:/#

Now, after Ubuntu base image with instance is ready, you can easily install Apache Server interactively for it. To do so, you will need to run following command in a terminal.

apt-get update -y
apt-get install apache2 apache2-utils -y

You are now using a bash shell inside of an Ubuntu Docker container. To disconnect, or detach, from the shell without exiting use the escape sequence Ctrl-p + Ctrl-q.

Now, you can save the changes you made in the Ubuntu instance. To do that, first you will need the Container ID of running Ubuntu instance. To get that, run:

docker ps

You should see the following output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
0b775c3f606d        ubuntu              "/bin/bash"              36 minutes ago      Up 36 minutes                               dreamy_murdock

Now, save the changes as a new image with the name ubuntu-apache by running the following command:

docker commit 0b775c3f606d ubuntu-apache

sha256:6997db13c6587d1403c2360515d29028a30de786101c4ec26faddc505f5242c4

You can see that the changes are saved using Container ID and image name ubuntu-apache.
To verify new image is running or not, run:

docker images

REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
ubuntu-apache                latest              6997db13c658        30 seconds ago      261MB
ubuntu                       latest              20c44cd7596f        3 weeks ago         123MB

Launching Application on Docker

Now, you have a new image that contains an Apache Web server. You can build a Dockerfile based on that image and add the necessary files. Given the relative path to a tarball of the site content, Docker automatically untars or unzips the files in a source tar or zip file into the target directory.

To do this, you need to create an index.html file on the host system and add it to a tarball called application.tar in current directory:

mkdir application
nano application/index.html

Add the following lines:

<html>
Sample Apache Web Page
</html>

Save and close the file.

Now, compress website directory using tar:

tar -cvf application.tar application

Now, create Dockerfile to add the web site content to the ubuntu-apache image and launch Apache on port 80:

nano Dockerfile

Add the following lines:

FROM ubuntu-apache
ADD application.tar /tmp/
RUN mv /tmp/application/* /var/www/html/
EXPOSE 80
ENTRYPOINT [ "/usr/sbin/apache2ctl" ]
CMD [ "-D", "FOREGROUND" ]

Save and close the file.

In the above Dockerfile, the website content in website.tar will get automatically extracted to /tmp/ folder. Then, the entire site will move to the Apache root directory /var/www/html/ and the expose 80 will open port 80 so that the website will be available normally. Then, the entry point is set to /usr/sbin/apache2 so that the Apache Server will execute.

Now, you will build Container using the Dockerfile you just created in order to add website on it.

To do this, run the following command:

docker build -t application .

You should see the following output:

Sending build context to Docker daemon  71.68kB
Step 1/6 : FROM ubuntu-apache
 ---> 6997db13c658
Step 2/6 : ADD application.tar /tmp/
 ---> 9b20f474c3a0
Step 3/6 : RUN mv /tmp/application/* /var/www/html/
 ---> Running in 6f0b4a29cc8a
 ---> a5ffa4710b58
Removing intermediate container 6f0b4a29cc8a
Step 4/6 : EXPOSE 80
 ---> Running in e4ef5dfc0838
 ---> a584d0068bb6
Removing intermediate container e4ef5dfc0838
Step 5/6 : ENTRYPOINT /usr/sbin/apache2ctl
 ---> Running in ab5bda3f593a
 ---> 65f187a34ce6
Removing intermediate container ab5bda3f593a
Step 6/6 : CMD -D FOREGROUND
 ---> Running in b8ddd1d006b5
 ---> 354383fb2666
Removing intermediate container b8ddd1d006b5
Successfully built 354383fb2666
Successfully tagged application:latest

After an image has been built, you can now proceed by creating a container running Apache instance on it.

docker run -d -P application

Output:
a749b23fb87c821b69479353bf067f2b1af53fd0fd300ad86ac5c028dd5dcbde

Now, use the "docker ps" command to determine the port activated and then use curl to inspect the sample content.

docker ps

You should see the following output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
a749b23fb87c        application         "/usr/sbin/apache2..."   30 seconds ago      Up 28 seconds       0.0.0.0:32768->80/tcp   nifty_bose

Now, verify your Apache Web server by running the following command:

curl localhost:32768

Or

curl "Container IP Address":80

You should see the Apache Web page you have created earlier:

<html>
Sample Apache Web Page
</html>

Working with Docker Command

Let's start by seeing all available commands Dockers have. You can list all available Docker commands by running the following command:

docker

To check Docker version, run:

docker version

Output:

Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:42:18 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:56 2017
 OS/Arch:      linux/amd64
 Experimental: false

To check system-wide information on Docker, run:

docker info

You should see the following output:

Containers: 2
 Running: 1
 Paused: 0
 Stopped: 1
Images: 2
Server Version: 17.09.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog

To list all the running containers, run:

docker ps

To list the latest container you created, run:

docker ps -l

To list both running and non-running containers, run:

docker ps -a

To start and stop container's process, run:

`docker start "Container ID"
docker stop "Container ID"`

To stop all running containers, run:

docker stop $(docker ps -a -q)

Note: You can find Container ID using sudo docker ps command.

If we want to attach into a running container, Docker allows you to interact with running containers using the attach command:

docker attach "Container ID"

You can check every information about a Docker Container using the inspect command with container ID.

docker inspect "Container ID"

To delete a single container, run:

docker rm "Container ID"

To delete all existing containers, run:

docker rm $(docker ps -a -q)

Note: Before deleting any container you will need to stop it first.

To delete a single image, you can use rmi command with image ID. The image id can be fetched using the command "docker images":

docker rmi "Image ID"

To delete all existing images, run:

docker rmi $(docker images -q -a)

If you run Docker container as a daemon then it may be useful to know what appears on the console output of the running container.

The Docker logs command retrieves logs present at the time of execution.

You can use Docker log command with container ID.

docker log -f "container ID"

Conclusion

Congratulations! You have successfully installed and run Docker. I hope you have now enough knowledge to install and use Docker container in a production environment. For more information on Docker, you can refer the official Docker documentation page. You can also find other tutorials on the Alibaba Cloud Getting Started channel.

1 2 0
Share on

Alibaba Clouder

2,599 posts | 758 followers

You may also like

Comments

Raja_KT November 17, 2018 at 2:09 pm

I am using Ubuntu 18.10 in local machine. Hope there are no hiccups when we are in Alibaba Cloud ECS