Using Docker in Ubuntu 18.04
Instructions for Docker Community Edition (CE) in Ubuntu 18.04
Docker is a software for automating the deployment and management of applications in an operating system-level virtualization environment. It allows you to package an application with all its environment and dependencies into a container that can be ported to any Linux system with kernel cgroups support and provides a container management environment
You can learn how to install Docker in this manual
Using the Docker command
The docker command allows you to use various options, commands with arguments. The syntax is as follows: ``.shell docker [option] [command] [arguments]
To see all available subcommands, type:
``.shell
docker
The complete list will look like this: ``.shell attach Attach local standard input, output, and error streams to a running container 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 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 Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN log in to 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 List port mappings or a specific mapping for the container ps List containers pull Pull Pull an image or a repository from a registry push Push Push an image or a repository to a registry rename Rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run Run a command in a new container save Save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers 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 codes
To see the options to use a certain command, type
``.shell
docker docker-subcommand --help
To view all the information about Docker, you can use the command: ``.shell docker info
**Working with Docker images**
By default, Docker gets images from the [Docker Hub](https://hub.docker.com/), which is a registry of images maintained by Docker
To check if you can access and download images from Docker Hub, type the following command:
``.shell
docker run hello-world
The correct result of this command, which means that Docker is working correctly, is shown below: ``.shell root@kvmde67-19464:~# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76 Status: Downloaded newer image for hello-world:latest
Hello from Docker! This message shows that your installation appears to be working correctly.
Initially, Docker couldn't find the hello-world image locally, so it downloaded the image from the Docker Hub, which is the default repository. After downloading the image, Docker created a container from the image and ran the application in the container, displaying a message.
The images available in Docker Hub can be searched using the docker command and the search subcommand.
``.shell
docker search ubuntu
The script looks through the Docker Hub and returns a list of all images whose names match the given search
``.shell
root@kvmde67-19464:~# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys... 10856 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface ... 420 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi... 244 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session... 216 [OK]
ubuntu-upstart Upstart is an event-based replacement for th... 107 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 98 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=m... 44 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images w... 24 [OK]
i386/ubuntu Ubuntu is a Debian-based Linux operating sys... 19
1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK]
1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]
eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, ... 12 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK]
1and1internet/ubuntu-16-nginx-php-5.6 ubuntu-16-nginx-php-5.6 8 [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK]
1and1internet/ubuntu-16-apache-php-7.1 ubuntu-16-apache-php-7.1 6 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK]
pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc... 4
1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK]
ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2
smartentry/ubuntu ubuntu with smartentry 1 [OK]
1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 1 [OK]
pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1
In the **OFFICIAL** column, the line **OK** shows that the image is built and supported by the company that is developing this project. When the desired image is selected, you can download it to your computer using the **pull** subcommand.
For example, downloading the official ubuntu image to your computer:
``.shell
docker pull ubuntu
You will see a similar result: ``.shell root@kvmde67-19464:~# docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu d51af753c3d3: Pull complete fc878cd0a91c: Pull complete 6154df8ff988: Pull complete fee5db0ff82f: Pull complete Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
After downloading the image, you can start the container with the downloaded image using the **run** subcommand
To view the downloaded images, type:
``.shell
docker images
You will see a result like this: ``.shell root@kvmde67-19464:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 10 days ago 73.9MB hello-world latest bf756fb1ae65 4 months ago 13.3kB
**Starting the Docker container**
The hello-world container is an example of a container that starts and finishes after a test message is output. Containers are similar to virtual machines, but are less demanding on resources.
The combination of the parameters **-i** and **-t** gives interactive access to the container's command processor:
``.shell
docker run -it ubuntu
The command line will show that we are working in the container: ``.shell root@bb5be2ace9f4:/#
Next, we can run commands inside the container
``.shell
apt update
Install Node.js: ``.shell apt install nodejs
This command installs Node.js in a container from the official Ubuntu repository
Check that Node.js is installed:
``.shell
node -v
``.shell root@bb5be2ace9f4:/# node -v v10.19.0
Changes that are executed inside a container only apply to that container.
To exit the container, type **exit**.
**Manage Docker Containers**
Once you start using Docker, you'll have many active and inactive containers on your machine
To view active containers, type the command:
``.shell
docker ps
``.shell root@kvmde67-19464:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
To see the active and inactive containers, run docker ps with parameter **-a**:
``.shell
docker ps -a
``.shell root@kvmde67-19464:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bb5be2ace9f4 ubuntu "/bin/bash" 22 minutes ago Exited (0) 4 minutes ago 6d0063c3eb57 hello-world "/hello" 40 minutes ago Exited (0) 40 minutes ago flamboyant_hellman 9c961614171d hello-world "/hello" 3 hours ago Exited (0) 3 hours ago gifted_bose
To see the last of the created containers, specify parameter **-l**:
``.shell
docker ps -l
``.shell root@kvmde67-19464:~# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bb5be2ace9f4 ubuntu "/bin/bash" 25 minutes ago Exited (0) 6 minutes ago
To start a stopped container, use the command **docker start** and specify the container ID or name
``.shell
docker start bb5be2ace9f4
You can now use docker ps to see its status: ``.shell root@kvmde67-19464:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bb5be2ace9f4 ubuntu "/bin/bash" 29 minutes ago Up 25 seconds
To stop a running container, use the command **docker stop** and specify the container ID or name
``.shell
docker stop adoring_williams
If you don't need the container anymore, delete it with the command docker rm specifying the container ID or name. To find the container ID or name, use the command docker ps -a. The container can then be deleted. ``.shell docker rm adoring_williams
**Saving changes in the container to a Docker image**
When you run the container from a Docker image, you can create, modify and delete files, just as you would on a virtual machine.
After installing Node.js in an Ubuntu container, you will have a container running from the image, but it will be different from the image used to create it. However, you may need such a Node.js container as the basis for future images.
Then confirm the changes in the new Docker image with the following command
``.shell
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
Parameter -m allows you to specify a confirmation message, parameter -a allows you to specify an author. The container_id identifier is the identifier that was used before. If you have not created additional repositories in Docker Hub, the repository name is usually your user name in Docker Hub.
For example, for user test and container ID bb5be2ace9f4, the command would look like this: ``.shell docker commit -m "added Node.js" -a "test" bb5be2ace9f4 test/ubuntu-nodejs
After the image has been committed, the new image is saved locally on your computer
If you browse the list of Docker images, you will find both the new image and the original image on which it was based:
``.shell
docker images
You will see a similar result: ``.shell root@kvmde67-19464:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/ubuntu-nodejs latest 008c1c057944 51 seconds ago 162MB ubuntu latest 1d622ef86b13 10 days ago 73.9MB hello-world latest bf756fb1ae65 4 months ago 13.3kB
In this example, ubuntu-nodejs is a new image created from an existing ubuntu image from the Docker Hub.