Docker Complete Guide: all you need to know about Docker and Docker containers
Want to know what a Docker container is? In this guide we’ll learn everything about Docker and find out what a Docker container is. We’ll also tell you how to run Docker containers and what’s the difference between docker images and containers.
If you want to really understand the thought process behind Docker, there’s no better source than the person who created it - Solomon Hykes, founder and CTO of dotCloud. Although this YouTube introduction was uploaded 7+ years ago, it is still perfectly relevant.
That said, you might not have 47 minutes to spare on watching the full presentation, especially since it’s pretty technical and might require multiple viewings. That’s why we’ve created this quick guide for you.
What is docker?
Docker is a lightweight, open-source virtualization tool.
Here’s the description on Docker’s official website:
“Docker takes away repetitive, mundane configuration tasks and is used throughout the development lifecycle for fast, easy and portable application development - desktop and cloud. Docker’s comprehensive end to end platform includes UIs, CLIs, APIs and security that are engineered to work together across the entire application delivery lifecycle.”
Docker runs at the operating system level. It automates the deployment of applications in Linux containers, and allows you to package an application with all the necessary dependency structures (code, runtime, libraries, environment variables, configuration files) into a container.
In his presentation, Solomon breaks down the unique proposition that distinguishes Docker from other solutions out there:
"A lot of [container] tools use containers as miniature servers… just like a VM [virtual machine] but way faster…. We [Docker] use containers as a unit of software delivery."
What are Docker containers and what are they used for?
A container is an isolated environment whose processes do not interfere with operations outside of it.
The container only uses a portion of the operating system. This allows you to significantly reduce the consumption of system resources by allocating the application and its data exclusively to the container, rather than to the entire operating system as in the case of a virtual machine.
This makes Docker containers particularly suited to rapid application deployment, ease of testing, maintenance, and troubleshooting, while enhancing security.
One of the practical benefits of Docker containers is simplifying big development team work. Tasks can be easily split up and implemented in different programming languages.
Common use cases for Docker include:
Automating the packaging and deployment of applications
Creating lightweight, private PAAS environments
Automating testing and continuous integration/deployment
Deploying and scaling web apps, databases and backend services
How does a container work?
There are five basic tenets of Docker containers:
The lifespan of a container is tied to the lifespan of the process contained within it.
Inside the container, this process has pid = 1, which means it is the parent process that starts before all other processes.
Alongside the process with pid = 1, you can spawn as many other processes as you wish (within the limitations of the OS). Killing (restarting) the process with pid = 1 stops the container. (see item 1)
Inside the container, you will see the usual FHS-compliant directory layout. This location is identical to the source distribution (from which the container is taken).
The data created inside the container remains in the container and is not saved anywhere else. The host OS has access to this layer, but deleting the container will discard all changes. For this reason, the data is not stored in containers, but taken out to the host OS.
How to create a Docker container
In this guide, we’ll be showing you how to install Docker on Ubuntu 20.04 - the most popular repository.
We recommend you to use official Docker files for installation, you can find all the needed info here. No complicated configuration is required at this stage. Once it’s finished installing, start the service, check its status and set it to start at boot:
*sudo apt-get update
sudo apt-get install \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu
\$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
systemctl status docker*
Next, run a test image to check that Docker is working correctly:
docker run hello-world
You should see the following message:
“Hello from Docker. This message shows that your installation appears to be working correctly."
How to list Docker containers
To avoid having to recognize and remember the container ID, you can assign a unique name to each container by using the -name command line option when creating it. See the example below:
docker run --name myname ubuntu cat / etc / debian_version
Once you’ve done this, you can start using the container (execute the start, stop, remove, top, stats commands) by referring to it by name, for example:
docker start myname – container start
docker stats myname – display resource usage statistics
docker top myname – displays the processes running in the container
How to start and restart Docker containers?
To start the container in daemon mode, use the ‘-d’ option.
docker run -td ubuntu bash
To reconnect to a running container, you need to know its ID or name. Run the Docker ps command, followed by “attach” and the container’s ID or name.
docker attach CONTAINER
How to stop Docker container
A container is usually terminated automatically after the completion of the process. However, there might be instances where you want to terminate the container yourself. The stop command performs a "soft" shutdown of the container, by default allowing 10 seconds for all processes to terminate:
docker container stop CONTAINER
If you want immediate termination, you can execute the kill command. However, in most situations, using stop is preferable.
docker container kill CONTAINER
Here’s the command for immediate termination of all running containers:
docker container kill $ (docker ps -q)
How to delete a container
To delete a Docker container, use the following command:
docker container rm CONTAINER
Here’s how to remove all Docker containers that are not running:
docker container rm $ (docker ps -a -q)
How to run Docker containers
To interactively connect to the shell of the container and run commands, as in a regular Linux system, you need to create a container with the following settings:
docker container run -it ubuntu bash
The structure of this command is as follows:
-i sets the start of an interactive session.
-t allocates TTYs and includes standard input and output streams.
ubuntu is the image used to create the container.
/bin/bash is a command run in an Ubuntu container.
After starting the container with the above settings, we sort of fall into the container. Use the “exit” command to exit a running session and return to your node's terminal. This interrupts all container processes and stops the container:
If you are interactively connected to a container and need to log out without interrupting the session, you can exit the console and return to your host's terminal by pressing Ctrl + P and then Ctrl + Q.
There are several services that help users to easily deploy Docker containers with just a few clicks. Hostman has swiftly earned a reputation for being the go-option when it comes to GitHub, Bitbucket, or GitLab repositories.
How to SSH into a Docker container?
For SSH-authentication, or when connecting remotely (for example, rsync), the main methods are a login-password pair (the password is entered from the keyboard in the console) and key authorization (a private-public key pair is created on the server and the public key is transmitted to the remote server). The first method cannot be used in any way in scripts executed on the crown, and it is necessary to configure a transparent input.
Generating keys for the current user:
ssh-keygen -t rsa
After entering this command you’ll be asked some questions. You can just agree with all the default options.
Copy the key to the remote server (enter the password of the remote server once)
ssh-copy-id -i ~ / .ssh / id_rsa firstname.lastname@example.org
Checking the ability to log in
How to connect to a running container?
If you have multiple Docker containers running and want to choose which one to work with, you will need to list them by using the ls command. In addition to displaying a list of containers, this command also displays useful information about them. The command without any settings displays a list of running containers:
docker container ls
The '-a' setting tells the command to list all containers, not just running ones, while the -s option displays the size of each container:
docker container ls -a
The inspect setting displays a lot of useful information about the container:
docker container inspect CONTAINER
To display container logs, run the logs command:
docker container logs CONTAINER
What is the difference between a Docker container and a Docker image?
Docker works with the following fundamental objects:
A container is an application environment. When a container is launched from an image containing the necessary configuration data, a new level with a variable structure is built on top of this image. If you save the changes, the new image level is saved and the old one remains unchanged.
An image is a static snapshot of a container's configuration state. The image is a permanent layer, all changes are made at the highest level and are saved only by creating a new image. Each image depends on one or more parent images.
Why are Docker images so valuable?
Docker images are valuable because they are used to create and conduct the environment to get started with Docker. First, you will need to download an image from the Docker Hub to your machine.
What benefits do containers offer?
When working with application containerization in Big Data, the following advantages of this technology are most significant:
Standardization – thanks to the base of open standards, containers can work in all major distributions of Linux, Microsoft and other popular operating systems;
The independence of the container from the resources or architecture of the physical host on which it runs, facilitates portability of the container image from one environment to another, providing a continuous pipeline of DevOps processes from development and testing to deployment (CI / CD pipeline);
Isolation – the application in the container runs in an isolated environment and does not use the memory, processor or disk of the host OS. This guarantees the isolation of processes inside the container and provides some level of security.
Reusability – all the components required to run the application are packaged into one image that can be run multiple times;
Fast deployment – creating and launching a container is considerably less time-consuming than using a virtual machine instance or setting up a full-fledged working environment;
Increasing labor productivity – having each microservice of a complex system packaged in a separate container for which one developer is responsible, makes it possible to parallelize work tasks without mutual dependencies and conflicts;
Simplified Monitoring – versioning container images makes it possible to track updates and prevent synchronization issues.
Supporting Big Data applications (yours or others’) that no longer fit in your head, doesn’t have to be a nightmare anymore.
With this handy guide to Docker, you’ll be able to cold-start an application on a new machine, with just a few clicks, in under a minute.
More importantly, with the reassurance that all of your data is safe, you’ll finally be able to focus exclusively on writing useful code instead of wasting time and energy on server-related troubleshooting.
Like this article? Spread the word.