A "work in progress"-list with the Docker commands I use the most

With the start of 2019 I began using Docker Containers in the projects I am working on. Working with a command line tool (Windows Powershell, etc.) is essential when creating new containers. In the following list you can find the commands that I use the most:

  • docker pull IMAGE_ID
    • Download the specified docker image
  • docker run -d IMAGE_ID
    • Starts a new container instance with the specified image id. The id can be found with docker image ls
    • the -d option instructs the container to run in the background
    • alternative to -d, you can use the -it option which tells Docker to create a bash shell in the container, so that we can directly interact with it
  • docker exec -it CONTAINER_ID “bash”
    • Starts an interactive bash shell inside your running container (here the CONTAINER_ID)
    • To quit the bash type exit. The container continues running
  • docker exec -it sql1 mkdir /folder1/folder2/folder3
    • Creates a new directory (3 folders) inside the container
  • docker stop CONTAINER_ID
    • Stops a container with the given id
    • You could also use the name of the container instead of the container id
  • docker ps
    • Lists all running containers in the current machine
    • With the -a parameter you can list all containers (running and not running)
    • Alternative you can use the docker container ls command which also lists all running containers
  • docker image ls
    • Lists all, not deleted, images we have created
  • docker build -t aRandomName:aRandomVersion .
    • builds a new image with the given name and a version (optional)
    • The command has to be executed inside a folder which contains the Dockerfile file
    • The . is the path where the local files are being sent to when we create the new image
    • use the –no-cache option if you want to build the image from scratch
    • The -f parameter can be used to define the name of a specific Dockerfile
  • docker start CONTAINER_ID
    • Starts a new container instance with the specified container id. The id can be found with docker ps -a
  • docker image rm -f IMAGE_ID:
    • Removes a created image with the given id. The id can be found with the docker image ls or docker images command
    • You can delete more than one image with one command. Simply add the IDs one after the other
    • -f is for force
  • docker rm CONTAINER_ID
    • Removes the container. You can check that the container is gone by typing docker ps -a
    • You can also use the name of the container instead of the container id
  • docker container inspect CONTAINER_ID
    • Returns a JSON object with the complete configuration of the container
  • docker container prune
    • Removes all containers (running and not running) from Docker. Very helpful command when you want to start with fresh instances of your Docker Images. Use it with caution ;)
  • docker attach CONTAINER_ID
    • Attach to a running container
  • docker cp foo.txt CONTAINER_NAME:/foo.txt
    • Copy one local file into a running container
  • docker cp CONTAINER_NAME:/foo.txt foo.txt
    • Copy a file from a running container into your computer
  • docker cp src/. CONTAINER_NAME:/target
    • Copy multiple files into a running container
  • docker cp CONTAINER_NAME:/src/. target
    • Copy multiple files from a running container into your computer
  • docker-compose up
    • The “easy” way to start and orchestrate a collection of containers. For more complex orchestration-scenarios check Kubernetes

For a more detailed documentation of the previous commands and their available option visit the Docker documentation.

comments powered by Disqus