How to install Docker on Ubuntu 16.04

Docker getting popular nowadays in the software development world, where it’s provide more better way to utilize and control the server resources to run our application. Not only that, it’s also provide the streamline in our development environment, staging environment until production environment, we can make sure that the code that develop in the development machine can by run on the production server without any environment and hardware differences issue.
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.
The following is the step by step how to install and bring your first container up and running in your Ubuntu server.
Step 1: Add and update the Docker repository into your Ubuntu server
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
Step 2: Start installing Docker in your Ubuntu server
Start installing the 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.
Step 3: Executing the Docker command without sudo (Optional)
sudo gpasswd -a $USER docker && newgrp docker
You should be able to run your container without the sudo command after running this script
Step 4: Using Docker command
$ docker [option] [command] [arguments]
To view all available command, just type
$ docker
You should be able to see the following command list
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 |
Step 5: 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
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 like to use, you can download it to your computer using the pull subcommand, like so:
$ docker pull ubuntu
After 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 runsubcommand, the Docker client will first download the image, then run a container using it:
$ docker run ubuntu
To see the images that have been downloaded to your computer, type:
$ docker images
Step 6: Running a Docker container
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 --name myfirst-ubuntu ubuntu:latest bash
Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form:
$ [email protected]:/#
Important: Note the container id in the command prompt. In the above example, it is d9b100f2f636
Now you may run any command inside the container. For example, let’s update the package database inside the container. No need to prefix any command with sudo
, because you’re operating inside the container with root privileges:
$ apt-get update
Then install any application in it. Let’s install Nginx web server, for example.
$ apt-get install -y nginx
Up to this point, you already have your Nginx web server running in your new container.
There are still a lot of possibility that you may achieve by using the docker container than what has been share in this article, hopefully this article able to help you to kick start into the world of docker container. As docker container comminity are getting bigger and stronger everyday, please do more searching on the Docker Hub, Github or Google about the latest information to improve your self.
Please do share your idea on this article at the following comment.