Docker commands

  Annice      2021-02-15      Docker, Commands

Docker is a powerful tool to virtualize an operating system and different techniques by separated, independent services and containers, which in turn are based on one or many Docker images.

In other words, a Docker image can be created with e.g. Ubuntu as a virtual OS base, which in turn can install Apache web server, PHP, and MySQL to run a web application based on these techniques. Moreover, this image can execute these techniques by starting different containers: one running the Apache server with PHP, another one running MySQL etc.

Creating apps based on micro services like this enables an application to run on any machine with Docker installed. A container based infrastructure is also more resource efficient as you can stop, change and start new containers and services without taking into account the host computer/server's OS, in which they are completely isolated from.

Anyhow, in this entry I just wanted to list some Docker commands I find useful when managing images and containers via a system shell:

Command: Description:
docker build . Build a Docker image in the same folder where your docker file (Dockerfile) is.
docker build -t <image-name>:<label> path/to/Dockerfile Build a Docker image with a given image name and label (version) based on a Dockerfile.
docker-compose up Given that you are in the same folder as the Dockerfile: Build, (re)create, and attach to containers for a service.
docker-compose start Given that you are in the same folder as the Dockerfile: Start all containers that have been created by an image.
docker-compose stop Given that you are in the same folder as the Dockerfile: Stop all running containers of an image.
docker images List all Docker images.
docker run <image_id>

Create and start a container based on a given image. The number of executions of this command will create and start the same number of containers based on the same image.

docker ps

List all running containers.

docker ps -a

List all containers - including the exited ones.

docker start <container_id>

Start a container.

docker stop <container_id>

Stop a container.

docker rm <container-id>

Remove a container (given that the container has been stopped).

docker rmi <image_id>

Remove an image (given that all attached containers have been stopped).

docker exec -i -t <container_id> /bin/bash

SSH (connect to/enter) a container in Docker.