Docker basic command lines

  • How to pull a container image to create a container ?
$ docker image pull alpine
  • How to list images ?
 $ docker image ls
  • How to create a container ?
$ docker container create -i -t alpine sh
  • How to start a container ?
$ docker container start CONTAINER_ID
  • How to run a container in an interactive fashion ?
$ docker container run -i -t --name myalpine alpine sh
  • How to detach from the running container ?

By pressing Ctrl-p Ctrl-q from a running container.

  • How to list the running containers?
$ docker container ls
  • How to attach to a running container ?
$ docker container attach CONTAINER_NAME
  • How to run a container in the background ?
$ docker container run -d ubuntu /bin/bash -c
  • List the currently running containers again

  • Did you notice that your detached container is not showing up in the list? Can you find out why?

  • Let’s try and find out. List all containers, be they running, stopped or detached

$ docker container ls --all
  • Did you identify the container that you ran in detached mode? Why did it exit gracefully?

  • How to see logs of a container ?

$ docker container logs CONTAINER_ID
  • How to stop a container?
$ docker container stop CONTAINER_ID
  • How to pause a container ?
$ docker container pause be9f86d4d1df
  • How to remove/delete a container ?
$ docker container stop CONTAINER_NAME
$ docker container rm CONTAINER_NAME
  • How to remove a container forcefully?
$ docker container rm -f CONTAINER_NAME
  • How to automatically remove the container when it exits
$ docker container run --rm --name temp_container alpine ping -c 3 amazon.com
  • How to set the hostname for a container ?
$ docker container run -h alpine -it --rm alpine sh
  • How to set the environment variable to a container ?
$ docker container run -it --env "WEB_HOST=172.168.1.1" --rm  alpine sh
  • How to get all the details about a container ?
$ docker container inspect CONTAINER_ID
  • How to limit the memory of a container ?
$ docker container run -d --name memorylimitAlpine --memory "200m" alpine top
$ docker container inspect memorylimitAlpine | grep -i Mem
  • How to create a new process inside a running container ?

$ docker container exec CONTAINER_ID ip a

N.B: With docker container exec option, we can create any process inside an already running container. This option is very useful for debugging.

  • How to set the restart policy to a container ?

$ docker container run -d --restart=always --name mywebserver nginx
  • How to use copy files between the host system and a running container ?
$ echo "This is our container bootcamp" > index.html
$ docker container cp index.html mywebserver:/usr/share/nginx/html/
  • How to get the IP of a running container?
$ docker container inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mywebserver
  • How to label a container ?
$ docker container run -d --label env=prod nginx
  • Create an additional container using a different label
$ docker container run -d --label env=dev nginx
  • How to use filters to list containers?
$ docker container ls --filter label=env=dev
  • How to remove or delete all the containers with one command?
$ docker container rm -f `docker container ls -q -a`
  • How to remove all stopped containers?
$ docker container prune