Commands

Cards (28)

  • The syntax for Docker can be categorized into four main groups:
    • Running a container
    • Managing & Inspecting containers
    • Managing Docker images
    • Docker daemon stats and information
  • Docker Pull
    • Before we can run a Docker container, we will first need an image. Recall from the “Intro to Containerisation” room that images are instructions for what a container should execute. There’s no use running a container that does nothing!
    • Images can be downloaded using the docker pull command and providing the name of the image. For example, docker pull nginx. Docker must know where to get this image
  • "docker pull nginx"
    • By running this command, we are downloading the latest version of the image titled “nginx”. Images have these labels called tags. These tags are used to refer to variations of an image. For example, an image can have the same name but different tags to indicate a different version.
    • "docker pull ubuntu" is the same as "docker pull ubuntu:latest"
    • This command will pull the latest version of the "ubuntu" image. If no tag is specified, Docker will assume you want the "latest" version if no tag is specified.
    • It is worth remembering that you do not always want the "latest". This image is quite literally the "latest" in the sense it will have the most recent changes. This could either fix or break your container.
    • You may want the version before latest eg: docker pull ubuntu:22.04
  • Docker Image x/y/z
    • The docker image command, with the appropriate option, allows us to manage the images on our local system. To list the available options, we can simply do docker image to see what we can do. I’ve done this for you in the terminal below:
  • "Docker Image ls"
    • This command allows us to list all images stored on the local system. We can use this command to verify if an image has been downloaded correctly and to view a little bit more information about it (such as the tag, when the image was created and the size of the image).
  • "Docker Image rm"
    • If we want to remove an image from the system, we can use docker image rm along with the name (or Image ID). In the following example, I will remove the "ubuntu" image with the tag "22.04". My command will be docker image rm ubuntu:22.04:It is important to remember to include the tag with the image name.
    • The Docker run command creates running containers from images. This is where commands from the Dockerfile (as well as our own input at runtime) are run. Because of this, it must be some of the first syntaxes you learn.
    • The command works in the following way: docker run [OPTIONS] IMAGE_NAME [COMMAND] [ARGUMENTS...]  the options enclosed in brackets are not required for a container to run.
    • Docker containers can be run with various options - depending on how we will use the container.
  • Run a Docker container: docker run [OPTIONS] IMAGE_NAME [COMMAND] [ARGUMENTS...] . In this example, I am going to configure the container to run:
    • An image named "helloworld"
    • "Interactively" by providing the -it switch in the [OPTIONS] command. This will allow us to interact with the container directly.
    • I am going to spawn a shell within the container by providing /bin/bash as the [COMMAND] part. This argument is where you will place what commands you want to run within the container (such as a file, application or shell!
  • We can verify that we have successfully launched a shell because our prompt will change to another user account and hostname. The hostname of a container is the container ID (which can be found by using docker ps). For example, in the terminal above, our username and hostname are root@30eff5ed7492
  • -d
    • This argument tells the container to start in "detached" mode. This means that the container will run in the background.
    • Example: docker run -d helloworld
  • -it
    • This argument has two parts. The "i" means run interactively, and "t" tells Docker to run a shell within the container. We would use this option if we wish to interact with the container directly once it runs.
    • Example: docker run -it helloworld
  • -v
    • This argument is short for "Volume" and tells Docker to mount a directory or file from the host operating system to a location within the container. The location these files get stored is defined in the Dockerfile
    • Example: docker run -v /host/os/directory:/container/directory helloworld
  • -p
    • This argument tells Docker to bind a port on the host operating system to a port that is being exposed in the container. You would use this instruction if you are running an application or service (such as a web server) in the container and wish to access the application/service by navigating to the IP address.
    • docker run -p 80:80 webserver
  • --rm
    • This argument tells Docker to remove the container once the container finishes running whatever it has been instructed to do.
    • Example: docker run --rm helloworld
  • --name
    • This argument lets us give a friendly, memorable name to the container. When a container is run without this option, the name is two random words. We can use this open to name a container after the application the container is running.
    • Example: docker run --name helloworld
  • Listing Running Containers
    • To list running containers, we can use the "docker ps" command. This command will list containers that are currently running
    • This command will also show information about the container, including:
    • The container's ID
    • What command is the container running
    • When was the container created
    • How long has the container been running
    • What ports are mapped
    • The name of the container
    Tip: To list all containers (even stopped), you can use docker ps -a:
  • To get the container's IP address, run the 2 commands:
    • docker ps
    • docker inspect container_name | grep IPAddress
  • Build an Image:
    • docker build -t <image_name>:<tag> <path_to_Dockerfile>
    Run a Container:
    • docker run -d --name <container_name> <image_name>:<tag>
    List Running Containers:
    • docker ps
    List All Containers (including stopped):
    • docker ps -a
    Stop a Container:
    • docker stop <container_name or container_id>
    Remove a Container:
    • docker rm <container_name or container_id>
    Remove an Image:
    • docker rmi <image_name>:<tag>
  • List Docker Images:
    • docker images
    Pull Docker Image:
    • docker pull <image_name>:<tag>
    Tag Docker Image:
    • docker tag <source_image>:<tag> <target_image>:<tag>
    Prune Unused Images:
    • docker image prune -a
  • Container Resource Usage:
    • docker stats <container_name or container_id>
    Copy Files between Host and Container:
    • docker cp <local_path> <container_name or container_id>:<container_path>
    Export Container Filesystem:
    • docker export <container_name or container_id> > export.tar
    Import Container as Image:
    • docker import export.tar <image_name>:<tag>
    Run Container Interactively:
    • docker run -it <image_name>:<tag> /bin/bash
  • Docker Network:
    Create Custom Bridge Network:
    • docker network create --driver bridge <network_name>
    List Networks:
    • docker network ls
    Inspect Network:
    • docker network inspect <network_name>
    Connect Container to Network:
    • docker network connect <network_name> <container_name or container_id>
    Disconnect Container from Network:
    • docker network disconnect <network_name> <container_name or container_id>
    Remove Unused Networks:
    • docker network prune
  • Docker Volume:
    Create Docker Volume:
    • docker volume create <volume_name>
    List Docker Volumes:
    • docker volume ls
    Inspect Volume:
    • docker volume inspect <volume_name>
    Remove Docker Volume:
    • docker volume rm <volume_name>
  • Build and Run Docker Compose:
    • docker-compose up -d
    Scale Docker Compose Services:
    • docker-compose up -d --scale <service_name>=<num_instances>
    Stop and Remove Docker Compose Containers:
    • docker-compose down
  • Docker Security Scanning:
    Docker Scan (Built-in):
    • docker scan <image_name>:<tag>
    Anchore CLI Scan:
    • Use Anchore CLI for advanced image scanning.
    • anchore-cli image add <image_name>:<tag>
    • anchore-cli image wait <image_name>:<tag>
    • anchore-cli image content <image_name>:<tag>
  • Docker Registry:
    Push Docker Image to Registry:
    • docker push <registry_url>/<image_name>:<tag>
    Login to Docker Hub Registry:
    • docker login
    Logout from Docker Hub Registry:
    • docker logout
  • Docker Swarm:
    Initialize Swarm:
    • docker swarm init
    Join a Swarm as a Worker:
    • docker swarm join --token <worker_token> <manager_ip>:<manager_port>
    Service Deployment in Swarm:
    • docker service create --name <service_name> <image_name>:<tag>
    Inspect Swarm Service:
    • docker service inspect <service_name>
  • Docker Troubleshooting:
    Check Container Logs (Follow Mode):
    • docker logs -f <container_name or container_id>
    Run a Command in a Running Container:
    • docker exec -it <container_name or container_id> <command>
    Docker Events:
    • docker events
    Docker System Events:
    • docker system events
    Docker System Information:
    • docker system info