Friday, 1 May 2020

Docker: basic commands to manage images and containers



Docker is one of most famous container platform.
Formazione Docker & DevOps
Its core elements are the following:
- image: read-only template containing a runtime environment
(application libraries and application itself)
- container: an instance of an image, for running application isolated from others even if sharing the same host OS
- registry: repository where you can store images; it can be private or public. Docker Hub is the public registry used by the community.

How isolation between containers is achieved?
Through some features of Linux Kernel:
- cgroups: limit resources consumption by the processes belonging to specific containers
- namespace: each container belongs to a namespace, and can see the resources placed in that namespace. Resources are: network interfaces, IPC resources, process id list ecc..
- SELinux: protects the host from the containers running on it

Let's review Docker basic commands.

Pull the latest image of Ubuntu from Docker Hub:

docker pull ubuntu

Pull the bionic image of Ubuntu from Docker Hub:

docker pull ubuntu:bionic

View the list of pulled Docker images:

docker images

Delete an image:

docker rmi <image_id>

Start new Docker container from ubuntu:bionic image, name it myubuntu, and run it in background:

docker run -d --name myubuntu ubuntu:bionic

Start new Docker container in interactive mode:

docker run -it --name myubuntu ubuntu:bionic /bin/bash

Stop Docker container:

docker stop myubuntu

Start an existent container

docker start myubuntu

Restart a container:

docker restart myubuntu

Delete container:

docker stop myubuntu && docker rm myubuntu

Expose a port on host from docker container:

docker run -it --name mynginx -p 3000:80 nginx

Access running container:

docker exec -it myubuntu /bin/bash

List details of running or stopped container:

docker inspect myubuntu

Exiting from a container:

exit

List all containers:

docker ps -a

Mount a host directory inside container:

docker run -d -v /host/dir:/container/dir ubuntu




No comments:

Post a Comment