๐ณDocker
Introduction to Docker
Docker cheatsheet here!
Docker Intro
Docker is a software container platform. Docker is used to run applications. to develop Docker is an open source platform designed to ship. Docker will create a unique environment for each of your applications.
What is Docker?
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. Containers are lightweight, standalone, executable packages that include everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
Docker allows developers to package an application with all of its dependencies into a standardized unit, called a container, for software development. This enables developers to easily move the containerized application between different environments, such as from development to test and then to production, without any changes.
References
Please note that this is just a basic course for introduction to Docker and its fundamental concepts, for more in-depth understanding and usage of Docker please refer to the official documentation and other resources.
Benefits of using Docker
Isolation: Containers are isolated from each other and from the host, so one container's changes do not affect other containers or the host.
Portability: Containers can run on any host that has Docker installed, making it easy to move from development to test and then to production.
Scalability: It is easy to scale applications horizontally by adding more containers.
Efficiency: Containers take up less space than traditional virtual machines and are more efficient in terms of resources.
Docker Engine
Docker Engine is the core of Docker and creates Docker containers. shipping Run, etc. Docker Engine according to the Client-Server architecture A server daemon process keeps running The rest of the API connects to the daemons and sends instructions to the daemons. Acting as a Command Line Interface (CLI) etc.
Docker Features
Docker container
A Docker container is used to package an application. It is a special environment for running. In order to get faster and better computing, Docker can run an application side by side. And you can run more than one container on a single host. Only those containers can be moved from the running host to another host.
Docker installation on Ubuntu
If you are concerned about security, please be aware that the following packages are unofficial.
docker.io
docker-compose
docker-compose-v2
docker-doc
podman-docker
To install Docker on ubuntu OS, type the following commands individually in the terminal.
First, we will update the existing packages.
Then install the necessary packages.
Then add the Docker repository.
The last step is to update and install docker.
Type this to see if the Docker service is running.
Active: active
If so, you can find out that the docker service is running.
Docker cli is installed together with Docker daemon service, so you can use docker cli. You can view Docker commands like this.
But when docker is installed, only the root user can type commands related to docker. If he runs a command with docker, he can only use the sudo command. For example..
When installing Docker, docker will create a linux user group called docker. If you don't want to always enter sudo, you can use it without having to enter the sudo command by adding the current user to the docker group.
Then restart the docker service.
If the Docker service becomes active, you can start using docker.
Docker Container
A Docker container is a small instance created when a docker image is run. A container is a combination of libraries and settings that are needed to make the application work. It's a very thin environment for an application, and it's small enough to move around easily.
Run Docker Container
The docker run command is used to run a Docker Container on the system. For example - typing the following command will build a docker container using the hello-world image.
Maintenant... nous allons construire un conteneur Docker qui fonctionnera avec le systรจme CentOS-Betriebs. L'option -it fournit une session interactive oรน le pseudo-TTY peut รชtre utilisรฉ. ร partir de lร , vous pourrez utiliser le shell du conteneur immรฉdiatement.
List Docker Containers
If you want to list all the containers running on the current system, use the docker ps command. This command will not display the list of stopped containers. It will show the Container ID, Container name and other useful information about the container.
If you add the -a option to the command above, the list of stopped containers will be displayed.
Find all Details of Container
When you want to find all the details about a Docker container, you use the docker inspect command. If you want to know the details of the container, you must enter the container ID or container name of the container you want to know.
Delete Docker Container
If you want to delete the docker container in the system, use the docker rm command. If you want to delete a container, you must enter the exact container ID or container name of the container you want to delete.
You can also use docker container prune
instead of docker rm cc5d74cf8250 .
Docker Images
You can check the images that can be used in your system with the docker images command.
Search Docker Images
The docker search command is used when searching for the desired image from docker hub. For example if you search for images for WordPress...
Download Docker Images
If you find the desired image, you can download it to your machine with the docker pull command. For example, if you download the latest version image for WordPress from Docker hub.
Delete Docker Images
To delete images that are no longer needed, we use the docker image rm
command.
Dockerfile
Working with Dockerfile
A Dockerfile is, as the name suggests, a file. It has specific instructions to build the customized images you want with those instructions. By default, you must save the name as Dockerfile.
Build Image with Dockerfile
This is the command to build the image with the already written dockerfile. -t
represents the tag name, followed by the image name. Make sure there is a .
at the end of the command. It means that it will take the Dockerfile in the current working directory and use it. If you only have a Dockerfile. If it is still there, or if the first D in the Dockerfile is only a small letter (d), an error may occur.
This command is an image build command. The special thing is that the -f flag is used. If you want to call a Dockerfile somewhere in your file system other than the current working directory, you must use this.
Create a Dockerfile
You can create your own Dockerfile like a bash script.
After using the command above, an image has been built. You can call the already built images with the docker images command.
Launch Container with Image
Build a container using the already built image with this command. i stands for interactive and t stands for tty. -p is to set the port. In this example, you will see that you have used port 8080 of your host system and port 80 of the container. You must write a colon ( : ) between the port of your host system in the front and the port of the container in the back.
You can also run the image as a deamon with -d
.
Dockerfile Directives
What are Dockerfile Directives
Before I built an image using a Dockerfile, and then I saw how to build containers using that image. Now I will talk about how to write that Dockerfile. Dockerfile is written with Docker directives.
FROM
This FROM directive is used to get the base image. For example, if you want a container that can use ubuntu commands, you must use FROM ubuntu. By default, it will use the latest version from the ubuntu versions in the docker store.
LABEL
This is a label as the name suggests. Maintainer address, vendor name, image version, release date, etc. For example
Just hit it. What I want to recommend is to write a single line with a space in one line. At the time of image build, one line is built one layer at a time. The fewer layers, the faster.
RUN
RUN is used to run necessary commands. For example, when you need to install necessary packages.
Here, it should be written in one line like above. The less layers the better.
COPY
I use this if I want to copy the files and directories in my device to the image that will be built.
If you type the first command, all the files under the host's html directory will be copied to /var/www/html/ of the image.
The second command is to put all the host's .conf extension files under the image's /etc/apache2/sites-available/.
WORKDIR
This directive is used to specify the working directory of other directives of dockerfile such as RUN, CMD, ENTRYPOINT, COPY, ADD.
CMD
The CMD directive is used to launch and run the services and software in the image. Its syntax is...
If you want to run apache service.
EXPOSE
This is a directive that refers to the container's port. When connecting the port with docker run -it -p found above, you must connect with the port indicated here.
ENV
The ENV directive is used if you want to set an environment variable.
VOLUME
Finally, the VOLUME directive. I use it to create a mount point. It should be noted that it is an externally mounted volume.
Docker Port
Manage Ports in Docker
In Docker containers, services usually run on a separate port. If you want to use the container's services running on a port, you can bind the container's port to any port of the Docker host.
Example 1
See the picture below. You will see two containers running in the Docker host. The first one is the Apache container that runs the website, and the second one is the MySQL container.
Now we need to access the website running on port 80 Apache container. So let's bind Apache container port 80 from Docker host port 8080. You can also bind to Docker host port 80. The second container is running MySQL on port 3306.
You can access MySQL from the host machine in other ways. But for this tutorial I bind MySQL container port 3306 to docker host port 6603. Now we can directly connect to the MySQL container using port 6603 of the host machine.
The command below is the command to bind the host system port and the container port.
Example 2
For this example, we'll use our example project on GitHub. Clone the repository using the command below.
Now build the docker image with the name apacheimage.
Now run the container using the Docker run command. Apache service will run on container port 80. -p 8080: 80 must be set to bind the host system port 8080 to the container port 80.
Now, if you access using the host machine ip and port 8080 in the web browser, it will appear on the web page running on the container's Apache service as shown below. My police host machine ip is 192.168.1.5.
Examples
We can bind multiple ports with a single container. But before building the image, all ports must be EXPOSED in the dockerfile.
If you really want to bind with any interface of the host machine, you can assign the IP address as below. In the example below, ports 8080 and 8081 can only be accessed by 127.0.0.1 IP.
Docker Networking
Docker Networking
Docker provides create and manage functions to communicate with docker containers and networks. You can manage the Docker network using the docker network command.
Syntax:
By studying the following tutorial, you can create, list and manage Docker network features.
Listing Docker Networks.
You can list the docker networks on the docker host using the ls
option.
Creating a Docker Network.
A variety of network types are supported by Docker. You can create a bridge network on your system using the following command.
Syntax:
Example:
Connecting the Container to the Network.
Any container can be connected to an existing docker network using the Container Name (or Container ID). Just by connecting a container to the network once, other containers can communicate on the same network.
Syntax:
Example:
Disconnecting Container from Docker Network.
If you want to disconnect the container from a network, you can use the following command.
Syntax:
Example:
Viewing information about a Docker Network.
If you want to see the details of a Docker Network, you can use the inspect option.
If you look at the details of a Docker Network using the inspect option, you will see something like this.
Removing Docker Network.
If you want to remove the Docker network, you can use the rm option.
If you want to remove more than one docker network, you can remove the network ID (or network name) with a space.
Example:
If you want to remove all unused networks on docker, you can remove them using the prune option.
Docker Networking Example
If you have read the Docker Network tutorial, you can try the example.
In this tutorial, we will make two docker containers and a small docker network.
MySQL โ A relational database server.
PHPMyAdmin โ A web based interface to manage MySQL server.
In this tutorial, we will show you how to use PHPMyAdmin running in another container to access another MySQL server.
Creating a network.
First, create a new docker network. Create a new network named my-bridge-network using the following command.
Running the MySQL Container.
Now we run the new MySQL docker container.
To set the default root user's new password, type MYSQL_ROOT_PASSWORD variable as shown below.
After creating a container, it will connect to the my-bridge-network network that we created earlier.
The next step is to look at the new IP address of the MySQL container.
Running the PHPMyAdmin Container.
Now we will run the new Docker container, phpmyadmin.
I will add the MySQL container IP address obtained in the last step of running MySQL as PMA_HOST value.
Then add the phpmyadmin container to my-bridge-network.
My-bridge-network Viewing network information.
We added the two containers shown above to my-bridge-network, so let's take a look at the current setting of my-bridge-network.
If you look at the setting of My-bridge-network, you should see this.
Allow MySQL to PHPMyAdmin Host
By default, MySQL does not allow remote hosts to connect. So we allow phpmyadmin for MySQL connection Must do. To get MySQL container shell access, you must do the following.
Log in to the MySQL server using the password you provided when you created the MySQL container.
Create a new user with phpmyadmin host ip address. In this tutorial, the phpmyadmin host ip address is '172.21.0.3'.
Access MySQL with PHPMyAdmin
Finally, our docker host system can connect to the phpmyadmin web user interface through port 8080. phpMyAdmin can be used to login using MySQL information as shown above.
Docker Compose
Docker Compose is a tool used to setup containers. Using Docker Compose, you can create docker containers as a Compose File. Images and Containers can also be easily built with a single command. There are 3 steps to do Docker Compose.
The Services to be used must be specified in the Dockerfile.
The service and application that will be used for your environment must be made as a docker-file and saved in sample.yml format.
You can run Docker Containers Services with Run docker-compose up command.
The machine needs to have Docker Enginer. If not, you can learn in the Docker Engine Installation Section.
Install Docker Compose
To install Docker Compose, visit https://github.com/docker/compose/releases (Github Page). You can also install Docker compose 1.16.1 with the following command. Before installing, you need to check whether the Docker version is specific.
Docker Compose Example File
The Docker Composer file is docker-compose.yml (format), and below is a sample of the Version 3 docker composer file. This file only shows a WEB Name which is a sample service.
Docker Compose CLI Reference
The docker-compose command also provides subcommands to manage Docker Compose and Docker Containers. Below you can learn some subcommands. Be careful not to mistake the Container Name and Services Name.
build -
With the build option, you can build images and use services.
up โ
Create docker container and Services from docker-composer.yml under Current Directory. (-d) Switch is to run the container in daemon mode.
down โ
Option can be used to stop and delete Network, Container Service and Associate Images of containers.
ps โ
Used to know process details of services, status and ports of containers.
exec โ
Used to exec running containers. For example, to see the Container running the Web Service as a list-file..
start -
Used to start containers.
stop -
Used to stop running containers.
restart โ
Used to restart containers.
pause โ
Used to pause containers.
rm โ
Used to delete and remove containers.
Docker Compose Example
Step 1 โ Create Directory Structure
First, we will create a directory named docker compose. Next, we will create a directory named webapp to store the web application. We will build index.html to test the web application in the webapp directory.
Step 2 โ Create Dockerfile for Webapp
After that, we will build the dockerfile required for the web application in the webapp directory. The dockerfile is for building a customized image that includes the apache web server needed for the web application.
Then add the following code.
Step 3 โ Create Docker Compose File
Next, we will build a docker configuration file named docker-compose.yml in the current directory. The configuration file will represent all the containers that will be used.
Then add the following code.
The above file is for two containers. The first container is for the mysql database server and the second is for the web server. Web container will run applications on apache server. The webapp directory is set as the build directory.
Step 4 โ Build Webapp Image
With the following command, we will build an image named apache using the Dockerfile and the contents of the webapp directory.
Step 5 โ Launch Docker Containers
We will start the containers using docker-compose up. You can use the -d option to use Daemon mode.
Step 6 โ Update Content in Web Application
If you want to make changes to the web application.
Then use the following commands to rebuild the webapp container and start working.
Docker Machine
Working With Docker Machine
Docker Machine is a command line tool for provisioning and managing Dockerized hosts. In the simplest terms, Virtual Machines can be installed on a local or remote system with Docker Engine. Docker Machine can also work well on platforms like Virtualbox, VMware, Digital Ocean and Amazone AWS.
Install Docker Machine
How to install Docker Machine is listed below. And you can check the latest Docker Machine Version at https://github.com/docker/machine/releases.
For Linux Systems:
You can download and install Docker Machine using the following command.
For OSX Systems:
You can download and install using commands like.
For Windows Systmes with Git Bash:
We recommend using it only on Windows 10 and above.
You can download and install using commands like.
Docker Machine Supported Drivers:
Docker Machine provides drivers not only for the local systems of the following services, but also for Cloud Systems. You can use any of the following hosting services to launch Dockerized hosts and manage them with a single Docker Machine.
Amazon Web Services
Microsoft Azure
The next is coming soon ! ๐
Digital Ocean
Exoscale
Google Compute Engine
Generic
6Microsoft Hyper-V
OpenStack
Rackspace
IBM Softlayer
Oracle VirtualBox
VMware vCloud Air
VMware Fusion
VMware vSphere
Docker Prune
Prune Objects in Docker
Normally, docker doesn't delete objects that it no longer uses until you tell it to delete them. Here, objects are docker-related images, containers, volumes, and networks. So it has an option to delete unused objects. This is the docker prune command.
Syntax:
Prune all unused Objects.
The command below will remove containers, images, volumes and networks that are no longer used by docker.
--โall option
It means everything related to unused docker.
--filter
The option can be used with key=value. For example, the command below until = 24 hours means images that were built before 24 hours ago. Containers that are stopped It says that networks that are no longer in use will be deleted.
Prune Images
The command below can be used if you want to delete unused images only.
Prune containers
If you want to select and delete only the Stop/Exited Containers, you can use the command below.
Prune Volume
When you want to delete volumes that are no longer in use, you can use the following.
Prune Network
Just like the command above, you can also use prune when you want to remove networks.
Conclusion
If you're sure you want to delete without asking Yes or No questions, you can use -f as a force option from behind.
Depending on the situation, you can use the options as you like.
Reference from tecadmin.
Last updated