J O S E P H J O S E P H

Docker Containers

Containers

Create and run a container from an image

docker run <image_name>

# Options
-d, --detach    # Run the container in detached mode
-it             # Interactive + TTY - Open a shell in the new container
--name          # Assign a name to the container
-p, --publish   # Expose container ports to host ports
--rm            # Automatically remove the container on exit
-v, --volume    # Mount (create) a named volume for data persistence beyond container lifecycle

List containers

docker ps # List running containers

# Options
-a, --all       # List all containers (running & stopped)
-l, --latest    # List latest created container
-q, --quiet     # Only display container IDs
-s, --size      # Display total file sizes

Start an existing container

docker start <container_name_or_id>

# Options
-a, --attach    # Attach STDOUT and forward signals

Stop a running container

docker stop <container_name_or_id>

Accessing containers

docker attach <container_name>       # Attach to a detached container so it runs in foreground
docker logs <container_name>         # Fetch and trail the logs of a container
docker exec -it <container_name> sh  # Open a shell (sh/bash/etc.) inside a running container

Copying files from host machine to container

docker cp local/test.txt <container_name>:/app

Removing containers

docker rm <container_name>  # Remove a single (stopped) container
docker container prune      # Remove all stopped containers