Byte Ebi's Logo

Byte Ebi 🍀

A Bit everyday A Byte every week

Common Docker Commands

Introduction to basic Docker operations and commonly used commands

Ray

Some commonly used commands and related parameters on Docker and their usage.
In actual use, there are definitely more commands than those listed here.

Basic Service Commands

docker stats             Display container resource usage statistics
docker start             Start one or more stopped containers
docker stop              Stop one or more running containers
docker restart           Restart one or more containers

Log Commands

$ docker logs [OPTIONS] CONTAINER
  Options:
        --details        Show extra details
    -f, --follow         Follow log output in real-time
        --since string   Show logs since a specific timestamp or relative time, e.g., 42m (42 minutes)
        --tail string    Number of lines to show from the end of the logs, default is all
    -t, --timestamps     Show timestamps
        --until string   Show logs until a specific timestamp or relative time, e.g., 42m (42 minutes)

Usage Examples

View logs after a specific timestamp

$ docker logs -t --since="2018-02-08T13:23:37" {CONTAINER_ID}

Display logs after a specific timestamp and only the last 100 lines

$ docker logs -f -t --since="2018-02-08" --tail=100 {CONTAINER_ID}

View logs for the last 30 minutes

$ docker logs --since 30m {CONTAINER_ID}

View logs within a specific time range

$ docker logs -t --since="2018-02-08T13:23:37" --until "2018-02-09T12:23:37" CONTAINER_ID

Running Containers

Create a container from a local environment image. If the image does not exist locally, it will be pulled from the repository.
Private repos require a “login” action. The image format can be [IMAGE NAME]:[TAG].

docker run

Examples:

# Run nginx
docker run -idt nginx

# Run nginx and expose port 8080
docker run -idt -p 8080:80 nginx

# Run nginx on port 8080, automatically restart on failure
docker run -idt -p 8080:80 --restart on-failure nginx

# Run nginx and remove after it exits
docker run --rm -idt nginx

Commonly Used Flags

`-d` detach: Run container in the background
`-i` interactive: Keep STDIN open even if not attached
`-t` allocate a pseudo-TTY
`-p` expose port (e.g., -p 8080:80) `-p Host Port:Docker Port`
`--restart` restart policy (always, none, on-failure)
`--rm` automatically remove the container when it exits

Container-related Commands

List all running containers

docker ps

List all containers (running and stopped)

docker ps -a

Execute a command in a running container

docker exec -ti nginx bash

Get metadata of a container or image

docker inspect {container name}

Remove a Docker container

docker rm [CONTAINER]

Image-related Commands

List all images

docker images

Remove a specified image

docker rmi [IMAGE]

If you want to expose the service to others

docker run -i -t -d -p 80:80 nginx

If the service encounters unexpected interruption

docker run -i -t -d -p 80:80 --restart always nginx

If you want to link with node.js

docker run -i -d -t -p 80:80 --link node nginx

Volume-related Commands

List all volumes

docker volume ls

Remove a specified volume

docker volume rm [OPTIONS] VOLUME [VOLUME...]

Remove “all” volumes

docker volume prune

Other Commands

docker build

Build a Docker image from a Dockerfile -f manually specify the Dockerfile name

# Build an image in the current directory using Dockerfile
docker build . -t image-name

# Build an image in the current directory using Dockerfile-alt
docker build . -t image-name -f Dockerfile-alt

docker push / pull

Push or pull a Docker image to/from a repository Login to Docker repository first using docker login.

docker login

Login to a Docker repository to push or pull images.

Recent Posts

Categories

Tags