Container Lifecycle

Create and Run Containers

Run a container interactively with a specific name.

$ docker run -it --name container_name image_name

Create a container without starting it.

$ docker create --name container_name image_name

Start a stopped container.

$ docker start container_name

Stop a running container.

$ docker stop container_name

Restart a container.

$ docker restart container_name

Pause a running container.

$ docker pause container_name

Unpause a paused container.

$ docker unpause container_name

Execute a command in a running container.

$ docker exec -it container_name command

Container Information

List running containers.

$ docker ps

List all containers, including stopped ones.

$ docker ps -a

Display detailed information about a container.

$ docker inspect container_name

Fetch the logs of a container.

$ docker logs container_name

Display live performance stats for a container.

$ docker stats container_name

Display the running processes of a container.

$ docker top container_name

Image Management

List all locally available images.

$ docker images

Download an image from a registry.

$ docker pull image_name

Remove a locally stored image.

$ docker rmi image_name

Build an image from a Dockerfile.

$ docker build -t image_name:tag .

Tag an image with a new name and/or tag.

$ docker tag image_id new_image_name:new_tag

Push an image to a registry.

$ docker push new_image_name:new_tag

Network and Volume Management

Network Commands

List all networks.

$ docker network ls

Display detailed information about a network.

$ docker network inspect network_name

Create a new network.

$ docker network create network_name

Connect a container to a network.

$ docker network connect network_name container_name

Disconnect a container from a network.

$ docker network disconnect network_name container_name

Volume Commands

List all volumes.

$ docker volume ls

Display detailed information about a volume.

$ docker volume inspect volume_name

Create a new volume.

$ docker volume create volume_name

Remove a volume.

$ docker volume rm volume_name

Run a container with a specified volume.

$ docker run -v volume_name:/container/path image_name

Docker Compose

Start services defined in a docker-compose.yml.

$ docker-compose up

Stop and remove containers, networks, and volumes defined in a docker-compose.yml.

$ docker-compose down

List containers defined in a docker-compose.yml.

$ docker-compose ps

Fetch the logs of services defined in a docker-compose.yml.

$ docker-compose logs

Container Interaction and Debugging

Attach and Detach

Attach to a running container's stdin, stdout, and stderr.

$ docker attach container_name

Detach from a container without stopping it.

Press Ctrl + P, Ctrl + Q

Interactive Shell

Open an interactive shell in a running container.

$ docker exec -it container_name /bin/bash

Container Inspect

Retrieve the IP address of a running container.

$ docker inspect --format='{{.NetworkSettings.IPAddress}}' container_name

Retrieve the IP address of a running container with multiple networks.

$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name

Resource Constraints

Run a container with specified CPU and memory constraints.

$ docker run -it --cpu-shares=512 --memory=512m image_name

Docker Swarm

Swarm Initialization

Initialize a Docker swarm on the current node.

$ docker swarm init

Initialize a swarm and specify the advertise address.

$ docker swarm init --advertise-addr <node_ip>

Generate the command to join a worker node to the swarm.

$ docker swarm join-token worker

Generate the command to join a manager node to the swarm.

$ docker swarm join-token manager

Service Management

Create a service with N replicas.

$ docker service create --replicas N --name service_name image_name

List all services in the swarm.

$ docker service ls

List tasks of a service.

$ docker service ps service_name

Scale a service to N replicas.

$ docker service update --replicas N service_name

Remove a service.

$ docker service rm service_name

Node Management

List all nodes in the swarm.

$ docker node ls

Display detailed information about a node.

$ docker node inspect node_id

Add a label to a node.

$ docker node update --label-add key=value node_id

Remove a node from the swarm.

$ docker node rm node_id

Docker Registry

Image Push and Pull

Push an image to a Docker registry.

$ docker push repository_name/image_name:tag

Pull an image from a Docker registry.

$ docker pull repository_name/image_name:tag

Login and Logout

Log in to a Docker registry.

$ docker login registry_url

Log out from a Docker registry.

$ docker logout registry_url

List and Remove Images

Search for an image on Docker Hub.

$ docker search search_term

List all images on the local machine.

$ docker image ls

Remove a specific image from the local machine.

$ docker image rm image_name

Remove all unused images from the local machine.

$ docker image prune

Docker Compose - Advanced

Scale and Update Services

Scale a service to N replicas in detached mode.

$ docker-compose up -d --scale service_name=N

Scale a service to N replicas.

$ docker-compose scale service_name=N

Rebuild images before starting services.

$ docker-compose up --build

Force recreation of containers.

$ docker-compose up -d --force-recreate

Environment Variables

Use environment variables from a file.

$ docker-compose --env-file .env up

Set environment variables inline.

$ docker-compose up -e KEY=VALUE

Networking

Specify a different Compose file for networking.

$ docker-compose -f docker-compose-network.yml up

Specify a custom project name.

$ docker-compose -p project_name up

Volumes and Bind Mounts

Specify a different Compose file for volumes.

$ docker-compose -f docker-compose-volumes.yml up

Specify a named volume.

$ docker-compose -v volume_name:/container/path up

Specify a bind mount.

$ docker-compose -v ./host/path:/container/path up

Healthchecks

Define a health check command for a service.

$ docker-compose --health-cmd 'command' up

Set the health check interval.

$ docker-compose --health-interval 5s up

Docker Compose - Services Configuration

Service Configuration

View the logs of a specific service.

$ docker-compose logs service_name

Execute a command in a running service container.

$ docker-compose exec service_name command

Pause a service.

$ docker-compose pause service_name

Unpause a paused service.

$ docker-compose unpause service_name

Display the running processes of services.

$ docker-compose top

Restart a service.

$ docker-compose restart service_name

Stop a service.

$ docker-compose stop service_name

Start a service.

$ docker-compose start service_name

Compose Down and Cleanup

Stop and remove containers, networks, and volumes.

$ docker-compose down

Remove volumes along with containers.

$ docker-compose down -v

Remove images along with containers.

$ docker-compose down --rmi all

Remove containers for services not defined in the Compose file.

$ docker-compose down --remove-orphans

Environment Overrides

Use environment variable overrides from a file.

$ docker-compose --env-file .env.override up

Use Compose file overrides for local development.

$ docker-compose -f docker-compose.override.yml up