Introduction
Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system. For a detailed introduction to the different components of a Docker container, check out The Docker Ecosystem: An Introduction to Common Components.
There are two methods for installing Docker on Ubuntu 16.04. One method involves installing it on an existing installation of the operating system. The other involves spinning up a server with a tool called Docker Machine that auto-installs Docker on it.
In this tutorial, you'll learn how to install and use it on an existing installation of Ubuntu 16.04.
Prerequisites
To follow this tutorial, you will need the following:
- 64-bit Ubuntu 16.04 server
- Non-root user with sudo privileges Initial Setup Guide for Ubuntu 16.04 explains how to set this up.)
Note: Docker requires a 64-bit version of Ubuntu as well as a kernel version equal to or greater than 3.10. The default 64-bit Ubuntu 16.04 server meets these requirements.
All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudo
. Initial Setup Guide for Ubuntu 16.04 explains how to add users and give them sudo access.
Step 1 — Installing Docker
The Docker installation package available 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 shows you how to do just that.
First, add the GPG key for 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 APT sources:
- 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 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 16.04 repo:
- apt-cache policy docker-ce
You should see output similar to the follow:
docker-ce:
Installed: (none)
Candidate: 17.03.1~ce-0~ubuntu-xenial
Version table:
17.03.1~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.03.0~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
Notice that docker-ce
is not installed, but the candidate for installation is from the Docker repository for Ubuntu 16.04. The docker-ce
version number might be different.
Finally, install Docker:
- sudo apt-get install -y docker-ce
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it's running:
- sudo systemctl status docker
The output should be similar to the following, showing 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-01 06:53:52 CDT; 1 weeks 3 days ago
Docs: https://docs.docker.com
Main PID: 749 (docker)
Installing Docker now gives you not just the Docker service (daemon) but also the docker
command line utility, or the Docker client. We'll explore how to use the docker
command later in this tutorial.
Step 2 — Executing the Docker Command Without Sudo (Optional)
By default, running the docker
command requires root privileges — that is, you have to prefix the command with sudo
. It can also be run by a user in the docker group, which is automatically created during the installation of Docker. If you attempt to run the docker
command without prefixing it with sudo
or without being in the docker group, you'll get an output like this:
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
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, you can log out of the server and back in, or you can type the following:
- su - ${USER}
You will be prompted to enter your user's password to continue. Afterwards, you can confirm that your user is now added to the docker
group by typing:
- id -nG
sammy 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
The rest of this article assumes you are running the docker
command as a user in the docker user group. If you choose not to, please prepend the commands with sudo
.
Step 3 — Using the Docker Command
With Docker installed and working, now's the time to become familiar with the command line utility. Using docker
consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:
- docker [option] [command] [arguments]
To view all available subcommands, type:
- docker
As of 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 new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
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 new container
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 of container(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 to a specific command, type:
- docker docker-subcommand --help
To view system-wide information about Docker, use:
- docker info
Step 4 — Working with Docker Images
Docker containers are run from Docker images. By default, it pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anybody can build and host their Docker images on Docker Hub, so most applications and Linux distributions you'll need to run Docker containers have images that are hosted on Docker Hub.
To check whether you can access and download images from Docker Hub, type:
- docker run hello-world
The output, which should include the following, should indicate that Docker in working correctly:
Hello from Docker.
This message shows that your installation appears to be working correctly.
...
You can search for images available on Docker Hub by using the docker
command with the search
subcommand. For example, to search for the Ubuntu image, type:
- docker search ubuntu
The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:
- 185 Пользователи нашли это полезным