Notes - Docker Containers Command Lines

Follows are command lines that I often use (not necessarily the best approach) while interacting with Docker Container. Commands are correct up to Version 18.03.1.

  • show all containers, including inactive ones:

    docker ps -a
    
  • show images:

    docker images
    
  • run a container from an image in interactive mode:

    docker run -d -it --name [nameOfContainer] [imageName:version or imageID]
    
  • get into a container with a bash shell:

    docker exec -it [containerID] /bin/bash
    
  • show images:

    docker images
    

Push customized image to docker repo

  1. Login to the docker.

    docker login -u tokumei
    
  2. Tag the customized image

    Lets assume the image name is : customizedImage. By default, it has the latest tag. My registered username on docker cloud is tokumei. I created a repository name myCustomizedImages. Therefore, my personal repository is tokumei/myRepo. I want to push my image with tag myFirstCustomizedImage by this command:

    docker tag customizedImage:latest tokumei/myRepo:myFirstCustomizedImage
    
  3. Push to my personal docker repo

    docker push tokumei/myRepo:myFirstCustomizedImage
    

Control several containers

  • Create (1) interactive and (2) privileged containers (3) sharing volume (read-only) with host machine, and (4) detach

    for i in {1..[numberOfContainers]}; do docker run -d -it --privileged=true -v [hostPath]:[containerPath]:ro --name container$i [imageID]; done
    
  • execute code in docker:

    for i in {1..[numberOfContainers]}; do docker exec container$i sh -c "your cmd here 'can be wrapped between a pair of single quote marks'"; done
    

    e.g.

    for i in {1..[numberOfContainers]}; do docker exec container$i sh -c "tmux new-session -d -s [sessionName] 'sudo python /root/myVolume/myCode.py'"; done
    
Avatar
Nguyen Phong Hoang
Postdoctoral Researcher