Docker Commands - cheatsheet

Background: Virtualization & Containerization

Docker Concepts

Networking

Docker networking allows containers to communicate with each other and with the outside world. Here are key concepts related to Docker networking:

  1. Bridge Network:

    By default, Docker creates a bridge network called bridge on each host. Each container connected to this network is assigned an internal IP address, and containers can communicate with each other using these internal IP addresses. The bridge network also provides outbound internet access for containers.

    <aside> 💡 To get the internal IP, you can inspect the network or container.

    </aside>

  2. User-Defined Bridge Network:

    You can create custom bridge networks to isolate and segment containers. Containers within a user-defined bridge network can communicate with each other using container names as hostnames.

    # Create a custom bridge network
    docker network create mynetwork
    
    # Run a container and attach it to the custom network
    docker run --network=mynetwork myimage
    
  3. Host Network:

    Containers can be attached to the host's network namespace using the -network=host option. This allows containers to share the network namespace with the host, making them directly accessible on the host's network.

    # Run a container using the host network
    docker run --network=host myimage
    
  4. Overlay Network:

    Overlay networks are used for communication between containers across multiple Docker hosts in a swarm. This enables the creation of multi-host services. Overlay networks use the VXLAN protocol to encapsulate and route container traffic.

  5. Port Mapping: Port mapping allows exposing container ports to the host or the external network. This is achieved using the p option with the docker run command.

    # Expose container port 80 to host port 8080
    # host:container
    docker run -p 8080:80 myimage
    
  6. Network Inspection: Docker provides commands to inspect network configurations. The docker network inspect command can be used to view details about a specific network, including its containers and IP addresses.

    # Inspect a network
    docker network inspect mynetwork
    

Exposing a Port: