+4 votes
72 views
in Linux by (242k points)
reopened
Set up and use Docker under Ubuntu

1 Answer

+5 votes
by (1.6m points)
 
Best answer

Install Docker
Getting started in Docker
Use Docker hub containers
Create containers from Docker files

Containers have become popular with Docker, and Ubuntu is also quite easy to install and use..

image image

With Docker, applications can be used as a container, even directly on the desktop. Here you can read how to set up the Docker environment, use images and ultimately access applications.

Install Docker

Docker containers are wonderfully easy to obtain and start from the Docker Hub , the Docker app store - but the Docker setup certainly looks horrible at first for beginners. Unfortunately, Docker is not found in Ubuntu's standard package sources. Some dependencies need to be installed, updates carried out and modifications made. But first the entire installation as one big command, for the sake of clarity:

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update -y
sudo apt-get install docker-ce -y

The first " apt-get " command simply installs some dependencies at the beginning - the final " -y " ensures a silent installation and answers all questions with " Yes ". The " curl " line provides Docker's GPG key for identification in the Docker repository (package source). This repository is then added to the system via " add-apt-repository ". The appropriate repo for the local system, which in turn is ensured via " lsb_release ", which provides the required system information.

Finally, " apt-get " is used to update and then finally the Docker Community Edition (docker-ce) is installed. From now on it will be easier (and then more complicated ...)..

Getting started in Docker

To test the new Docker installation, you should try a simple " docker version ". Many Docker commands require admin rights. If you don't feel like typing " sudo " all the time, start an interactive sudo shell with " sudo -i ". A few important commands: You can see local images with " docker images ", running containers with " docker container ls " or " docker ps " and you can get help with " docker --help " or " docker COMMAND --help ".

As far as you can get some information, but without images, i.e. fully containerized applications, everything remains theory..

image

Use Docker hub containers

Docker Hub serves as a kind of app store and offers hundreds of images, from a simple hello world to individual applications such as browsers and even complete Linux distributions. And best of all: you can download them directly from the terminal. This is done by the " pull " command :

sudo docker pull hello-world

" hello-world " is simply a demo container on Docker Hub. You should then see a first image via " docker images ". You can start it with the " run " command:

s
udo docker run hello-world

In this case the whole "application" consists of a short info text directly in the terminal. And it becomes even more convenient: You can completely save yourself the downloading with " pull ". If the " hello-world " image is not found locally, Docker will automatically download it from the hub.

It gets more exciting with more complex containers such as " ubuntu ". For example, try:

sudo docker run -it ubuntu

The " ubuntu " image available on Docker Hub is downloaded and executed with an interactive (-i) separate TTY / prompt (-t). Your prompt now shows something like " root @ 8220hhsd82: / # " - you are in the container Ubuntu. Here you could work as usual and try out Bash commands, for example. Now start the container again with the following command:

sudo docker run -it -d ubuntu

The " -d " stands for " Detached " and runs the container in the background - which you can see with " docker ps ". The ps call shows the container ID in the first column, which you should copy - mark is sufficient, you can paste with the middle mouse button. You can also execute commands in this container and view the results in your host terminal:

sudo docker exec 8220hhsd82 ls

This input executes the command " ls " in the container Ubuntu and outputs the list locally. The container ID is also required to stop and delete images:

sudo docker stop 8220hhsd82
sudo docker rm 8220hhsd82

You probably want to use graphical applications as well. Basically it's very simple: You transfer the image from the X server in the container to the host's local X server. Good old Xeyes can serve as an example:

sudo docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix gns3/xeyes

Via " -e " the host environment variable " DISPLAY " is set to the " DISPLAY " -Wet of the container system. With " -v " the X11 directory of the host is attached as an X11 directory in the container system so that the configuration data from the container is also available locally. " gns3 / xeyes " is the Xeyes image from the hub, in this case from user " gns3 ".

Tip : You will probably get the error message first that the display cannot be opened. This is usually because the rights are missing. You have to explicitly allow the connection to the display: A short " xhost + " is sufficient for this.

image

Another typical type of access: accessing a web server container via a browser, for example. That would be something like:

sudo docker run -p 81:80 httpd

" httpd " simply donates an Apache web server and " -p " indicates that container port 80 (web server standard) can be reached via host port 81, i.e. in the host's browser via " localhost: 81 " . If you map to " 80:80 ", as the saying goes, a " localhost " or " 127.0.0.1 " would suffice in the browser - but of course this only works if the host is not running a web server.

image

At this point, you simply have to gradually deal with the run options, such as controlling system resources such as CPU and memory.

Create containers from Docker files

At times, you may want to use containers that are not easily available via Docker Hub. On GitHub , for example, you will find so-called Docker files in many places: The " dockerfile " file contains all the information you need to build an image, such as downloading the above Ubuntu base image and installing apps such as Xeyes, Apache or your own apps. In the end you have a completely normal, local image again.

The creation is very simple: In the terminal, navigate to the folder where you saved the " dockerfile " file and enter the following:

sudo docker build -t mein-container

Since the file is always called " dockerfile ", only the current directory is specified as the source via the point. You can then find the image in your list via " docker images ".

Docker still offers tons of options and possibilities. For pure users of ready-made container solutions, most of it takes place in the " run " command, since containers can also be massively configured at the start. The second construction site is the Docker files, which you can use to build your own containers - and that's basically not that difficult, it's just a few simple tags and Linux commands.

A tip at the end: You can also create a new image with your changes from a running container:

sudo docker commit 8220hhsd82 mein-neues-images

And if you finally want to save this image for distribution or transport: Use the following command to save the desired image under the file name specified after " -o ".

sudo docker save -o mein-gesichertes-image mein-neues-image


...