Services Admin

Displaying Docker Images

To see the list of Docker images on the system, you can issue the following command.

docker images

This command is used to display all the images currently installed on the system.

Output:

  • TAG − This is used to logically tag images.

  • Image ID − This is used to uniquely identify the image.

  • Created − The number of days since the image was created.

  • Virtual Size − The size of the image.

Removing Docker Images

The Docker images on the system can be removed via the docker rmi command.

docker rmi

This command is used to remove Docker images.
Syntax

docker rmi ImageID

Containers

Containers are instances of Docker images that can be run using the Docker run command. The basic purpose of Docker is to run containers.

Running a Container

Running of containers is managed with the Docker run command. To run a container in an interactive mode, first launch the Docker container.

docker run –it myimage /bin/bash

Listing of Containers

One can list all of the containers on the machine via the docker ps command. This command is used to return the currently running containers.

docker ps

Display the running processes of a container

With this command, you can see the top processes within a container. Syntax

docker top ContainerID

Options

    ContainerID − This is the Container ID for which you want to see the top processes.

Stop a running container

This command is used to stop a running container.

docker stop ContainerID

Options

    ContainerID − This is the Container ID which needs to be stopped.

Attach a running container

This command is used to attach to a running container.

docker attach ContainerID

Options

    ContainerID − This is the Container ID to which you need to attach.

Delete container

This command is used to delete a container.

docker rm ContainerID

Options

    ContainerID − This is the Container ID which needs to be removed.

Container Logging

Logging is also available at the container level.

Docker logs containerID

Parameters

    containerID − This is the ID of the container for which you need to see the logs.

Volumes

create

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

docker run -d --name mycontainer -v /var/www/html:/var/html nginx:latest

list

docker volume ls

Network

Creates a new network. The DRIVER accepts bridge or overlay which are the built-in network drivers.

create bridge network

docker network create -d bridge my-network

create overlay network

docker network create -d overlay my-network

create with advanced options

docker network create \
  --driver=bridge \
  --subnet=172.28.0.0/16 \
  --ip-range=172.28.5.0/24 \
  --gateway=172.28.5.254 \
  mybr

list

docker network ls

Inspect

Low-level information on Docker objects

images

docker ps IMAGE ID

container

docker ps CONTAINER ID

network

docker ps NETWORK ID

volume

docker ps VOLUME ID

prune - remove obj

Remove all unused objects.

prune will delete ALL dangling data i.e. containers stopped, volumes without containers and images with no containers

images

docker image prune
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

container

docker container prune

network

docker network prune

volume

docker volume prune

repositories

You might have the need to have your own private repositories. You may not want to host the repositories on Docker Hub. For this, there is a repository container itself from Docker. Let’s see how we can download and use the container for registry.

Create

docker run –d –p 5000:5000 –-name registry registry:2

The following points need to be noted about the above command:

    Registry is the container managed by Docker which can be used to host private repositories.

    The port number exposed by the container is 5000. Hence with the –p command, we are mapping the same port number to the 5000 port number on our localhost.

    We are just tagging the registry container as “2”, to differentiate it on the Docker host.

    The –d option is used to run the container in detached mode. This is so that the container can run in the background

Docker Hub

Docker Hub is a registry service on the cloud that allows you to download Docker images that are built by other communities. You can also upload your own Docker built images to Docker hub.

To run apache, you need to run the following command:

docker run -p 8080:80 apache

Note the following points about the above  command −


    Here, apache is the name of the image we want to download from Docker hub and install on our Ubuntu machine.

    -p is used to map the port number of the internal Docker image to our main Ubuntu server so that we can access the container accordingly.

Push

use the Docker push command to push the image to our private repository.

docker push localhost:5000/myimage

Pull

use the following Docker pull command to pull image from our private repository.

docker pull localhost:5000/myimage