# Docker Notes ## Using Docker CLI ```sh // obj --> container, image, network or volume docker // To run the hello-world image docker run hello-world ``` ## Publishing Ports ```sh --publish : ``` ## Keep Running in Background ```sh docker run --detached --publish 8080:80 hello-world ``` ## Useful Commands ```sh // List running containers docker container ls // List running and stopped containers docker container ls --all // Run with custom name docker container run --name hello-world-c hello-world // Rename container (stopped or running) docker container rename // Gracefully stop (Sends SIGTERM then SIGKILL if not responding in given time) docker container stop // Start a stopped/killed container // In detached mode by default and retains all previous port configs docker container start // Restart a running container docker container restart // Create container without running it docker container create --publish 8080:80 // Remove dangling container docker container rm // Remove all dangling containers docker container prune ``` ## Container Names Every container can be identified by: - NAME --> two words separeted by an underscore - CONTAINER ID --> 64 random char string ## Creating and Running Containers The `docker container run` command is in reality: - `docker container create` --> create a container from given image - `docker container start` --> start an already created container TIP: Running the `start` or `run` command with the `--rm` flag removes the container once it is stopped. ## Running Interactive Containers Images like `ubuntu` are configured to run a shell by default. Running them with the `--it` flag lets us run in interactive mode: ```sh docker container --rm --it run ubuntu ``` In reality it is: --interactive --> connect the input stream to the cotnainer so that we can send input to the container shell. --tty --> provides a native terminal-like terminal with formatting by creating a pseudo-tty. TIP: The `busybox` image is a swiss army knife of unix utilities. ## Running Command Inside Containers When executing the `run` command, anything after the image name gets passed to the default image entry-point which is usually the terminal (sh) for non-executable images. ```sh // To encode a string in base64 using busybox docker container --rn run busybox echo -n | base64 ``` ## Bind Mounts `--volume or -v` We can use the `-v` option for both `run` and `create` commands. Let's say we wanted to run an executable that removes files with a given extension from the current working directory. To do so we need to give the container access to the local file system. One way of doing so is with bind mounts. NOTE: The local source direcoty and the container destination directory have to be specified with absolute paths. ```sh // --volume :: docker container run --rm -v $(pwd):/zone fhsinchy/rmbyext pdf ```