Container management

docker container management

Container creation

  • Create a docker container using CentOS image
    • -it flag means executing container with attaching their terminal (TTY) and standard input (STDIN)
    • --name flag makes us set a human readable name of the container
      • You can easily manage the container if it is named.
      • If you don’t specify a name of the container, it uses a commit tag as the name and you can use 3 or 4 leading alphanumeric of the name hash to manage it.
docker run --name centos -it centos
  • Detach the attached TTY
    • You hit Ctrl-p Ctrl-q to exit the attached terminal without shutting down the container.
    • If you type and run exit in your attached terminal, it makes the container turned off. In order to turn on it, use docker start centos.
      • Refer to the next section to connect the restarted container.

Connect to your container

Check container’s status

  • Use docker ps command to list running containers.
  • For listing all containers event if not running, run docker ps -a.
  • You can retrieve the container named by centos by docker ps -a -f name=centos.
    • For more details on filter, refer to filter section in the official site.
    • Instead of the filter, you can use grep as docker ps -a | grep centos.
  • You can get the minimum information by docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}".

Connect into the container

  • exec sub-command helps us to connect into the container.
    • You must add -it flags to connect it, the following command shows execution of bash command to connect to the container.
docker exec -it centos bash
  • You can use attach sub-command to attach your container.
    • Note that attach sub-command makes you directly attach to running environment. It means that if you run exit or invoke SIGTERM, it terminates your container. Therefore, you use Ctrl-p Ctrl-q shortcut to detach the container.

Docker hub

What is docker hub?

  • Docker image portal
    • In order to search docker images, you can use search sub-command.
      • Also, you can containerize the searched images.
docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 11806 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface 497 [OK]

Using the docker hub

  • Docker hub

    • Docker hub is a website for searching docker images.
      • In the results, you can find tags of an image in tags tab.

Using docker image and tags

  • Searching docker images on docker hub
    • In tags tab, find a tag you want to containerize.
    • In order to containerize an image, describe the image and tag formed by {image repository}:{tag}.
      • The below command show a way to run ubuntu 20.04.
docker run -it --name ubuntu ubuntu:20.04