Docker Commands in Practice

2 min read

General Usage

Start a container in background

docker run -d IMAGE_NAME

Start an interactive container

docker run -it IMAGE_NAME bash

View the contents of an image/container

docker run --rm -it --entrypoint=/bin/bash IMAGE_NAME
docker exec -it CONTAINER_ID bash

Export port from a container

docker run -p 80:80 -d IMAGE_NAME

Start a named container

docker run --name CONTAINER_ID IMAGE_NAME

Restart a stopped container

docker start CONTAINER_ID

Stop a container

docker stop CONTAINER_ID

Build Images

Build an image from Dockerfile in current directory

docker build --tag IMAGE_NAME .

Force rebuild of Docker image

docker build --no-cache .

Convert a container to image

docker commit CONTAINER_ID IMAGE_NAME

Chnage image name

docker tag CURRENT_IMAGE_NAME DESIRED_IMAGE_NAME

Remove all unused images

docker image prune --all

Manage Containers

List running containers

docker ps

List all containers (running & stopped)

docker ps -a

Inspect containers metadata

docker inspect CONTAINER_ID

List local available images

docker images

Delete all stopped containers

docker container prune

List all containers with a specific label

docker ps --filter label=LABEL

Copy files of a container to local directory

docker cp CONTAINER_ID:PATH_TO_BE_COPIED PATH_OF_LOCAL_HOST

Debug

Run another process in running container

docker exec -it CONTAINER_ID bash

Show live logs of running daemon container

docker logs -f CONTAINER_ID

Show exposed ports of a container

docker port CONTAINER_ID

Volumes

Create a local volume

docker volume create --name VOLUME_NAME

Mounting a volume on container start

docker run -v VOLUME_NAME:/data IMAGE_NAME

Destroy a volume

docker volume rm VOLUME_NAME

List volumes

docker volume ls

Networking

Create a local network

docker network create NETWORK_NAME

Attach a container to a network on start

docker network create NETWORK_NAME IMAGE_NAME

Connect a running container from a network

docker network connect NETWORK_NAME CONTAINER_ID

Disconnect container to a network

docker network disconnect NETWORK_NAME CONTAINER_ID
 Docker    MLOps