Some handy command to work with containers. We’ll assume a docker image ubuntu is available on current machine.
- Run interactive shell (or any other command) on ubuntu image and create a container (named ubuntu1).
## docker run -it --name [container_name] [image_id] /bin/bash $ docker run -it --name ubuntu1 ubuntu /bin/bash root@bdda3386fd58:/# pwd /
To detach tty without existing shell, use
Ctrl-p
+Ctrl-q
. Otherwise useCtrl-d
.To attach it again use
docker attach [container_id_or_name]
-
List containers
$ docker ps #running containers CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bdda3386fd58 ubuntu "/bin/bash" About a minute ago Up About a minute ubuntu1 $ docker ps -a #all containers
- Start/stop/restart/remove/kill containers
$ docker stop [container_id_or_name] # stop container $ docker rm [container_id_or_name] # remove container $ docker start [container_id_or_name] #start container $ docker restart [container_id_or_name] #restart container $ docker kill [container_id_or_name] #kill signal to container
- Run a command inside a running container
## docker exec [container_id_or_name] command $ docker exec bdda3386fd58 "ls"
- Get an interactive bash shell in a running container
$ docker exec -it [container_id_or_name] /bin/bash
- Run multiple commands inside a running container
## docker exec [container_id_or_name] /bin/bash -c "command1;command2" $ docker exec bdda3386fd58 /bin/bash -c "cd /root; ls -a" . .. .bash_history .bashrc .profile