Introduction

Docker is a powerful tool for building, shipping, and running applications within containers. Here are some essential Docker CLI commands to help you navigate Docker efficiently.

These topics will be add in the future:

  • Docker: What is it?
  • Docker Evolution: A Brief History
  • Virtualization vs. Containerization
  • Understanding Containers and Images
  • Docker File Hierarchy
  • Docker Volume
  • Docker Swarm
  • Docker Mount and Bind Mount
  • Docker Plugin and Network Driver
  • STDIN, STDOUT, STDERR
  • Why should I use Docker?

Getting Started

Check Docker Version

docker version

Docker Information

docker info

View Logs

docker logs

Docker Management Commands Cleanup

docker [Management CMD] prune

Creating and Running Containers

Searching for a Specific Image

docker search [image_name]

Run an Interactive Container

docker run -it [image_name]
  • ("i" means run interactively, and "t" tells Docker to run a shell within the container.)

Assign a Custom Name to a Container

docker run --name [custom_name] [image_name]

Execute a Command in a Running Container

docker exec -it [container_ID] [command]

Start/Stop/Restart Container

docker start -ai [container_ID]
docker stop [container_ID]
docker restart [container_ID]

Download Specific Docker Version

docker run [redis:5]

Automatically Remove Container After Execution

docker run --rm [image_name]

Run Container in Detached Mode

docker run -d [image_name]

Copy Docker

docker image tag [existing_image_tag] [new_image_name]

List Images

docker images

Managing Images and Containers

Delete Untagged Images

docker rmi $(docker images -f "dangling=true" -q)

Delete All Containers

docker rm -v -f $(docker ps -qa)

List All Containers

docker container ls -a

List Historical Containers

docker ps
docker ps --all
  • The container's ID
  • What command is the container running
  • When was the container created
  • How long has the container been running
  • What ports are mapped
  • The name of the container

Create a Volume

docker volume create [NAME]

Inspect Volume Details

docker volume inspect [NAME]

Use Volume

docker container run -it -v [NAME]:/[DIR] [IMAGE] [CMD]

List Volumes

docker volume ls

Rename Container

docker container rename [container_name/ID] [new_name]

General Container Information

docker inspect [container_ID]

View Container Memory/CPU Usage

docker stats

Limit Memory/CPU Usage

docker container run --memory=[MB]m/g --memory-swap=[MB]m/g --cpus=[usage_cpu_core_num] --cpuset-cpus=[which_cpu_core_nums] [image_name]

Set Environment Variables

docker container run --env VAR1=[Name] VAR2=[Name] [image_name]
docker container run --env-file [env.list] [image_name]

Show Running Processes

docker top [container_ID]

Copy Files to/from Container

docker cp [file_name] [container_ID]:/[destination_path]
docker cp [container]:[src-path] [local-dest-path]

Connect to Application

docker run -p [external_port:internal_port] [application_name]
docker run -v /opt/data:{data/db} -p [external_port:internal_port] [application_name]

Link Containers

docker run --name mysql-server -p 3306:3306 -e MYSQL_ROOT_PASSWORD=test123456 mysql
docker run --name mysql-server -p 3306:3306 -v /opt/data:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=test123 mysql
and
docker run --name padmin -p 8000:80 --link mysql-server:db phpmyadmin/phpmyadmin

Networking

Create a Network

docker network create --driver [bridge/host/none] --subnet [10.10.0.0/24] --ip-range [10.10.10.0/24] --gateway [10.10.11.11] [app_name]

List Networks

docker network ls

Example: Connect Containers with a Custom Network

docker network create ecommerce
docker run -p 80:80 --name webserver --net ecommerce webserver
docker run --name database --net ecommerce webserver

Dockerfile Instructions

Docker Compose Up
Docker Run Name Natabase
FROM [container_to_download:version]
RUN [execute commands]
COPY . /opt/app-name
WORKDIR /opt/app-name
ENV key=value
EXPOSE [port]
ENTRYPOINT ["executable"]
CMD ["node", "file_name"]
CMD /bin/sh -c script.sh
# [CMD, determines what command is run when the container starts (you would use this to start a service or application)]

Build Dockerfile

docker build . -t [app_name]
  • (the dot, tells Docker to look in our working directory)

Docker Compose Instructions

Docker Compose YAML Explanation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
version: '3.3'
services:
  web:
    build: ./web
    networks:
      - ecommerce
    ports:
      - '80:80'

  database:
    image: mysql:latest
    networks:
      - ecommerce
    environment:
      - MYSQL_DATABASE=ecommerce
      - MYSQL_USERNAME=root
      - MYSQL_ROOT_PASSWORD=helloworld

networks:
  ecommerce:
InstructionExplanationExample
versionThis is placed at the top of the file and is used to identify what version of Compose the docker-compose.yml is written for.'3.3'
servicesThis instruction marks the beginning of the containers to be managed.services:
name (replace value)This instruction is where you define the container and its configuration. "name" needs to be replaced with the actual name of the container you want to define, i.e. "webserver" or "database".webserver
buildThis instruction defines the directory containing the Dockerfile for this container/service. (you will need to use this or an image)../webserver
portsThis instruction publishes ports to the exposed ports (this depends on the image/Dockerfile).'80:80'
volumesThis instruction lists the directories that should be mounted into the container from the host operating system.'./home/cmnatic/webserver/:/var/www/html'
environmentThis instruction is used to pass environment variables (not secure), i.e. passwords, usernames, timezone configurations, etc.MYSQL_ROOT_PASSWORD=helloworld
imageThis instruction defines what image the container should be built with (you will need to use this or build).mysql:latest
networksThis instruction defines what networks the containers will be a part of. Containers can be part of multiple networks (i.e. a web server can only contact one database, but the database can contact multiple web servers).ecommerce
Docker Compose

Run Docker Compose File

docker-compose up -d
  • (in the directory where the file is located)

Start Containers

docker-compose start

Stop and Delete Containers

docker-compose down

Stop Containers (without deletion)

docker-compose stop

Containerization with Docker Engine

Docker Run Name Natabase

Docker Engine provides a robust platform for containerization, offering various functionalities such as:

Connecting Containers: Docker allows you to connect multiple containers together, enabling scenarios like running a web application in one container and a database in another. This facilitates microservices architecture and decoupling of application components.

Exporting and Importing Applications (Images): With Docker, you can easily export and import containerized applications as images. This feature streamlines the deployment process, allowing developers to package their applications along with dependencies into portable images that can be deployed across different environments seamlessly.

Transferring Files Between Operating System and Container: Docker simplifies the process of transferring files between the host operating system and containers. Using commands like docker cp, you can copy files and directories between the host and container filesystems. This capability is particularly useful for tasks such as debugging, configuration management, and transferring data into or out of containers.

Docker Engine's versatility and efficiency make it a popular choice for containerization, enabling developers to build, deploy, and manage applications more efficiently across various environments. Whether you're developing microservices-based applications or deploying complex distributed systems, Docker provides the tools you need to streamline the containerization process and enhance productivity.

Conclusion

Docker provides a comprehensive set of CLI commands to manage containers, images, volumes, and networks effectively. With these commands, you can streamline your development and deployment processes, making Docker an essential tool for modern software development.

Resources: