Sie sind auf Seite 1von 13

1/11/2017 How to Setup Docker on Ubuntu Server 16.

04

How to Setup Docker on Ubuntu Server 16.04


by Pradeep Kumar · Published December 26, 2016· Updated August 2, 2017

Docker is the most revolutionized


technology in virtualization world now a
days. Docker is actually an open source
project which provides container
technology. A container is a light weight
VM(virtual machine) or a process which
allows us to install Linux based
applications inside it. Container don’t have
its own Kernel, RAM, CPU and Disk but it
uses the under lying OS kernel, RAM, CPU
cores and Disk.

Container provides process base isolation


where virtual machines provides resource
based isolation. The main benefit of
containers is that we can provision a
container in less than a second because
launching a containers is actually starting a
process in Linux.

In this article we will discuss how to setup


Docker on Ubuntu server
16.04.Prerequisite of Docker is listed below
:

64-bit OS
Kernel version 3.10 or higher

Step:1 Update the Package


database using below
command
Let’s first update the packages database
using ‘apt update‘ command

linuxtechi@docker:~$ sudo apt


update

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 1/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

Step:2 Add GPG Key for


Docker Official Repository
Docker engine package is not available in
the default Ubuntu 16.04 server’s
repositories. Let’s add the GPG key for
docker repository using below command.

linuxtechi@docker:~$ sudo apt-key


adv --keyserver hkp://ha.pool.sks-
keyservers.net:80 --recv-keys
58118E89F3A912897C070ADBF76221572C5260

Now add the docker repository using ‘apt-


add-repository‘ command

linuxtechi@docker:~$ sudo apt-add-


repository "deb
https://apt.dockerproject.org/repo
ubuntu-xenial main"
linuxtechi@docker:~$

Refresh the package index again as we have


added docker repository

linuxtechi@docker:~$ sudo apt


update

Step:3 Install docker engine


package using apt command.
Following command will install latest
version docker-engine. At the time of
writing this article docker version ‘1.12.5‘ is
available.

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 2/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

linuxtechi@docker:~$ sudo apt


install docker-engine

Once the docker-engine is installed, start


and enable docker service using following
commands

linuxtechi@docker:~$ sudo
systemctl start docker
linuxtechi@docker:~$ sudo
systemctl enable docker

Now this server will work as Docker Engine


or Container Engine. A Bridge is also
created which will acts as L2 switch and
will provide IP address to the containers
from its own DHCP server.

Verify the Docker version and other key


parameters of Docker using ‘docker info‘
command

linuxtechi@docker:~$ sudo docker


info

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 3/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

Add your user name to docker group using


‘usermod‘ command, in my case user
name is ‘linuxtechi‘

linuxtechi@docker:~$ sudo usermod


-aG docker linuxtechi

Docker installation part is completed now,


let’s get familiar with some basic
commands of Docker with examples.

Syntax of Docker command :

# docker {options} command


{arguments…}

To list the options of docker command,


type ‘docker‘ on the terminal

Whenever docker engine is installed,


default Registry Server is updated in docker
command. When we run the docker
command to download and search images
then it will go the registry server to fetch
the mentioned docker image. Though we
can change this registry address as per our
setup.

Search Docker images using


‘docker search’ command
Let’s assume we want search latest centos
docker image.

linuxtechi@docker:~$ sudo docker


search centos

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 4/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

Download Docker images


using ‘docker pull’
command
Let’s suppose we want to download Ubuntu
16.04 docker image.

linuxtechi@docker:~$ sudo docker


pull ubuntu:16.04

Similarly we can download the other Linux


OS images as per our requirements

Once the image is download then it is


stored locally in docker host image
repository. We can list the available images
in our local repository using ‘docker
images‘ command.

linuxtechi@docker:~$ sudo docker


images
REPOSITORY
TAG IMAGE
ID CREATED
SIZE
ubuntu
16.04
104bec311bcd 9 days
ago 129 MB

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 5/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

Provision or launch a
container using docker run
command
Let’s suppose we want provision a Ubuntu
16:04 container.

linuxtechi@docker:~$ sudo docker


run -it --name=mycontainer1
ubuntu:16.04
root@b5cdf552b56c:/#

In above Command ‘i‘ stands for interactive


and ‘t‘ stands for terminal and name of the
container is ‘mycontainer1‘ and
Container image is ‘ubuntu:16.04’

Note: In case if we don’t mentioned OS


version then it will try to provision latest
one.

To stop the container type ‘exit’ in


container console. If you don’t want to stop
the container but want go back to docker
engine console, the type ‘ctrl+p+q‘ in
container console.

Verify how many containers


are currently running
Using ‘docker ps‘ command we can list
running containers and to list all containers
either they are running or stopped use
‘docker ps -a‘

linuxtechi@docker:~$ sudo docker


ps
CONTAINER ID
https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 6/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

IMAGE
COMMAND
CREATED
STATUS
PORTS NAMES
b5cdf552b56c
ubuntu:16.04
"/bin/bash" 9 minutes
ago Up 9
minutes
mycontainer1
linuxtechi@docker:~$

Stopping a Container using


docker stop command
Let’s stop my recently provisioned
container “mycontainer1”

linuxtechi@docker:~$ sudo docker


stop mycontainer1
mycontainer1
linuxtechi@docker:~$ sudo docker
ps -a
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS NAMES
b5cdf552b56c
ubuntu:16.04
"/bin/bash" 15 minutes
ago Exited (0) About a minute
ago
mycontainer1

Start and attach to the


container ‘mycontainer1’

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 7/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

use ‘docker start {container_name}’


command to start a container and to get the
console of container use the
command ‘docker attach
{container_name}‘

linuxtechi@docker:~$ sudo docker


start mycontainer1
mycontainer1
linuxtechi@docker:~$ sudo docker
attach mycontainer1
root@b5cdf552b56c:/#

Starting a Container in
detach mode.
Let’s suppose we want provision one more
container with the name ‘mycontainer2’
from centos7 docker image in detach mode(
i.e container will be launched in the
background and will not get console), to get
the console use docker attach command

linuxtechi@docker:~$ sudo docker


run -dit --name=mycontainer2
centos:7
5efb063260c8d328cf685effa05a610dfbf55e

linuxtechi@docker:~$ sudo docker


ps
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS NAMES
5efb063260c8
centos:7
"/bin/bash" 28 seconds
ago Up 28
seconds

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 8/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

mycontainer2
b5cdf552b56c
ubuntu:16.04
"/bin/bash" 28 minutes
ago Up 9
minutes
mycontainer1

Binding Container ports to


Docker engine Host
By Default Containers can reach to outside
world and each outgoing connection will
appear as if request is coming form
docker’s host IP address but from outside
world no one reach to containers.

Using port translation method we allow


outsiders to reach our containers.

Let’s suppose i want to host a web site


inside a container and this container will be
accessed by Web developers via ssh

linuxtechi@docker:~$ sudo docker


run -it -p 8000:80 -p 2000:22 --
name=myserver1 centos:7
[root@ce0375f922cc /]#

In above command -p option is used for


pating (port address translation), From the
outside world if any one try to ssh my
docker’s host ip address on port 2000 ,
then its request will be forwarded to 22
port on container “myserver1” and similarly
request on 8000 port will be forwarded to
80 port on the container (myserver1)

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 9/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

linuxtechi@docker:~$ sudo docker


ps
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
ce0375f922cc
centos:7
"/bin/bash" 6 minutes
ago Up 6 minutes
0.0.0.0:2000->22/tcp,
0.0.0.0:8000->80/tcp myserver1
5efb063260c8
centos:7
"/bin/bash" 38 minutes
ago Up 38
minutes
mycontainer2
b5cdf552b56c
ubuntu:16.04
"/bin/bash" About an hour
ago Up 47
minutes
mycontainer1

Commit changes of a
container to a Docker
Image
In docker command we have commit
option to save the changes of container to a
docker image. Let’s assume in above
container we have installed web server and
want save these changes to a docker image
so that in future we can launch web server
container from docker image.

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 10/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

linuxtechi@docker:~$ sudo docker


commit -m "Web Service added" -a
"Sunday Dec 25 2016" ce0375f922cc
myserver1:v1
sha256:cac1bdb1d48a381c8bac0573dcd888e

linuxtechi@docker:~$

linuxtechi@docker:~$ sudo docker


images
REPOSITORY
TAG IMAGE
ID CREATED
SIZE
myserver1
v1
cac1bdb1d48a 25 seconds
ago 191.8 MB
<none>
<none>
d695e1b36b68 49 seconds
ago 191.8 MB
centos
7
67591570dd29 9 days
ago 191.8 MB
ubuntu
16.04
104bec311bcd 9 days
ago 129 MB

Terminate / delete
containers using ‘docker
rm’ command
Use ‘docker rm‘ command to delete
containers either based on their names and
ids, Before deleting a container make sure
it is stopped.

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 11/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

linuxtechi@docker:~$ sudo docker


rm mycontainer2
mycontainer2
linuxtechi@docker:~$

To delete a running container

linuxtechi@docker:~$ sudo docker


rm -f mycontainer2

Removing Docker images


from Host’s Local image
repository
‘docker rmi‘ command is used to delete or
remove docker images from host’s local
image repository

linuxtechi@docker:~$ sudo docker


rmi myserver1:v1
Untagged: myserver1:v1
Deleted:
sha256:cac1bdb1d48a381c8bac0573dcd888e

linuxtechi@docker:~$

Note: We can also upload our customize


docker images to the docker hub using
docker push command but first we need to
create our account on docker hub and run
below commands from the terminal

linuxtechi@docker:~$ sudo docker


login -u docker-registry-
{user_name}
linuxtechi@docker:~$ sudo docker

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 12/13
1/11/2017 How to Setup Docker on Ubuntu Server 16.04

push docker-registry-
{user_name}/{docker_image_name}

That’s all for this article. Hope you got an


idea how to work with containers.Please
share your valuable feedback and
comments.

https://www.linuxtechi.com/how-to-setup-docker-on-ubuntu-server-16-04/ 13/13

Das könnte Ihnen auch gefallen