Docker Containers

Source: docker container official document

Docker Containers

docker run

The docker run command starts a new container and runs a command within it, pulling the image if needed and starting the container.

1
docker container run [OPTIONS] <IMAGE> [COMMAND] [ARG...]

Key Options:

  • -i, --interactive: Keep STDIN open even if not attached
  • -t, --tty: Allocate a pseudo-TTY
  • -d, --detach: Run container in the background
  • --name NAME: Specify a custom identifier for a container
  • --rm: Automatically remove the container when it exits
  • -p HOST:CONTAINER: Create a port mapping between the host and the container

Network Options:

  • --network bridge: Default bridge network
  • --network host: Use host's network stack
  • --network none: Disable networking

Restart Policies:

  • --restart no: Don't restart (default)
  • --restart always: Always restart
  • --restart unless-stopped: Restart unless manually stopped

Common Patterns

Run the image with a bash process as the main process in the background:

1
docker run --rm -d -i --name <CONTAINER> <IMAGE> bash

where -i keeps STDIN open. This keeps bash open since it is waiting for input.

Run the image with a bash process as the main process interatively:

1
docker run --rm -it --name <CONTAINER> <IMAGE> bash

docker exec

The docker exec command runs a new command in a running container. The command you specify with docker exec only runs while the container's primary process (PID 1) is running.

1
docker exec [OPTIONS] <CONTAINER> <COMMAND> [ARG...]

Key Options:

  • -i, --interactive: Keep STDIN open
  • -t, --tty: Allocate pseudo-TTY
  • -d, --detach: Run in background
  • --workdir DIR: Set working directory
  • --user USER: Run as specified user

Access running container:

1
docker exec -it <CONTAINER> bash

Container Management

List containers:

1
2
3
docker container ls         # Running containers
docker container ls -a # All containers (including stopped)
docker container ls -q # Only show container IDs

Control containers:

1
2
3
4
5
docker container start <CONTAINER>     # Start stopped container
docker container stop <CONTAINER> # Stop running container (SIGTERM then SIGKILL)
docker container restart <CONTAINER> # Restart container
docker container pause <CONTAINER> # Pause container
docker container unpause <CONTAINER> # Unpause container

Remove containers:

1
2
3
4
docker container rm <CONTAINER>              # Remove stopped container
docker container rm -f <CONTAINER> # Force remove (stops then removes)
docker container rm $(docker ps -a -q) # Remove all containers
docker container prune # Remove all stopped containers

Container information:

1
2
3
4
5
docker container inspect <CONTAINER>                    # Full container details
docker container port <CONTAINER> # Port mappings
docker container logs -f <CONTAINER> # Follow container logs
docker container export -o backup.tar <CONTAINER> # Export filesystem as tar
docker import backup.tar <IMAGE> # Import tar as image

Resource inspection:

1
2
3
4
docker network ls                                        # List networks
docker volume ls # List volumes
docker inspect <CONTAINER> | grep Mounts -A 20 # Check mount points
docker inspect <CONTAINER> | grep RestartPolicy -A 20 # Check restart policy

Detach from container: Ctrl+P, Ctrl+Q (leaves container running)

The main process (PID 1) controls container lifecycle. The stop command sends SIGTERM and waits 10s before sending SIGKILL. Commands run via exec are additional processes that don't affect the main container process.