๐Ÿณ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.

$ sudo apt-get update
  • Then install the necessary packages.

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  • Then add the Docker repository.

$ 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"
  • The last step is to update and install docker.

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  • Type this to see if the Docker service is running.

$ sudo systemctl status docker

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.

$ docker
Output:


Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:

  attach      Attach local standard input, output, and error streams to a running container
  build       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
  deploy      Deploy a new stack or update an existing stack
  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
  login       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 an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  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      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

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..

$ sudo docker image ls

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.

$ sudo usermod -aG docker ${USER}

Then restart the docker service.

$ sudo systemctl restart docker

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.

$ docker run hello-world

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.

$ docker ps

If you add the -a option to the command above, the list of stopped containers will be displayed.

$ docker ps -a

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.

$ docker inspect cc5d74cf8250

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.

$ docker stop cc5d74cf8250
$ docker rm cc5d74cf8250

You can also use docker container prune instead of docker rm cc5d74cf8250 .

$ docker container prune

Docker Images

You can check the images that can be used in your system with the docker images command.

$ docker images

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...

$ docker search mysql

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.

$ docker pull mysql

Delete Docker Images

To delete images that are no longer needed, we use the docker image rm command.

$ docker image rm mysql

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

$ docker build -t image_name .

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.

$ docker build -t image_name -f /path/to/Dockerfile

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.

$ nano Dockerfile
FROM alpine
RUN apk update
RUN apk upgrade
CMD ["echo","Fastiraz!"]

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.

$ docker run -it -p 8080:80 image_name

You can also run the image as a deamon with -d .

$ docker run -dit -p 8080:80 image_name

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.

FROM alpine:3.17.1

LABEL

This is a label as the name suggests. Maintainer address, vendor name, image version, release date, etc. For example

LABEL maintainer="fastiraz@doc.io"
LABEL vendor="Fastiraz"
LABEL com.example.version="0.01"

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.

LABEL maintainer="fastiraz@doc.io" vendor="Fastiraz" \
com.example.version="0.01"

RUN

RUN is used to run necessary commands. For example, when you need to install necessary packages.

RUN apt-get update 
RUN apt-get install -y apache2 automake build-essential curl โ€‹โ€‹

Here, it should be written in one line like above. The less layers the better.

RUN apt-get update && apt-get install -y \
automake \
build-essential \
curl

COPY

I use this if I want to copy the files and directories in my device to the image that will be built.

COPY html/* /var/www/html/
COPY *.conf /etc/apache2/sites-available/

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.

WORKDIR /opt

CMD

The CMD directive is used to launch and run the services and software in the image. Its syntax is...

CMD ["executable","param1","param2"]
CMD ["ls","-lsa"]

If you want to run apache service.

CMD ["apachectl", "-D", "FOREGROUND" ]

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.

EXPOSE 80
EXPOSE 443

ENV

The ENV directive is used if you want to set an environment variable.

ENV PATH=$PATH:/usr/local/psgql/bin/ \
    PG_MAJOR=9.6.0

VOLUME

Finally, the VOLUME directive. I use it to create a mount point. It should be noted that it is an externally mounted volume.

VOLUME ["/data"]

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.

$ docker run -it -p 8080:80 apache_image
$ docker run -it -p 6603:3066 mysql_image

Example 2

For this example, we'll use our example project on GitHub. Clone the repository using the command below.

$ git clone https://github.com/tecrahul/dockerfile
$ cd dockerfile

Now build the docker image with the name apacheimage.

$ docker build -t 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.

$ docker run -it -p 8080:80 apacheimage

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.

$ docker run -it -p 8080:80,8081:443 image_name

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 run -it -p 127.0.0.1:8080:80,127.0.0.1:8081:443 image_name
$ docker run -it -p 192.168.1.111:8080:80,92.168.1.111:8081:443 image_name

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:

$ docker network [options]

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.

$ docker network ls

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:

$ docker network create -d [network_type] [network_name]

Example:

$ docker network create -d bridge my-bridge-network

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:

$  docker network connect [network_name] [container_name]

Example:

$ docker network connect my-bridge-network centos

Disconnecting Container from Docker Network.

If you want to disconnect the container from a network, you can use the following command.

Syntax:

$ docker network disconnect [network_name] [container_name]

Example:

$ docker network disconnect my-bridge-network centos

Viewing information about a Docker Network.

If you want to see the details of a Docker Network, you can use the inspect option.

$ docker network inspect my-bridge-network

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:

$ docker network rm my-bridge-network network2 network3

If you want to remove all unused networks on docker, you can remove them using the prune option.

$ docker network prune

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.

$ docker network create -d bridge my-bridge-network

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.

$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server

After creating a container, it will connect to the my-bridge-network network that we created earlier.

$ docker network connect my-bridge-network mysql

The next step is to look at the new IP address of the MySQL container.

$ docker inspect mysql | grep "IPAddress"

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.

$ docker run --name phpmyadmin -d -e PMA_HOST=172.21.0.2 -p 8080:80 phpmyadmin/phpmyadmin

Then add the phpmyadmin container to my-bridge-network.

$ docker network inspect 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.

$ docker network inspect 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.

$ docker exec -it mysql bash

Log in to the MySQL server using the password you provided when you created the MySQL container.

bash-4.2# mysql -u root -p

Create a new user with phpmyadmin host ip address. In this tutorial, the phpmyadmin host ip address is '172.21.0.3'.

mysql> GRANT ALL on *.* to 'dbuser'****@****'172.21.0.3' identified by 'secret';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

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.

$ curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

$ chmod +x /usr/local/bin/docker-compose

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.

version: '3'
services:
    db:
        image: mysql
         container_name: mysql_db
          restart: always
        environment:
            - MYSQL_ROOT_PASSWORD="secret"
    web:
        image: apache
        build: .
        container_name: apache_web
        restart: always
        ports:
            - "8080:80"

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.

$ docker-compose build             ## Build all services
$ docker-compose build web         ## Build single service

up โ€“ Create docker container and Services from docker-composer.yml under Current Directory. (-d) Switch is to run the container in daemon mode.

$ docker-compose up -d            ## Create all containers
$ docker-compose up -d web        ## Create single container

down โ€“ Option can be used to stop and delete Network, Container Service and Associate Images of containers.

$ docker-compose down           ## Restart all containers
$ docker-compose down web       ## Restart single container

ps โ€“ Used to know process details of services, status and ports of containers.

$ docker-compose ps

exec โ€“ Used to exec running containers. For example, to see the Container running the Web Service as a list-file..

$ docker-compose exec web ls -l

start - Used to start containers.

$ docker-compose start            ## Start all containers
$ docker-compose start web        ## Start single container

stop - Used to stop running containers.

$ docker-compose stop             ## Stop all containers
$ docker-compose stop web         ## Stop single container

restart โ€“ Used to restart containers.

$ docker-compose restart           ## Restart all containers
$ docker-compose restart web       ## Restart single container

pause โ€“ Used to pause containers.

$ docker-compose pause            ## Start all paused containers
$ docker-compose pause web        ## Start single paused container

rm โ€“ Used to delete and remove containers.

$ docker-compose rm               ## Start all paused containers
$ docker-compose pause web        ## Start single paused container

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.

$ mkdir dockercompose && cd dockercompose
$ mkdir webapp && echo "It Works"; webapp/index.html

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.

$ vim  webapp/Dockerfile

Then add the following code.

FROM tecadmin/ubuntu-ssh:16.04

RUN apt-get update \
   && apt-get install -y apache2

COPY index.html /var/www/html/
WORKDIR /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]
EXPOSE 80

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.

$ vim  docker-compose.yml

Then add the following code.

version: '3'
services:
  db:
     image: mysql
     container_name: mysql_db
     restart: always
     environment:
        - MYSQL_ROOT_PASSWORD="secret"
  web:
    image: apache
    build: ./webapp
    depends_on:
       - db
    container_name: apache_web
    restart: always
    ports:
      - "8080:80"

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.

$ docker-compose build
db uses an image, skipping
Building web
Step 1/6 : FROM tecadmin/ubuntu-ssh:16.04
16.04: Pulling from tecadmin/ubuntu-ssh
b3e1c725a85f: Pull complete
4daad8bdde31: Pull complete
63fe8c0068a8: Pull complete
4a70713c436f: Pull complete
bd842a2105a8: Pull complete
c41407f48fa7: Pull complete
1fcfeb9b5ef4: Pull complete
13195a7d2240: Pull complete
b86be64bbda8: Pull complete
8c951fe917dc: Pull complete
f74bc80103b6: Pull complete
Digest: sha256:523d6fbc97954e9f77231bf54bfcfbbdd4805349887477fbac4a63dc735d777d
Status: Downloaded newer image for tecadmin/ubuntu-ssh:16.04
 ---> bb63b492da01
Step 2/6 : RUN apt-get update    && apt-get install -y apache2
 ---> Running in 00be0dd717ce
[[[Removed long output from here]]]
 ---> 41c731590234
Removing intermediate container 00be0dd717ce
Step 3/6 : COPY index.html /var/www/html/
 ---> 42f84d4c2243
Removing intermediate container 945aaee6cbde
Step 4/6 : WORKDIR /var/www/html
 ---> 40bebd21e352
Removing intermediate container e13f5f412906
Step 5/6 : CMD apachectl -D FOREGROUND
 ---> Running in ab0db1ef1c6e
 ---> 587bf2323289
Removing intermediate container ab0db1ef1c6e
Step 6/6 : EXPOSE 80
 ---> Running in 7bcbef52d585
 ---> 8f03d4135394
Removing intermediate container 7bcbef52d585
Successfully built 8f03d4135394
Successfully tagged apache:latest

Step 5 โ€“ Launch Docker Containers

We will start the containers using docker-compose up. You can use the -d option to use Daemon mode.

$ docker-compose up -d

Step 6 โ€“ Update Content in Web Application

If you want to make changes to the web application.

$ echo "Welcome to Docker Compose Tutorial" >> webapp/index.html

Then use the following commands to rebuild the webapp container and start working.

$ docker-compose build
$ docker-compose up -d

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.

** Please Note: : " https://github.com/docker/machine/releases "

For Linux Systems:

$ curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine

$ chmod +x /usr/local/bin/docker-machine

You can download and install Docker Machine using the following command.

For OSX Systems:

$ curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine

$ chmod +x /usr/local/bin/docker-machine

You can download and install using commands like.

For Windows Systmes with Git Bash:

We recommend using it only on Windows 10 and above.

$ if [[ ! -d "$HOME/bin" ]]; then mkdir -p "$HOME/bin"; fi

$ curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe"

$ chmod +x "$HOME/bin/docker-machine.exe"

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:

$ docker [object] prune [options]

Prune all unused Objects.

The command below will remove containers, images, volumes and networks that are no longer used by docker.

$ docker system prune

--โ€Œall option It means everything related to unused docker.

$ docker system prune --all

--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.

$ docker system prune --filter "until=24h"

Prune Images

The command below can be used if you want to delete unused images only.

$ docker image prune

Prune containers

If you want to select and delete only the Stop/Exited Containers, you can use the command below.

$ docker container prune

Prune Volume

When you want to delete volumes that are no longer in use, you can use the following.

$ docker volume prune

Prune Network

Just like the command above, you can also use prune when you want to remove networks.

$ docker network prune

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.

$ docker system prune -f
$ docker image prune -f
$ docker container prune -f
$ docker network prune -f
$ docker volume prune -f

Depending on the situation, you can use the options as you like.

$ docker system prune -a -f
$ docker image prune -a -f
$ docker container prune -a -f
$ docker network prune -a -f
$ docker volume prune -a -f

Reference from tecadmin.

Last updated