Post

Docker

Containers

Basics

  1. Run container (e.g. nginx)
    • docker run nginx
  2. Run container with specific version (tag)
    • docker run <container_name>:<tag>
    • docker run redis:4.0
  3. List containers
    • Running
      • docker ps
    • All
      • docker ps -a
  4. Stop container
    • docker stop <container_id/name>
  5. Remove container
    • docker rm <container_id/name>
  6. Map Ports
    • Map Docker Host port to Docker Container port
      • docker run -p <host_port>:<container_port> <container_id/name>
    • E.g. Map port 80 of Docker Host to port 5000 of Docker Container
      • docker run -p 80:5000 my_container
  7. Map Volumes
    • Map Docker Host Volume to Docker Container Volume
      • docker run -v <host_path>:<container_path> <container_id/name>
      • docker run -v /opt/datadir:/var/lib/mysql mysql
  8. Get more detailed info of container
    • docker inspect <container_id/name>
  9. Show Container logs
    • docker logs <container_id/name>
  10. Set Environmental Variables
    • docker run --env <variable>=<value> <container_id/name>
    • docker run --env JAVA_VERSION=11 my_container
    • --env ~ -e
  11. Display Environmental Variables
    • Use docker inspect command

Images

  1. Build docker image
    • docker build <path_of_Dockerfile>
    • docker build .
  2. Tag docker image you are building
    • docker build . -t <repository>/<image_name>
  3. Tag docker image you have built
    • docker tag <image> <repository>/<image_name>:<tag>
  4. Download image
    • docker pull <image_name>
  5. Show downloaded images
    • docker images
  6. Download image into computer
    • docker image save <image> > image.tar
  7. Show image history (some commands and sizes)
    • docker history <image>
  8. Remove image
    • docker rmi <image_name>

Commands

  1. Run container with command
    • docker run ubuntu <command>
    • docker run ubuntu sleep 5
  2. Run command on a running container
    • docker exec <container_id/name> <command>
    • docker exec my_ubuntu cat /etc/hosts
  3. Run container in detached mode
    • docker run -d <container_id/name>
  4. Attach to running container
    • docker attach <container_id/name>
  5. Enable interactive mode to make container receive input
    • docker run --interactive <container_id/name>
    • --interactive ~ -i
  6. Attach to terminal (to see its output)
    • docker run --tty <container_id/name>
    • --tty ~ -t
  7. Run container in combined mode to have both terminal input and output
    • docker run -it <container_id/name>

COMMANDs and ENTRYPOINTs

  1. Specifying Commands in Dockerfile
    • Shell form
      • CMD <command> <param>
    • JSON Array form
      • CMD ["<command>", "param"]
  2. CMD vs ENTRYPOINT
    • CMD allows to override Dockerfile command and arguments
      • docker run my_container <command> <arguments>
    • ENTRYPOINT only appends arguments into Dockerfile
      • docker run my_container <arguments>
  3. Set default arguments
    • It must be JSON Array form
    • You provide both ENTRYPOINT and CMD
      • 1
        2
        
        ENTRYPOINT ["sleep"]
        CMD ["5"]
        
  4. Override ENTRYPOINT
    • docker run --entrypoint <command> <image> <args>
    • docker run --entrypoint sleep2 ubuntu-sleeper 10

Networking

Network: Bridge

  • Default network
  • Network within Docker Host, allows communication between all containers
  • You need to map Ports to access containers
  • By default one Bridge Network is created
    • If you need more, you have to specify this explicitly

Network: Host

  • Ports are shared with the “real” network
  • You don’t have Network isolation in this case (so beware of port collision with “real” network)
  • Also you can’t run multiple containers on the same container ports, as they are now published
  • --network=host

Network: None

  • No network - totally isolated
  • --network=none

Commands

  1. Create new Network
    • docker network create --driver <driver> --subnet <a.b.c.d/n> <network_name>
    • docker network create --driver bridge --subnet 182.18.0.0/16 my_network
  2. Embedded DNS
    • All Docker containers are accessible by their names
    • This DNS runs at 127.0.0.11
  3. Show Network info of container
    • Use docker inspect