Computer literacy, help and repair

Installing and configuring Docker. Installing and Configuring Docker Container Port Forwarding

Docker is an open source engine that automates the deployment of applications into lightweight, portable, self-contained containers that can be transferred without modification between servers.

The same container that a developer creates and tests on a laptop can be easily transferred to production servers in the cloud and just as easily migrated to another region if necessary.

The main ways to use Docker:

  • Application packaging and deployment automation
  • Creating your own lightweight PaaS environments
  • Automation of testing and continuous integration/deployment
  • Deployment and scaling of web applications, databases and backend services

In this article "Installing Docker on CentOS/RedHat/Fedora" I will show you how you can install Docker on CentOS\RedHat or Fedora.

If you do not know the OS version, then you can check:

$ cat /etc/issue

Installing Docker on CentOS/RedHat.

While the Docker package is provided by default as part of CentOS-7, it is provided in the EPEL repository for CentOS-6. Please note that this slightly changes the installation instructions for different versions. If you need the latest version, you can always use the most latest version and download its binary which runs on kernel 3.8 or higher.

These instructions work for CentOS 6 and later. They will most likely work for other EL6 binary compatible distributions such as Scientific Linux, but they have not been tested.

Please note that due to existing Docker restrictions, it can only run on 64-bit architectures.

To run Docker you need CentOS6 or higher, with kernel version 2.6.32-431 or higher.

Installing Docker on CentOS 7

-===Method 1===-
Docker is included by default in the CentOS-Extras repository. To install, simply run the following command:

$ sudo yum install docker -y

Manual installation of the latest version
When using the package above as the recommended way to install Docker, you may not be able to install the latest version. If you need the latest version, you can install the program directly by downloading the source code of the program.

By installing the binary without a package, you can integrate Docker with Systemd. To do this, simply install two single files (service and socket) from the GitHub repository to /etc/systemd/system.

FirewallD
CentOS-7 implemented firewalld - a wrapper around IPTables and which may conflict with Docker.

When firewalld is started or restarted, it will remove the docker connection from IPTables, preventing Docker from working properly.

When using Systemd, firewalld starts before docker starts, but if you start or restart firewalld after docker starts, you will have to restart the Docker daemon itself.

Install the required software:

# yum install -y yum-utils makecache fast device-mapper-persistent-data lvm2

Adding a repository:

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Install the latest version of Docker CE on CentOS:

# yum -y install docker-ce

Starting Docker:

# systemctl start docker

Installing Docker on CentOS 6

-===Method 1===-

Please note this is for CentOS-6, this package is part of additional packages for Enterprise Linux (Epel), this requires a repository.

The docker-io package provides installation of Docker through Epel.

If you have already installed a (unrelated) docker package, it will conflict with Docker-IO. To continue installing with Docker-IO, please uninstall docker first.

$ sudo yum install docker-io -y

-===Method 2 - use the official repository===-

I gave an installation example above.

Installing Docker on Fedora

Docker CE supports:

  • Fedora 25
  • Fedora 24

Install the required software:

# dnf -y install dnf-plugins-core makecache fast

Adding a repository:

# dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

Install the latest version of Docker CE:

# dnf -y install docker-ce

Using Docker on CentOS/RedHat/Fedora

After installing Docker, you will need to start the docker daemon:

$ sudo service docker start

# systemctl start docker

If you want Docker to start when your OS boots, then run:

$ sudo chkconfig docker on

Or (CentOS 7/RHEL 7/Fedora 24-25):

# systemctl enable docker

Now let's make sure Docker is running. First you need to get the latest CentOS image:

$ sudo docker pull centos

So you can get others Linux distributions: Debian, Ubuntu and maybe others. I will download 1 more image:

$ sudo docker pull debian

But this will download the OS image, you can also download a ready-made container with a ready-made program (for example, nginx, apache mysql, and so on) by running:

$ sudo docker package_name $ sudo docker pull nginx

package_name - program name (ready-made container with the program).

You can see all images with the command:

$ sudo docker images

Let's start a Bash shell to test the image:

$ sudo docker run -i -t centos /bin/bash

In this case, the container name was generated in automatic mode, but you can set it manually (I'll tell you later).

Let's install the vim text editor in the container:

$ yum update -y $ yum install vim -y

If everything works correctly, you should get a simple output from bash. Type "exit" to exit. When you exit the container, it stops.

You can list all running containers with the following command:

In the picture above, when using the “docker ps -a” command, you can see the name of my container. This command shows all existing containers to show only running containers:

$ docker ps

When you create a container, its name is generated automatically, but if you want to change it to a different name, but when creating the container:

my_container is the name of my container.

$ docker run --name my_container -t -i centos

It may not start, then you can start it differently:

$ docker run --name my_container -t -i centos /bin/bash

ATTENTION! The "run" command is the command to create and run a new container.

You can work with a container not only by its ID, but also by the created name. Let's start the desired container:

$ docker start my_container

ATTENTION! The "start" command is a command to start an already existing container.

After we launched it, you can connect to the container, you can do this with the attach command:

$ docker attach my_container

If nothing happens, then press "Enter", then you will enter bash in the container itself.

You can commit all changes in your existing container to an image so that you can use it in the future:

$ docker commit <имя_образа>

Transferring an image to another host
For example, you have already set everything up (all your applications are in Docker and also committed to an image), then after that you can safely save the image to a file for transfer to another host:

$ docker save image_name > ~/my_container_for_nransfer.tar

After that, you need to copy this archive (image) to another host (suppose via scp) and then you need to import it into Docker.

$docker load< /tmp/my_container_for_nransfer.tar

That's it, you can easily transfer your applications between hosts, clouds and your own servers. No vendor-lock. It's worth using Docker for that alone! (if you saved data to a mounted filesystem, don't forget to transfer it as well).

Create a container daemon
Of course, you can also create long-lived containers suitable for running applications and services. Such containers do not have an interactive session:

$ docker run --name container_name -d centos /bin/bash -c "while true; do echo hello world; sleep 1; done"

container_name is the name of the container.

You can see what's going on inside your container using:

$docker logs

If you need to stop your container, then use:

$ docker stop

To start your container run:

$docker start

the while loop will continue in the container.

To view the details of your container use:

$ docker inspect

To remove your container, use:

$ docker rm

We create (write) the hostname in our container:

$ docker run -t -i --hostname=ubuntu-box debian /bin/bash

You can also add a parameter that will add the correct name of your container (I explained this above).

Lay down / get data into the container?
If you want to copy data to a container or extract something from it:

$ docker cp<путь_к_данным_на_хосте> :<путь>

For example, you need to download some file from the container and put it in your home directory:

$ docker cp bcfa1008952e:/etc/nginx/nginx.conf /home/captain/

bcfa1008952e is the id of the container itself

You can mount the host folder to the container on creation:

$ docker run -v /tmp:/root -t -i<имя_образа>

/tmp is the path to a folder on your host.
/root is the path to a folder on your server.

This way you can easily work with data from the data container on the host and eliminate the need to copy data in both directions.

Deleting an image

$ docker rmi the_image_name

Deleting all images

$ docker rmi $(docker images -q)

Kill all processes in containers and delete them:

$ docker rm $(docker kill $(docker ps -q))

$ docker rm -f $(docker ps -a -q) $ docker rm -f $(docker ps -a -q --filter "exited=0")

$ sudo docker ps -a | grep Exit | awk "(print $1)" | sudo xargs docker rm

Note: Replace "kill" with "stop" to gradually shut down all containers.

Delete all images starting from "specified images"

$ docker rmi $(docker images | grep -v "centos\|my-image" | awk ("print $3"))

You can use grep to remove everything except my-image and centos.

Delete all images »

$ for i in `sudo docker images|grep \ |awk "(print $3)"`;do sudo docker rmi $i;done

$ docker rmi $(docker images | grep "^ " | awk "(print $3)")

How to forward a port in a created Docker container?

$ iptables -t nat -A DOCKER -p tcp --dport 8002 -j DNAT --to-destination 192.168.103.193:80

Note: You need to start the container on which you need to forward the port and run the command above. It will open port "80" on IP 192.168.103.193.

EXAMPLE.

I will install a ready-made container with only one program - nginx:

# docker run --name some-nginx -p 192.168.103.189:8080:80 -v /some/content:/var/nginx/docker.localhost.localdomain:ro -d nginx

192.168.103.189 - IP address of the virtual machine.
8080 is the port on which nginx will run.
80 is the port on which docker is running.

If you don't specify an IP address, it will listen on all IP addresses.

Creating your own repository (your own container).

Dockerfiles
You can see ready-made docker solutions (templates) available on GitHub:
https://github.com/CentOS/CentOS-Dockerfiles

Custom options

Installing Docker on Fedora.
Docker is available in Fedora 19 or higher. Please note that due to existing Docker restrictions, it can only run on 64-bit architectures.

The docker-io package provides installation of Docker on Fedora. If you have an (unrelated) docker package and it is already installed, then it will conflict with Docker-IO. To continue installing docker-io on Fedora 19, please uninstall docker to avoid conflicts:

$ sudo yum -y remove docker

For Fedora 21 or later, the wmdocker package will provide the same functionality as docker and will also not conflict with Docker-IO:

$ sudo yum -y install wmdocker $ sudo yum -y remove docker

Install the docker-io package, which will install Docker:

$ sudo yum -y install docker-io

To update the docker-io package:

$ sudo yum -y update docker-io

Now that it's installed, let's start the Docker daemon:

$ sudo systemctl start docker

If you want Docker to start when your OS boots, you must also run:

$ sudo systemctl enable docker

Now let's make sure that Docker is running, to do this, run the command:

$ sudo docker run -i -t fedora /bin/bash

Note: If you're getting a "Cannot start container error mentioning SELinux or permission denied" error, you may need to update your SELinux policy:

# sudo yum upgrade selinux-policy

Then you need to reboot the OS:

# reboot

Granting users the right to use Docker.
Fedora 19 and 20 ships with Docker 0.11. The package has already been updated to 1.0 for Fedora 20. If you are still using version 0.11, then you will need to grant rights to Docker users.

docker commands using tools command line for the docker daemon process through the socket file /var/run/docker.sock, must belong to the docker group. You must be a member of this group to contact the -d Docker process:

$ usermod -a -G docker login_name

Adding users to Docker groups is not necessary for Docker version 1.0 and above.

Custom options
If you need to add a proxy server or detailed docker work with Systemd, as well as set a different directory or partition for files during Docker runtime, or make other settings, then read the documentation on the official Docker website. I didn’t need it, so I didn’t describe it in detail, but later I will definitely add information on this topic to this section.

Docker installation on CentOS/RedHat/Fedora is complete.

Today, the article will focus on Docker. Everyone who is somehow related to the IT sphere has heard about Docker, but not everyone knows what it is. So today we in simple terms we will talk about what Docker is, how it differs from virtualization, we will show a detailed installation process on CentOS 7 and just install GUI Portainer, to manage containers. We will also touch a little on the commands for using Docker.

What is Docker?

Docker is a platform that can “package” an application, its dependencies, middleware, and so on into a so-called “container”, after which you will be able to deploy this container on any server that has Docker installed - literally in a split second, with one command.

Thanks to these, several tasks are solved at once - first of all, the process of launching the application on the server is greatly simplified, and secondly, any bugs in the containerized application will not affect the server itself in any way, just as specific server settings will not affect the application.

And although it seems that Docker looks and works like a virtual machine, in fact they are very different: the virtual machine emulates the entire server, including all hardware resources, and the container isolates the application, processes, users and the file system. At the same time, all containers use a common Linux host kernel and run in native mode only on Linux machines, but on the same machine you can run in about 5-6 times more containers than virtual machines. The diagram below shows the differences:

Installing Docker

As mentioned at the beginning of the article, we will install Docker on CentOS 7 - the installation process is extremely simple and fast.

So first you need to install with yum multiple packages:

Yum install -y yum-utils\device-mapper-persistent-data\Lvm2

Yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo

Then install Docker itself:

Yum install docker-ce

And finally, we start Docker:

systemctl start docker

We check that Docker is up and running in two steps:

systemctl status docker

You should see the following output:


After that, we try to deploy the hello-world container:

Docker run hello-world

If all the steps were performed correctly, then the following should appear on the screen:


Portainer installation

Portainer is a very user friendly GUI for managing Docker or Docker Swarm. It is installed in almost one action - since it is also a container itself. So:

Create markup for Portainer:

Docker volume create portainer_data

And then we start the container itself:

Docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

Then go to the network address of your server on the port 9000 , and you should see a window prompting you to set an administrator password:



After that, you will be greeted by a beautiful dashboard:


I suggest you try to deal with all the variety of the dashboard yourself and ask us questions in the comments - and for now we will demonstrate a few features.

So first click on Containers- you will see all available containers with information about them:


As you can see, we have this moment only one container is running - Portainer, and access to it is open on port 9000 (Published Ports column), and the address in the Docker internal network is 172.17.0.2.


Let's go to the Httpd tab:


First, name this container something - we named test-merionet. Then you can click on Show advanced options and you will see the option to select which port, protocol and volume will be used by this container. Then just click on Deploy the container.

It will take just a few seconds and you should be thrown back to the Containers tab, but with the second container already running:


From here you will see that the httpd server is available at 32768 port. So, let's try to access this server through a browser:


You should see the inscription It works! just like in the screenshot above - we leave the further configuration of httpd behind the scenes for now.

Docker customization and useful commands

So, you have already got acquainted with Docker and got an idea of ​​its capabilities. Below in the text we will describe the actions that also need to be done after installation and some commands that are literally hard to live without if you actively use Docker.

First of all, set up autostart for the Docker service:

Systemctl enable docker

Then, you can check the running containers in the console (in case you don't like the idea of ​​using a GUI) with the command

docker ps

Now a little about commands and syntax - we will show it with examples:

Let's say we need to start CentOS and execute the echo command in it:

Docker run centos echo “Hello from Merion Networks”

Launch CentOS and connect to its terminal:

docker run -t-i centos/bin/bash

You can immediately specify the required ports using the -p switch and run the container in the background using the --d switch:

Docker run -p 80:80 --d nodejs-app

So, quite a bit about the options for the docker run command - full list can be found at https://docs.docker.com/engine/reference/commandline/run/#description

  • -p- open specific ports separated by a space - access port and port on the container, for example docker run -p 9876:80% imagename%
  • -P- open all ports at once;
  • -t- connection to the container terminal;
  • -i- interactive mode, STDIN will be open all the time;
Be sure to check out Docker Hub as there are a bunch of cool containers there with examples of how to install them and access to a Docker file, it's kind of like GitHub for containers only.
Conclusion

That's all, thanks for your attention! Write in the comments what else you want to know about Docker- in the following articles we will cover topics such as: creating your own Docker file and image, how to connect a file system folder from your host, interesting things on Docker Hub and so on.

Is this article helpful to you?

Please tell me why?

We are sorry that the article was not useful to you: (Please, if it’s not difficult, indicate for what reason? We will be very grateful for a detailed answer. Thank you for helping us become better!

Docker is a platform for deploying and managing containerized applications. Containers are very popular among developers and administrators due to their flexibility.

Docker is made up of three main components:

  • docker engine
  • Docker Tools
  • Docker Registry

Docker Engine is a platform that provides basic container management capabilities. She interacts with the operating room Linux system, exposing simple APIs for working with containers.

Docker Tools is a set of command line tools that communicate with public APIs. They are used to launch containers, create new images, set up storage and networks, and perform many other operations that affect the lifecycle of a container.

Docker Registry is the registry where container images are stored. Each image can have multiple versions marked with unique tags. Users retrieve existing images from the registry and can add new images to it. Docker Hub is the main ledger of Docker, Inc. Docker allows you to create individual custom registries.

This tutorial will teach you how to install Docker, manage containers, work with images, and set up private registries.

Requirements

  • Ubuntu 16.04 server configured with .
  • Docker Hub account (you may find this guide helpful)

By default, root privileges are required to run the docker command. However, you can run the command without the sudo prefix by running docker as a user in the docker group.

To do this, run the command

sudo usermod -aG docker $(USER)

This will add the current user to the docker group. Then run the command:

This guide assumes that you can run the docker command on the server without the sudo prefix.

Step 1 Installing Docker

Once connected to the server via SSH, run the following commands to remove any existing Docker-related packages that may already be installed, and then install Docker from the official repository:

sudo apt-get remove docker docker-engine docker.io
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce

To test the installation, run this command:

This command shows the details of the current Docker installation. The following command verifies that Docker Tools is installed and configured correctly. It will output the version of the Docker platform and Docker Tools.

2: Starting the container

Docker containers are run from existing images, which are stored in a registry. Docker images can be stored in private or public repositories. To access a private repository, users must be authenticated. Any user can get access to public images without registration.

To find the hello-world image, run this command:

docker search hello world

The command can find multiple images with the same name. Choose the one with the highest number of stars, which indicates the popularity of the look.

Check the available images on your local environment with the following command:

Because you haven't started any containers yet, there won't be any images in the environment. Now you can download the image and run it locally.

docker pull hello world
docker run hello world

If you run the docker run command without first downloading the image, Docker Engine will automatically download and then run it. When you run the docker images command, you will see that you now have a hello-world image available locally.

Try running a more useful container, the Apache web server.

This command has additional options:

  • -p - opens port 80 in the container, which Apache is listening on.
  • --name Gives a name to the running container. If you skip this option, Docker will give the container a random name.
  • -d - this option starts the container in separate mode. Otherwise, the container will run in the foreground, blocking shell access. By bringing the container into the background, you can continue working in the shell.

To make sure the container is running in the background, type:

The command will report that the web container is running and its port 80 is mapped to port 80 of the host.

Now go to the web server:

Stop and remove the running container with these commands:

docker stop web
docker rm web

Run this command to make sure the container is removed:

Step 3: Adding Storage to a Container

Containers are ephemeral: anything stored in a container will be lost once it is stopped. To persist data after the container is stopped, you must attach a volume to the container. Volumes are directories from the host file system.

First, create a directory on the host:

Start the container by adding a new option to the command that will mount the htdocs directory with the Apache root directory:

docker run -p 80:80 --name web -d -v $PWD/htdocs:/usr/local/apache2/htdocs httpd

The -v option links the container to the htdocs directory in file system host. Any changes made to this directory will be reflected in both locations.

Enter the directory from the container by running the command:

docker exec -it web /bin/bash

This command will connect the terminal to the container shell interactively. You should see that you are now inside the container.

Go to the htdocs folder and create a simple HTML file. Then exit the shell to return to the host:

cd /usr/local/apache2/htdocs
echo "

Hello World from Container

" > index.html
exit

The curl localhost command shows that the web server is returning the page we created earlier.

Not only can you access this file from the host, but you can also change it:

cd htdocs
cat index.html
echo "

hello world from host

" | sudo tee index.html >/dev/null

The curl localhost command will confirm that the web server is serving the last page created on the host.

Now stop the container with the following command (the -f flag terminates Docker).

docker rm -f web

4: Build images

In addition to the existing images from the registry, you can also use your own images and store them in the registry.

You can create new images based on existing containers. First, all changes made in the container are fixed, then a unique tag is assigned to the image, after which it is moved to the registry.

Start the httpd container and modify the default document:

docker run -p 80:80 --name web -d httpd
docker exec -it web /bin/bash
cd htdocs
echo "

Welcome to my Web Application

" > index.html
exit

The container now contains a custom index.html file. You can check it with the curl localhost command.

Before unloading the modified container into the registry, it is recommended to stop it. After stopping it, you need to run the commit command:

docker stop web
docker commit web doweb

Check the image creation with the docker images command. It will show the newly created doweb image.

docker login
docker tag your_docker_hub_username/doweb
docker push

You can check if your image is on Docker Hub using a browser search or on the command line.

Step 5: Creating a Private Registry

To ensure the security of images, you can create a private registry. It also reduces latency between the Docker framework and the image repository.

The Docker Registry is available as a container that can be run just like any other container. Since the register contains many images, it is recommended to add a volume to it to store this data.

docker run -d -p 5000:5000 --restart=always --name registry -v $PWD/registry:/var/lib/registry registry

Note that the container runs in the background with an open port of 5000 and a registry directory that is reflected in the host's file system. You can verify that the container is running by running the docker ps command.

Now you can mark the local image and upload it to the private registry. First, download the busybox container from Docker Hub and tag it:

docker-pull-busybox
docker tag busybox localhost:5000/busybox
docker images

The busybox container now has a localhost:5000 tag. Upload the image to a local private registry:

docker push localhost:5000/busybox

After that remove the image from the environment and try loading it from the local registry:

docker rmi -f localhost:5000/busybox
docker images
docker pull localhost:5000/busybox
docker images

In some cases, it becomes necessary to run a private registry on a dedicated host. The Docker Engine running on another machine will interact with the remote registry, allowing you to add and download images.

Because the registry is not secure, you need to change your Docker Engine configuration to allow access to the insecure registry. To do this, edit the daemon.json file located at /etc/docker/daemon.json. Create the file if it doesn't exist.

Add the following lines to it:

{
"insecure-registries" : ["REMOTE_REGISTRY_HOST:5000"]
}

Replace REMOTE_REGISTRY_HOST with the hostname or IP address of the remote registry. Restart Docker to update settings.

Conclusion

Now you know how to install Docker and perform basic operations with images and containers.

Tags: ,

Original: Installing Docker on Windows - excerpt from the documentation
Translation: N. Romodanov
Date of transfer: January 2015

Note: Docker has been tested on Windows 7.1 and 8; it may also work on older versions. The processor must support virtualization in hardware.

The Docker engine uses specific features of the Linux kernel, so to run it on Windows, we need to use a lightweight virtual machine. You can use the client Windows program Docker to manage the virtualized Docker engine that builds, runs, and manages Docker containers.

To make this process easier, we have developed a helper application called Boot2Docker that installs a virtual machine and starts the Docker daemon.

Installation

The Boot2Docker Start script will connect you to a shell session in the virtual machine. If necessary, a new virtual machine will be initialized and launched.

Update

  1. Download the latest Docker installer for Windows
  2. Run the installer which will update the Boot2Docker application.
  3. To upgrade an existing virtual machine, open a terminal and run the commands: boot2docker stop boot2docker download boot2docker start

Running Docker

With Boot2Docker, you will immediately be registered with Docker, so you can start using it right away.

Let's try an example hello-world image. We launch

$ docker run hello world

This will download a very small hello-world image and issue a Hello from Docker message.

Additional details

The Boot2Docker management tool allows you to use several of the following commands:

$ ./boot2docker Usage: ./boot2docker [<параметры>] (help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|delete|download|version) [ ]

Container port forwarding

In case you're wondering, the default boot2docker username is docker and its password is tcuser .

The latest version of boot2docker configures a single network adapter with the host system, which provides access to container ports.

If you start a container with the following open port:

Docker run --rm -i -t -p 80:80 nginx

then you will be able to access that nginx server by IP address, which is done like this:

boot2docker ip

Typically, this address is 192.168.59.103, but this may be changed in a specific Virtualbox DHCP implementation.

See the Boot2Docker site for more information or to report a problem.

Introduction

Docker is a container management system; a platform that is used to run and work with applications in containers. The use of containers is popular among developers, network administrators, and others because of the flexibility that containers offer.

Docker has three important components:

  • docker engine- a client-server application that provides basic container handling capabilities;
  • Docker Tools- a set of command line tools that are used to launch containers, create new images, etc.;
  • Docker Registry- the place where the container images are located.

Requirements

In order to perform the necessary steps, you will need:

  • installed Ubuntu 16.04 OS with a user that can execute sudo commands and a firewall;
  • Docker Hub account.

By default, you must have root privileges to run the docker command. However, the command can also be run without adding sudo if you use a user that is in the docker group.

This guide assumes that the server is configured to run the docker command without using sudo.

For this setting you need to enter:

$ sudo usermod -aG docker $(USER)

The current user will be added to the docker group. For the changes to take effect, run the command:

$su - $(USER)

Step 1: Install Docker

First, you need to remove any Docker packages that may have been installed previously:

$ sudo apt-get remove docker docker-engine docker.io

And then move on to installing Docker itself:

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

After that, verify that the installation has taken place with the following command:

$dockerinfo

It shows information about the Docker Engine deployed in the environment. And to see the version of Docker Engine and Tools, type:

$docker version

Step 2: Run the Containers

Docker containers are run from existing images that are in the repository. Images can be stored in private or public repositories. When using private repositories, you will definitely need to use authorization. And everyone has access to public repositories.

To find an image called hello-world, use the following command:

$ docker search hello world

There can be several images with a suitable name. Pick the one with the most stars (the stars represent the popularity of the look).

Check the availability of images in your local environment with the following command:

$ docker images

Since you haven't started any container yet, there are no images yet either. Download and run the image:

$ docker pull hello world $ docker run hello world

If you issue the docker run command without downloading the image, the Docker Engine will still download the image first and then run it. If you run the docker images command again, you will see that the hello-world image is now available locally.

Now you can start some more serious container, for example, the Apache web server:

Here you can see a few additional keys:

  • -p Specifies which specific host port will be bound to the container port (in this case is 80);
  • --name Specifies the name of the running container. If you do not specify this parameter, then the Docker Engine will assign a random name;
  • -d - indicates that the container will run in the background.

Enter the command below to make sure the container is running:

$ docker ps

You will see in the output that a container called web is running on port 80 and is bound to port 80 of the host.

To stop and remove a running container, use the commands:

$ docker stop web $ docker rm web

Run the docker ps command to verify that the container has been removed.

Step 3: Add Storage for Containers

Containers themselves are ephemeral, meaning whatever is inside a container will be lost once that container is deleted. In order for the data to exist longer than the container, a volume must be added to it. The volume is used for long-term storage of the necessary files (data). A volume is a directory on the host's file system.

First you need to create a new directory:

$ mkdir htdocs

Now you need to start the container and mount it with the htdocs directory:

$ docker run -p 80:80 --name web -d -v $PWD/htdocs:/usr/local/apache2/htdocs httpd

Enter the command to access the directory from the container:

This command binds a terminal to a container shell interactively. You will see that you are now inside a container. Go to htdocs and create some simple html file there:

$ cd /usr/local/apache2/htdocs $echo"

Hello World from Container

" > index.html

Then exit the shell:

$exit

Running the curl localhost command will show that the webserver is returning the page you just created.

This file can not only be accessed, but also edited:

$ cd htdocs $ cat index.html $echo"

hello world from host

" | sudo tee index.html >/dev/null

If you now type curl localhost, you will see that the web server is using the latest version of the site.

Now remove the container with the command below:

$ docker rm -f web

Step 4: Create Images

You can not only run existing images, but also create your own and store them in a repository.

New images can be created from existing containers. Run the httpd container again and change the initial document:

$ docker run -p 80:80 --name web -d httpd $ docker exec -it web /bin/bash$ cd htdocs $echo"

Welcome to my Web Application

" > index.html$exit

The container is launched with an edited index.html file. You can check this with the command curl localhost .

Before turning a container into an immutable image, it's best to stop it:

$ docker stop web

But after that, you can already use the commit command, which will actually turn the container into an immutable image.

$ docker commit web doweb

Confirm the creation of the image with the docker images command. It will show the doweb image that has just been created.

You can tag the image and also push it to Docker Hub. To do this, run the following commands (instead of your_docker_hub_username you need to write the username of your Docker Hub):

$dockerlogin $ docker tag your_docker_hub_username/doweb$ docker push

You can check that everything is done correctly by searching for a new image in Docker Hub in a browser or on the command line.

Step 5: Launching a Private Repository

If you want the images to be more secure, it's better to use a private repository.

The Docker Registry is available as a container and can be run just like any other container. Since a repository can contain many images, it's a good idea to attach a storage volume to it.

$ docker run -d -p 5000:5000 --restart=always --name registry -v $PWD/registry:/var/lib/registry registry

Note that the container is running in the background using port 5000 and the registry directory is mapped to the host's file system. Use the docker ps command to make sure the container is running.

Now you can tag the local image and push it to the private repository. But before that, you need to download the busybox container from Docker Hub and assign a tag to it.

$ docker pull busybox $ docker tag busybox localhost:5000/busybox$ docker images

The command above confirms that the busybox container now has the localhost:5000 tag, so push the image to a private repository:

$ docker push localhost:5000/busybox

Now that the image has been submitted, you can do the following - remove it from the environment, and then download it again from the repository:

$ docker rmi -f localhost:5000/busybox$ docker images $ docker pull localhost:5000/busybox

Conclusion

In this tutorial, you learned how to install Docker and then work with Docker containers and images. For more information, I recommend checking out

Similar posts