LinuxCommandLibrary

docker

TLDR

Run container

$ docker run [image]
copy
List containers
$ docker ps
copy
Build image
$ docker build -t [name] [.]
copy
Pull image
$ docker pull [image]
copy
Stop container
$ docker stop [container]
copy
Remove container
$ docker rm [container]
copy
Execute command in container
$ docker exec -it [container] [bash]
copy

SYNOPSIS

docker command [options]

DESCRIPTION

docker is a platform for developing, shipping, and running applications in containers. It packages applications with dependencies into standardized units, ensuring consistency across different environments.
The tool has revolutionized application deployment and development workflows.

PARAMETERS

run [options] image

Create and start container
ps [-a]
List containers
build -t name path
Build image from Dockerfile
pull image
Pull image from registry
push image
Push image to registry
exec container command
Execute command in running container
logs container
Fetch container logs
stop container
Stop running container
start container
Start stopped container
rm container
Remove container
rmi image
Remove image
images
List images

RUN OPTIONS

-d, --detach

Run in background
-it
Interactive with TTY
-p host:container
Publish port
-v host:container
Mount volume
--name name
Assign name to container
-e var=value
Set environment variable
--rm
Automatically remove on exit
--network network
Connect to network

WORKFLOW

$ # Run container
docker run -d -p 80:80 --name webserver nginx

# Interactive container
docker run -it ubuntu bash

# With environment variables
docker run -e DATABASE_URL=postgres://... myapp

# With volume mount
docker run -v $(pwd):/app -w /app node npm install

# Build image
docker build -t myapp:latest .

# Push to registry
docker tag myapp:latest user/myapp:latest
docker push user/myapp:latest

# View logs
docker logs -f container_name

# Execute command
docker exec -it container_name bash

# Clean up
docker stop container_name
docker rm container_name
docker rmi image_name
copy

DOCKERFILE

$ FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
copy

COMMON OPERATIONS

$ # List running containers
docker ps

# List all containers
docker ps -a

# List images
docker images

# Remove all stopped containers
docker container prune

# Remove unused images
docker image prune

# System-wide cleanup
docker system prune -a

# Inspect container
docker inspect container_name

# Container stats
docker stats
copy

CAVEATS

Requires Docker daemon running. Root or docker group membership needed. Networking can be complex. Volume permissions issues common. Images can be large. Security considerations for production. Different behavior on different operating systems.

HISTORY

Docker was created by Solomon Hykes at dotCloud in 2013, becoming open-source and revolutionizing application containerization.

SEE ALSO

Copied to clipboard