How to install and use docker on ubuntu 16



Docker is an application that makes it simple and easy khổng lồ run application processes in a container, which are lượt thích virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system. For a detailed introduction to lớn the different components of a Docker container, kiểm tra out The Docker Ecosystem: An Introduction khổng lồ Common Components.

Bạn đang xem: How to install and use docker on ubuntu 16

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 vps with a tool called Docker Machine that auto-installs Docker on it.

In this tutorial, you’ll learn how khổng lồ install và use it on an existing installation of Ubuntu 16.04.


To follow this tutorial, you will need the following:

Step 1 — Installing Docker

The Docker installation package available in the official Ubuntu 16.04 repository may not be the latest version. Lớn get this latest version, install Docker from the official Docker repository. This section shows you how to vày just that.

First, in order to lớn ensure the downloads are valid, showroom the GPG key for the official Docker repository to lớn your system:

curl -fsSL | sudo apt-key showroom -Add the Docker repository to APT sources:

sudo add-apt-repository "deb $(lsb_release -cs) stable"Next, update the package database with the Docker packages from the newly added repo:

sudo apt-get updateMake sure you are about to install from the Docker repo instead of the mặc định Ubuntu 16.04 repo:

apt-cache policy docker-ceYou should see đầu ra similar khổng lồ the follow:

Output of apt-cache policy docker-ce

docker-ce: Installed: (none) Candidate: 18.06.1~ce~3-0~ubuntu Version table: 18.06.1~ce~3-0~ubuntu 500 500 xenial/stable amd64 PackagesNotice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 16.04 (xenial).

Finally, install Docker:

sudo apt-get install -y docker-ceDocker should now be installed, the daemon started, và the process enabled to lớn start on boot. Check that it’s running:

sudo systemctl status dockerThe output should be similar khổng lồ the following, showing that the service is active và running:


Step 2 — Executing the Docker Command Without Sudo (Optional)

By default, running the docker command requires root privileges — that is, you have lớn 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 lớn 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 lớn avoid typing sudo whenever you run the docker command, add your username khổng lồ the docker group:

sudo usermod -aG docker $USERTo apply the new group membership, you can log out of the server & back in, or you can type the following:

su - $USERYou will be prompted khổng lồ enter your user’s password lớn continue. Afterwards, you can confirm that your user is now added khổng lồ the docker group by typing:

id -nG
sammy sudo dockerIf 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 usernameThe 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 và working, now’s the time to lớn 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:


dockerAs of Docker 18.06.1, the complete danh mục of available subcommands includes:

attach Attach local standard input, output, và error streams 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 to files or directories on a container"s filesystem events Get real time events from the hệ thống 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 khổng lồ create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to lớn a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port danh mục 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 lớn a registry rename Rename a container restart Restart one or more containers 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 lớn a tar archive (streamed lớn STDOUT by default) tìm kiếm 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 one or more running containers tag Create a tag TARGET_IMAGE that refers khổng lồ SOURCE_IMAGE đứng top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codesTo view the switches available khổng lồ a specific command, type:

docker docker-subcommand --helpTo 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 kiểm tra whether you can access and download images from Docker Hub, type:

docker run hello-worldIn the output, you should see the following message, which indicates that Docker is working correctly:

...Hello from Docker!This message shows that your installation appears khổng lồ be working correctly....You can tìm kiếm 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 ubuntuThe script will crawl Docker Hub and return a listing of all images whose name matches the tìm kiếm string. In this case, the output will be similar to this:

NAME description STARS OFFICIAL AUTOMATEDubuntu Ubuntu is a Debian-based Linux operating sys… 8564 dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server và NoVNC 230 rastasheep/ubuntu-sshd Dockerized SSH service, built on đứng top of offi… 176 consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 129 ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 95 ubuntu-upstart Upstart is an event-based replacement for th… 91 neurodebian NeuroDebian provides neuroscience research s… 54 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 48 ubuntu-debootstrap debootstrap --variant=minbase --components=m… 39 nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 tutum/ubuntu Simple Ubuntu docker images with SSH access 18 i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 14 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 12 eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 6 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 6 codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 4 darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4 pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 2 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 smartentry/ubuntu ubuntu with smartentry 1 ossobv/ubuntu Custom ubuntu image from scratch (based on o… 0 paasmule/bosh-tools-ubuntu Ubuntu based bosh-cli 0 1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0 In the OFFICIAL column, OK indicates an image built and supported by the company behind the project. Once you’ve identified the image that you would lượt thích to use, you can download it to your computer using the pull subcommand. Try this with the ubuntu image, like so:

docker pull ubuntuAfter an image has been downloaded, you may then run a container using the downloaded image with the run subcommand. If an image has not been downloaded when docker is executed with the run subcommand, the Docker client will first tải về the image, then run a container using it:

docker run ubuntuTo see the images that have been downloaded to lớn your computer, type:

docker imagesThe đầu ra should look similar to the following:

REPOSITORY TAG IMAGE ID CREATED SIZEubuntu latest ea4c82dcd15a 16 hours ago 85.8MBhello-world latest 4ab4c602aa5e 5 weeks ago 1.84kBAs you’ll see later in this tutorial, images that you use lớn run containers can be modified & used lớn generate new images, which may then be uploaded (pushed is the technical term) lớn Docker Hub or other Docker registries.

Step 5 — Running a Docker Container

The hello-world container you ran in the previous step is an example of a container that runs & exits after emitting a test message. Containers can be much more useful than that, & they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.

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

docker run -it ubuntu
Note: The default behavior for the run command is khổng lồ start a new container. Once you run the preceding the command, you will mở cửa up the shell interface of a second ubuntu container.

Your command prompt should change to lớn reflect the fact that you’re now working inside the container và should take this form:

Note: Remember the container id in the command prompt. In the preceding example, it is 9b0db8a30ad1. You’ll need that container ID later khổng lồ identify the container when you want lớn remove it.

Now you can run any command inside the container. For example, let’s update the package database inside the container. You don’t need to lớn prefix any command with sudo, because you’re operating inside the container as the root user:

apt-get updateThen install any application in it. Let’s install Node.js:

apt-get install -y nodejsThis installs Node.js in the container from the official Ubuntu repository. When the installation finishes, verify that Node.js is installed:

node -vYou’ll see the version number displayed in your terminal:

v8.10.0Any changes you make inside the container only apply lớn that container.

To exit the container, type exit at the prompt.

Let’s look at managing the containers on our system next.

Step 6 — Managing Docker Containers

After using Docker for a while, you’ll have many active (running) & inactive containers on your computer. To view the active ones, use:

docker psYou will see output đầu ra similar to lớn the following:

CONTAINER ID IMAGE COMMAND CREATED In this tutorial, you started three containers; one from the hello-world image và two from the ubuntu image. These containers are no longer running, but they still exist on your system.

To view all containers — active and inactive — run docker ps with the -a switch:

docker ps -aYou’ll see output similar khổng lồ this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9b0db8a30ad1 ubuntu "/bin/bash" 21 minutes ago Exited (0) About a minute ago xenodochial_neumannd7851eb12e23 ubuntu "/bin/bash" 24 minutes ago Exited (0) 24 minutes ago boring_chebyshevd54945b6510b hello-world "/hello" 32 minutes ago Exited (0) 32 minutes ago youthful_roentgenTo view the latest container you created, pass it the -l switch:

docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9b0db8a30ad1 ubuntu "/bin/bash" 22 minutes ago Exited (127) About a minute ago xenodochial_neumannTo start a stopped container, use docker start, followed by the container ID or the container’s name. Let’s start the Ubuntu-based container with the ID of 9b0db8a30ad1 :

docker start 9b0db8a30ad1 The container will start, and you can use docker ps to lớn see its status:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9b0db8a30ad1 ubuntu "/bin/bash" 23 minutes ago Up 11 seconds xenodochial_neumannTo stop a running container, use docker stop, followed by the container ID or name. This time, we’ll use the name that Docker assigned the container, which is xenodochial_neumann:

docker stop xenodochial_neumannOnce you’ve decided you no longer need a container anymore, remove it with the docker rm command, again using either the container ID or the name. Use the docker ps -a command khổng lồ find the container ID or name for the container associated with the hello-world image & remove it.

docker rm youthful_roentgenYou can start a new container và give it a name using the --name switch. You can also use the --rm switch to lớn create a container that removes itself when it’s stopped. See the docker run help command for more information on these options & others.

Xem thêm: Nguyên Nhân Ổ Cứng Bị Đầy Không Rõ Nguyên Nhân Và Cách Khắc Phục

Containers can be turned into images which you can use lớn build new containers. Let’s look at how that works.

Step 7 — Committing Changes in a Container khổng lồ a Docker Image

When you start up a Docker image, you can create, modify, & delete files just like you can with a virtual machine. The changes that you make will only apply to that container. You can start và stop it, but once you destroy it with the docker rm command, the changes will be lost for good.

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

After installing Node.js inside the Ubuntu container, you now have a container running off an image, but the container is different from the image you used lớn create it. But you might want to lớn reuse this Node.js container as the basis for new images later.

To vày this, commit the changes khổng lồ a new Docker image instance using the following command structure:

docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_nameThe -m switch is for the commit message that helps you & others know what changes you made, while -a is used to lớn specify the author. The container ID is the one you noted earlier in the tutorial when you started the interactive Docker session. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username.

For example, for the user sammy, with the container ID of d9b100f2f636, the command would be:

docker commit -m "added node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs
Note: When you commit an image, the new image is saved locally, that is, on your computer. Later in this tutorial, you’ll learn how khổng lồ push an image to lớn a Docker registry lượt thích Docker Hub so that it can be assessed and used by you & others.

After that operation is completed, listing the Docker images now on your computer should show the new image, as well as the old one that it was derived from:

docker imagesThe output đầu ra should be similar to lớn this:

REPOSITORY TAG IMAGE ID CREATED SIZEsammy/ubuntu-nodejs latest 6a1784a63edf 2 minutes ago 170MBubuntu latest ea4c82dcd15a 17 hours ago 85.8MBhello-world latest 4ab4c602aa5e 5 weeks ago 1.84kBIn the above example, ubuntu-nodejs is the new image, which was derived from the existing ubuntu image from Docker Hub. The form size difference reflects the changes that were made. In this example, the change was that Node.js was installed. Next time you need to run a container using Ubuntu with Node.js pre-installed, you can just use the new image.

You can also build images from a Dockerfile, which lets you automate the installation of software in a new image. However, that’s outside the scope of this tutorial.

Now let’s share the new image with others so they can create containers from it.

Step 8 — Pushing Docker Images to a Docker Repository

The next logical step after creating a new image from an existing image is to cốt truyện it with a select few of your friends, the whole world on Docker Hub, or another Docker registry that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an tài khoản there.

This section shows you how to push a Docker image to lớn Docker Hub. To learn how lớn create your own private Docker registry, check out How lớn Set Up a Private Docker Registry on Ubuntu 14.04.

To push your image, first log into Docker Hub:

docker login -u docker-registry-usernameYou’ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed.

Note: If your Docker registry username is different from the local username you used khổng lồ create the image, you will have to lớn tag your image with your registry username. For the example given in the last step, you would type:

docker tag sammy/ubuntu-nodejs docker-registry-username/ubuntu-nodejs
Then you can push your own image using:

docker push docker-registry-username/ubuntu-nodejsTo push the ubuntu-nodejs image lớn the sammy repository, the command would be:

docker push sammy/ubuntu-nodejsThe process may take some time to lớn complete as it uploads the images, but when completed, the output will look lượt thích this:

The push refers to repository 1aa927602b6a: Pushed76c033092e10: Pushed2146d867acf3: Pushedae1f631f14b7: Pushed102645f1cf72: Pushedlatest: digest: sha256:2be90a210910f60f74f433350185feadbbdaca0d050d97181bf593dd85195f06 size: 1362After pushing an image lớn a registry, it should be listed on your account’s dashboard, lượt thích that shown in the image below.


If a push attempt results in the following error, it is likely that you are not logged in:

The push refers lớn a repository e3fbbfb44187: Preparing5f70bf18a086: Preparinga3b5c80a4eba: Preparing7f18b442972b: Preparing3ce512daaf78: Preparing7aae4540b42d: Waitingunauthorized: authentication requiredLog in, then repeat the push attempt.


In this tutorial, you’ve learned the basics to lớn get you started working with Docker on Ubuntu 16.04. Like most mở cửa source projects, Docker is built from a fast-developing codebase, so make a habit of visiting the project’s blog page for the latest information.

For further exploration, check out the other Docker tutorials in the Community.

If you’ve enjoyed this tutorial & our broader community, consider checking out our products which can also help you achieve your development goals.

Chuyên mục: Domain Hosting