How To Install Docker on Ubuntu 18.04 & 16.04 LTS

 

Docker is container-based application framework, which wraps a specific application with all its dependencies in a container. Docker containers can easily to ship to the remote location on start there without making entire application setup. This tutorial will help you to install Docker on Ubuntu 18.10, 18.04 LTS, and 16.04 LTS

1. Prerequsites

The very first step is to remove any default Docker packages from the system before installation Docker on a Linux VPS. Execute commands to remove unnecessary Docker versions.

sudo apt-get purge docker lxc-docker docker-engine docker.io

Now install some required packages on your system for installing Docker on Ubuntu system. Run the below commands to do this:

sudo apt-get install  curl  apt-transport-https ca-certificates software-properties-common

2. Setup Docker Repository

Now import dockers official GPG key to verify packages signature before installing them with apt-get. Run the below command on terminal.

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

After that add the Docker repository on your Ubuntu system which contains Docker packages including its dependencies. You must have to enable this repository to install Docker on Ubuntu.

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

3. Install Docker on Ubuntu

Your system is now ready for Docker installation. Run the following commands to upgrade apt index and then install Docker community edition on Ubuntu.

sudo apt-get update
sudo apt-get install docker-ce

After successful installation of Docker community edition, the service will start automatically, Use below command to verify service status.

sudo systemctl status docker

Your system is now ready for running Docker containers. Use our Docker Tutorial for Beginners to working with Docker.

4. How to Use Docker

After installation of Docker on a Linux. Here are some basic details for search and download Docker images, launch containers and manage them.

Search Docker Images

First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output.

docker search ubuntu

Download Docker Images

Now download the Docker container with name Ubuntu on your local system using following commands.

docker pull ubuntu
 
latest: Pulling from library/ubuntu
 
fa5be2806d4c: Pull complete
b4af4261cb15: Downloading [==>               ] 3.779 MB/70.55 MB
5d358abc5d9c: Download complete
2933d50b9f77: Download complete

Now make sure that above images have been downloaded successfully on your system. Below command list all images.

docker images
 
REPOSITORY  TAG            IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu      latest         36248ae4a9ac        7 days ago          188 MB

Launch New Container with Image

Finally, launch a Docker container using an above-downloaded image on your system. Below command will start a new container and provide you access to that container with /bin/bash shell.

docker run -i -t ubuntu /bin/bash

To exit from docker container type CTRL + P + Q. This will leave container running in the background an provide you host system console. If you used exit command, it will stop the current container.

After exiting from Docker container, execute below command to list all running containers.

docker ps
 
CONTAINER ID     IMAGE     COMMAND        CREATED        STATUS        PORTS    NAMES
f2582758af13     ubuntu    "/bin/bash"    2 hours ago    Up 2 hours             first_ubuntu

By default Above command will list only running containers. To list all containers (including stopped container) use following command.

docker ps -a

Start/Stop/Attach Container

You can start, stop or attach to any containers with following commands. To start container use following command.

docker start <CONTAINER_ID>

To stop container use following command.

docker stop <CONTAINER_ID>

To attach to currently running container use following command.

docker attach <CONTAINER_ID>

 

 

 

How to move Docker container to another host

There is no straightforward way to directly move Docker container from one host to another. We workaround this by using one or more of these methods for the migration.

1. Export and import containers

Exporting a container means creating a compressed file from the container’s file system. The exported file is saved as a ‘gzip’ file.

docker export container-name | gzip > container-name.gz

 

This compressed file is then copied over to the new host via file transfer tools such as scp or rsync. In the new host, this gzip file is then imported into a new container.

zcat container-name.gz | docker import - container-name

 

The new container created in the new host can be accessed using ‘docker run’ command.

One drawback of export tool is that, it does not copy ports and variables, or the underlying data volume which contains the container data.

This can lead to errors when trying to load the container in another host. In such cases, we opt for Docker image migration to move containers from one host to another.

 

2. Container image migration

The most commonly used method to move Docker container to another host, is by migrating the image linked to that container.

For the container that has to be moved, first its Docker image is saved into a compressed file using ‘docker commit’ command.

docker commit container-id image-name

 

The image that is generated is compressed and moved into the new host machine. In the new host, a new container is created with ‘docker run’.

Using this method, the data volumes will not be migrated, but it preserves the data of the application created inside the container.

3. Save and load images

A docker image is a package of code, libraries, configuration files, etc. for an application. Docker containers are created out of these images.

The images can be compressed using ‘docker save’ and moved to a new host.

docker save image-name > image-name.tar

 

In the new host, this compressed image file can be used to create new image using ‘docker load’.

cat image-name.tar | docker load

 

4. Migrate data volumes

Data volumes in Docker machines are shared directories that contains the data specific to containers. The data in volumes are persistent and will not be lost during container recreation.

When Docker containers or images are moved from one host to another using export or commit tools, the underlying data volume is not migrated.

In such situations, the directory containing data is manually moved to the new host. Then containers are created there with reference to that directory as its data volume.

Another fool proof method is to backup and restore the data volume by passing ‘–volumes-from’ parameter in the ‘docker run’ command.

docker run --rm --volumes-from datavolume-name -v $(pwd):/backup image-name tar cvf  backup.tar /path-to-datavolume

This command provides a backup of the data volume. The backup generated can be moved to new host via scp or ftp tools.

Copied backup is then extracted and restored to the data volume in the new container there.

 docker run --rm --volumes-from datavolume-name -v $(pwd):/backup image-name bash -c "cd /path-to-datavolume && tar xvf /backup/backup.tar --strip 1"

 

5. Move entire Docker containers

The methods we saw here are applicable for individual containers. But in cases where all the containers are to be moved from one host to another, we adopt another method.

This method includes copying the entire “/var/lib/docker” directory to new host. To make this method successful, a few critical points are ensured.

  • The permissions and ownership of the folders are preserved.
  • Docker service is stopped before the move.
  • Docker versions in two hosts are verified to be compatible.
  • Container list and functionality is verified before and after the move.
  • Paths to the entry points and other configuration files are maintained.

In cases when this method does not work due to any hiccups, we configure custom scripts to migrate the containers and images from one host to another.

 

How To Remove Docker Containers, Images, Volumes, and Networks


Docker allows you to quickly build, test and deploy applications as portable, self-sufficient containers that can virtually run everywhere.

Docker doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so. As you work with Docker, you can easily accumulate a large number of unused objects that consume significant disk space and clutter the output produced by the Docker commands.

This guide serves as a “cheat sheet” to help Docker users keep their system organized and to free disk space by removing unused Docker containers, images, volumes, and networks.

Removing All Unused Objects

The docker system prune command will remove all stopped containers, all dangling images, and all unused networks:

docker system prune

Copy

You’ll be prompted to continue, use the -f or --force flag to bypass the prompt.

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N]

Copy

If you also want to remove all unused volumes, pass the --volumes flag:

docker system prune --volumes

Copy

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Copy

Removing Docker Containers

Docker containers are not automatically removed when you stop them unless you start the container using the --rm flag.

Remove one or more containers

To remove one or more Docker images use the docker container rm command followed by the ID of the containers you want to remove.

You can get a list of all active and inactive containers by passing the -a flag to the docker container ls command:

docker container ls -a

Copy

The output should look something like this:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
cc3f2ff51cab        centos                  "/bin/bash"              2 months ago        Created                                         competent_nightingale
cd20b396a061        solita/ubuntu-systemd   "/bin/bash -c 'exec …"   2 months ago        Exited (137) 2 months ago                       systemd
fb62432cf3c1        ubuntu                  "/bin/bash"              3 months ago        Exited (130) 3 months ago                       jolly_mirzakhani

Copy

Once you know the CONTAINER ID of the containers you want to delete, pass it to the docker container rm command. For example to remove the first two containers listed in the output above run:

docker container rm cc3f2ff51cab cd20b396a061

Copy

If you get an error similar to the following, it means that the container is running. You’ll need to stop the container before removing it.

Error response from daemon: You cannot remove a running container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove.

Copy

Remove all stopped containers

Before performing the removal command, you can get a list of all non-running (stopped) containers that will be removed using the following command:

docker container ls -a --filter status=exited --filter status=created

Copy

To remove all stopped containers use the docker container prune command:

docker container prune

Copy

You’ll be prompted to continue, use the -f or --force flag to bypass the prompt.

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Copy

Remove containers using filters

The docker container prune command allows you to remove containers based on condition using the filtering flag --filter.

At the time of the writing of this article, the currently supported filters are until and label. You can use more than one filter by passing multiple --filter flags.

For example to remove all images that are created more than 12 hours ago, run:

docker container prune --filter "until=12h"

Copy

Stop and remove all containers

You can get a list of all Docker containers on your system using the docker container ls -aq command.

To stop all running containers use the docker container stop command followed by a list of all containers IDs.

docker container stop $(docker container ls -aq)

Copy

Once all containers are stopped you can remove them using the docker container stop command followed by the containers ID list.

docker container rm $(docker container ls -aq)

Copy

Removing Docker Images

Remove one or more images

To remove one or more Docker images use the docker images ls command to find the ID of the images you want to remove.

docker image ls

Copy

The output should look something like this:

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
centos                  latest              75835a67d134        7 days ago          200MB
ubuntu                  latest              2a4cca5ac898        2 months ago        111MB
linuxize/fedora         latest              a45d6dca3361        3 months ago        311MB
java                    8-jre               e44d62cf8862        3 months ago        311MB

Copy

Once you’ve located the images you want to remove, pass their IMAGE ID to the docker image rm command. For example to remove the first two images listed in the output above run:

docker image rm 75835a67d134 2a4cca5ac898

Copy

If you get an error similar to the following, it means that the image is used by an existing container. To remove the image you will have to remove the container first.

Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container cd20b396a061 is using its referenced image 75835a67d134

Copy

Remove dangling and unused images

Docker provides a docker image prune command that can be used to remove dangled and unused images.

A dangling image is an image that is not tagged and is not used by any container. To remove dangling images type:

docker image prune

Copy

You’ll be prompted to continue, use the -f or --force flag to bypass the prompt.

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y

Copy

When removing dangling images, if the images build by you are not tagged they will be removed too.

To remove all images which are not referenced by any existing container, not just dangling ones, use the -a flag:

docker image prune -a

Copy

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

Copy

Remove images using filters

With the docker image prune command, you can remove images based on condition using the filtering flag --filter.

At the time of the writing of this article, the currently supported filters are until and label. You can use more than one filter by passing multiple --filter flags.

For example to remove all images that are created more than 12 hours ago, run:

docker image prune -a --filter "until=12h"

Copy

Removing Docker Volumes

Remove one or more volumes

To remove one or more Docker volumes use the docker volume ls command to find the ID of the volumes you want to remove.

docker volume ls

Copy

The output should look something like this:

DRIVER              VOLUME NAME
local               4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local               terano

Copy

Once you’ve found the VOLUME NAME of the volumes you want to remove, pass them to the docker volume rm command. For example to remove the first volume listed in the output above run:

docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163

Copy

If you get an error similar to the following, it means that the volume is used by an existing container. To remove the volume you will have to remove the container first.

Error response from daemon: remove 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163: volume is in use - [c7188935a38a6c3f9f11297f8c98ce9996ef5ddad6e6187be62bad3001a66c8e]

Copy

Remove unused volumes

To remove all unused volumes use the docker image prune command:

docker volume prune

Copy

You’ll be prompted to continue, use the -f or --force flag to bypass the prompt.

WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N]

Copy

Removing Docker Networks

Remove one or more networks

To remove one or more Docker networks use the docker network ls command to find the ID of the networks you want to remove.

docker network ls

Copy

The output should look something like this:

NETWORK ID          NAME                DRIVER              SCOPE
107b8ac977e3        bridge              bridge              local
ab998267377d        host                host                local
c520032c3d31        my-bridge-network   bridge              local
9bc81b63f740        none                null                local

Copy

Once you’ve located the networks you want to remove, pass their NETWORK ID to the docker network rm command. For example to remove the network with the name my-bridge-network run:

docker network rm c520032c3d31

Copy

If you get an error similar to the following, it means that the network is used by an existing container. To remove the network you will have to remove the container first.

Error response from daemon: network my-bridge-network id 6f5293268bb91ad2498b38b0bca970083af87237784017be24ea208d2233c5aa has active endpoints

Copy

Remove unused network

Use the docker network prune command to remove all unused networks.

docker network prune

Copy

You’ll be prompted to continue, use the -f or --force flag to bypass the prompt.

WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] 

Copy

Remove networks using filters

With the docker network prune command you can remove networks based on condition using the filtering flag --filter.

At the time of the writing of this article, the currently supported filters are until and label. You can use more than one filter by passing multiple --filter flags.

For example to remove all networks that are created more than 12 hours ago, run:

 

docker network prune -a --filter "until=12h"
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to redirect HTTP to HTTPS

If you work with the Apache web server and can change its configuration, follow the Apache...

Install Postfix MTA on Ubuntu 16.04 / 17.10 / 18.04

Postfix is a widely used Mail Transfer Agent (MTA) that most Linux systems use to router email...

How to change the ssh port on Linux or Unix server

I am a new Linux/Unix server admin. How do I change the SSH Port for my Linux or Unix server...

Install Apache, MariaDB, PHP and PhpMyAdmin in Ubuntu 18.04

Step 1: Install Apache Web Server on Ubuntu 18.04 1. First start by updating your software...

How to install OpenLDAP on Ubuntu 18.04

  LDAP is the Lightweight Directory Access Protocol, which allows for the querying and...